php 如何php if else语句并返回不同的div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5044898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to php if else statement and return different divs?
提问by tokyowp
I'm trying to create an if-else statement, that will return different divs. I believe it is failing because there are too many '
in the else statement.
我正在尝试创建一个 if-else 语句,它将返回不同的 div。我认为它失败了,因为'
else 语句中的内容太多。
<?php $blogentryid = get_the_ID();
if ($blogentryid=="1572") {
echo '<div>Hello</div>'
}
else {
echo '<div class="socialnews2"><!-- start div social-->
<div class="twitternews2">
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php the_title(); ?>" d
ata-url="<?php the_permalink() ?>" data-via="giantmangocom">Tweet</a>
<script type="text/javascript">
//async script, twitter button fashiolista.com style
(function() {
var s = document.createElement('SCRIPT');
var c = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://platform.twitter.com/widgets.js';
c.parentNode.insertBefore(s, c);
})();
</script>
</div>
</div>
<div class="facebooknews2">
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&layout=button_count&
amp;show_faces=false&width=80&action=like&colorscheme=light;height=21"" scrolling="no" frameborder="0" style="border:none; overflow:hidden; wid
th:80px; height:21px;" allowTransparency="true"></iframe>
</div>'
}
?>
回答by mellamokb
When you want to show conditional content, open and close the <?php
tags instead of using echo statements.
当您想要显示条件内容时,请打开和关闭<?php
标签而不是使用 echo 语句。
<?php if ($blogentryid == "1572") { ?>
<div>Hello</div>
<?php } else { ?>
<div class="socialnews2"><!-- start div social-->
... rest of content here
<? } ?>
It will also ensure that your php code inside of the div gets evaluated as you intended.
它还将确保您在 div 中的 php 代码按照您的意图进行评估。
回答by Mauro
you cannot have a <?php ?>
tag inside a <?php ?>
tag.
<?php ?>
标签内不能有<?php ?>
标签。
<?php .. data-text="<?php the_title(); ?>" ...?>
maybe do something like this:
也许做这样的事情:
<?php echo '... data-text="'; echo the_title(); echo '...'; ?>
回答by Chris Baker
This code is all over the place.
这段代码无处不在。
At the end of every line of executable code, you must have a semi-colon (";") before the syntax is considered valid. When you're trying to assemble PHP plus HTML, it isn't necessary to echo everything. Here's a cleaned up version of your code below.
在每一行可执行代码的末尾,必须有一个分号 (";"),然后才能认为语法有效。当您尝试组合 PHP 和 HTML 时,没有必要回显所有内容。这是下面代码的清理版本。
As you can see, when you don't need any PHP logic, you can end php with ?>
and return to normal HTML. You seemed to have that idea, but you contained it all in an echo
statement. That would have worked, but you had some <?php echo $whatever; ?>
statements mixed in. Take a look at the cleaned up code I posted and you should be able to see where you were going wrong.
如您所见,当您不需要任何 PHP 逻辑时,您可以结束 php?>
并返回正常的 HTML。你似乎有这个想法,但你把它全部包含在一个echo
声明中。那会奏效,但是您<?php echo $whatever; ?>
混入了一些语句。看看我发布的清理过的代码,您应该能够看到哪里出错了。
<?php
$blogentryid = get_the_ID();
if ($blogentryid=="1572") {
echo '<div>Hello</div>';
} else {
?>
<div class="socialnews2"><!-- start div social-->'
<div class="twitternews2">
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php the_title(); ?>" data-url="<?php the_permalink() ?>" data-via="giantmangocom">
Tweet
</a>
<script type="text/javascript">
//async script, twitter button fashiolista.com style
(function() {
var s = document.createElement('SCRIPT');
var c = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://platform.twitter.com/widgets.js';
c.parentNode.insertBefore(s, c);
})();
</script>
</div>
<div class="facebooknews2">
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&layout=button_count&show_faces=false&width=80&action=like&colorscheme=light;height=21"" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:21px;" allowTransparency="true"></iframe>
</div>
</div>
<?php
} // end :: if
?>
回答by Chris Thompson
For multiline strings that are as complex as the one you are suggesting, I would suggest simply ending your php block before the output. For instance:
对于像您建议的那样复杂的多行字符串,我建议在输出之前简单地结束您的 php 块。例如:
<?php
if($outputDiv1){?>
<div id="1">Plain old unescaped html code</div>
<?php }else{ //output div 2 ?>
<div id="2">More html</div>
<?php } ?>
I find this method is far easier. If you need at access a php variable from within the html, you simply open up another php block like <input name="name" type="text" value="<?php echo $someVal; ?>" />
我发现这种方法要容易得多。如果您需要从 html 中访问 php 变量,您只需打开另一个 php 块,如<input name="name" type="text" value="<?php echo $someVal; ?>" />
回答by Marc B
The '
in your javascript sections need to be escaped: \'
, otherwise they'll be seen by PHP as the end of the string you're echoing.
将'
在JavaScript的部分需要进行转义:\'
,否则他们将通过PHP被看作是你呼应字符串的结尾。
For large blobs of text like that, look into HEREDOCs, which allow you to output multi-line strings without any worries about quote escaping.
对于像这样的大块文本,请查看HEREDOCs,它允许您输出多行字符串而无需担心引号转义。
As well, you can't embed php blocks within strings as you are:
同样,您不能像这样在字符串中嵌入 php 块:
echo 'hello<?php echo "there" ?>'
will not work.
不管用。