我只是在浏览一个论坛,有人问他们在网上找到的PHP文件。在代码中有几个这样的地方:
if ($REMOTE_ADDR == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR);
我一直认为,如果条件为真,则需要用括号括起来。还有其他选择吗,例如您是否不在同一行?
还有另一行是这样的: if ($action != ""): mail("$adminaddress","Visitor Comment from YOUR SITE",
if ($action != ""): mail("$adminaddress","Visitor Comment from YOUR SITE",
我的本能是说这行不通,但是我也不知道它是否是过时的PHP文件并且它曾经可以工作?
您可以执行以下其他语句:
<?php if ($something) { echo 'one conditional line of code'; echo 'another conditional line of code'; } if ($something) echo 'one conditional line of code'; if ($something) echo 'one conditional line of code'; echo 'a NON-conditional line of code'; // this line gets executed regardless of the value of $something ?>
然后您还可以编写if-else的替代语法:
<?php if ($something): echo 'one conditional line of code'; echo 'another conditional line of code'; elseif ($somethingElse): echo 'one conditional line of code'; echo 'another conditional line of code'; else: echo 'one conditional line of code'; echo 'another conditional line of code'; endif; ?>
使用备用语法,您也可以退出解析模式,如下所示:
<?php if ($something): ?> one conditional line of code<br /> another conditional line of code <?php else: echo "it's value was: $value<br />\n"; ?> another conditional line of code <?php endif; ?>
但这真的很快就会变得凌乱,我不建议您使用它(除非用于模板逻辑)。
并使其完整:
<?php $result = $something ? 'something was true' : 'something was false'; echo $result; ?> equals <?php if ($something) { $result = 'something was true'; } else { $result = 'something was false'; } echo $result; ?>