我有一些代码,我试图在彼此之间使用不同的ifs,但我遇到了问题..我已经制作了这个测试代码,将显示问题:
<?php
$test = 'lol';
if ($test == 'wat') :
if (!empty($_GET['wat'])) {
echo 'well';
}
elseif ($test == 'lol') :
echo 'loool';
endif;
die();
?>
这将返回此错误:
解析错误:语法错误,意外&#39;:&#39;在第7行的/var/www/domain.com/public_html/test.php中
但是只有在我添加了 if(!empty($ _ GET [&#39; wat&#39;])){}
之后问题是,我做错了什么,或者是否有可能在一个没有的情况下使用带有花括号的if?
答案 0 :(得分:2)
我不确定您在替代if
语法语句中使用基本if
语法尝试完成的任务,但您可能应该避免使用它。它有点不稳定,但以下语法工作正常:
$condition = "test";
$other = null;
if ($condition == "test") :
if (isset($other))
echo "Set";
else
echo "Not Set";
elseif ($condition == "something") :
echo "Huh";
endif;
如果$condition == "test"
为真,则上面会回显“未设置”,并且未设置$other
。
我认为您的示例缺少的是关于基本语法if语句的结束else
语句(我知道这不是必需的,但在这种情况下似乎会引起问题)。将代码修改为:
$test = "lol";
if ($test == "wat") :
if (!empty($_GET["wat"]))
echo "well";
else
echo "Nothing";
elseif ($test == "lol") :
echo "loool";
endif;
使它编译并运行得很好。这很奇怪,但似乎有效。
希望能提供一些见解!
修改强>
对于多行,只需在内部if
语句中添加括号:
$test = "lol";
if ($test == "wat") :
if (!empty($_GET["wat"])){
echo "well";
// Do something else
} else {
echo "Nothing";
// More Stuff
}
elseif ($test == "lol") :
echo "loool";
endif;