你可以做 if else 语句,如下所示:
<?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;
?>