if 语句在连接中间? [英] if statement in the middle of concatenation?
本文介绍了if 语句在连接中间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这不行吗?还是我只是做错了?尝试了它的多种变体,似乎无法找到有关该主题的任何可靠信息.有什么想法吗?
Does this not work? or am I just doing it wrong? Tried multiple variations of it, and can't seem to find any solid info on the subject. any ideas?
$given_id = 1;
while ($row = mysql_fetch_array($sql))
{
if ($i < 10){
$display = '<a href="' . $row['info'] . '" onMouseOver="' . if($row['type']=="battle"){ . 'showB' . } else { . 'showA'() . "><div class="' . $row['type'] . "_alert" . '" style="float:left; margin-left:-22px;" id="' . $given_id . '"></div></a>';
推荐答案
if 是一个独立的声明.这就像一个完整的陈述.所以你不能在字符串的串联之间使用它.更好的解决方案是使用速记三元运算符
if is a self standing statement. It's a like a complete statement. So you can't use it in between concatenetion of strings or so. The better solution is to use the shorthand ternary operatior
(conditional expression)?(ouput if true):(output if false);
这也可以用于字符串的连接.示例:
This can be used in concatenation of strings also. Example :
$i = 1 ;
$result = 'The given number is'.($i > 1 ? 'greater than one': 'less than one').'. So this is how we cuse ternary inside concatenation of strings';
您也可以使用嵌套的三元运算符:
You can use nested ternary operator also:
$i = 0 ;
$j = 1 ;
$k = 2 ;
$result = 'Greater One is'. $i > $j ? ( $i > $k ? 'i' : 'k' ) : ( $j > $k ? 'j' :'k' ).'.';
这篇关于if 语句在连接中间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文