PHP 条件,需要括号吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/381259/
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
PHP conditionals, brackets needed?
提问by Levi
I was just browsing a forum and someone asked about a PHP file they had found on the web. It has several spots like this in the code:
我只是在浏览一个论坛,有人询问了他们在网上找到的一个 PHP 文件。它在代码中有几个这样的地方:
if ($REMOTE_ADDR == "") $ip = "no ip";
else $ip = getHostByAddr($REMOTE_ADDR);
if ($REMOTE_ADDR == "") $ip = "no ip";
else $ip = getHostByAddr($REMOTE_ADDR);
I have always thought brackets are needed to enclose what you want to do if the condition is true. Is there some other alternative, such as if it is on the same line you don't?
我一直认为如果条件为真,需要用括号括起您想要执行的操作。是否有其他选择,例如如果它在同一条线上,您不在同一条线上?
There is also another line like this:
if ($action != ""):
mail("$adminaddress","Visitor Comment from YOUR SITE",
还有另一行是这样的:
if ($action != ""):
mail("$adminaddress","Visitor Comment from YOUR SITE",
My instinct is to say this wouldn't work, but I also don't know if it is an outdated PHP file and it used to work?
我的直觉是说这行不通,但我也不知道它是否是一个过时的 PHP 文件并且它曾经可以工作?
回答by Jacco
you can do if else statements like this:
你可以这样做 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
?>
and then you can also write if - else in an alternate syntax:
然后你也可以用另一种语法写 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;
?>
with the alternate syntax you can also fall out of parsing mode like this:
使用替代语法,您也可以像这样脱离解析模式:
<?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;
?>
But this gets really messy really fast and I won't recommend it's use (except maybe for template-logic).
但这很快就会变得非常混乱,我不建议使用它(模板逻辑除外)。
and to make it complete:
并使其完整:
<?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;
?>
回答by chaos
To go into a little more detail, the reason that the braces are optional is that the syntax looks like:
更详细地说,大括号是可选的原因是语法如下所示:
if(CONDITION) BLOCK
[elseif(CONDITION) BLOCK]
[else BLOCK]
BLOCK can be a single statement:
BLOCK 可以是单个语句:
foo();
or it can be a brace-enclosed group of statements:
或者它可以是大括号括起来的一组语句:
{
foo();
bar();
}
回答by smartcoder
In my opinion
在我看来
if ($REMOTE_ADDR == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR);
is valid, but much harder to read than:
是有效的,但比以下内容更难阅读:
if ($REMOTE_ADDR == "") {
$ip = "no ip";
} else {
$ip = getHostByAddr($REMOTE_ADDR);
}
回答by Velojet
9 years on and I'm surprised no-one's mentioned the ternary operator:
9 年过去了,我很惊讶没有人提到三元运算符:
$ip = ($REMOTE_ADDR == "") ? "no ip" : getHostByAddr($REMOTE_ADDR);
Much clearer for assignment IMHO - because it leads out with the variable being assigned to, as for usual variable assignment.
恕我直言,分配更清晰 - 因为它会以被分配的变量引出,就像通常的变量分配一样。
回答by Darryl Hein
Yes, excluding the braces is allowed, although many times I have heard 2 reasons for not using that syntax:
是的,允许排除大括号,尽管我多次听到不使用该语法的两个原因:
- It's harder to read. Less obvious to another programmer.
- If you ever wany to add something inside the if, then you need to add the braces which is harder after then when you're first coding since most editors will add the closing brace for you.
- 读起来更难。对另一个程序员来说不太明显。
- 如果您想在 if 中添加一些东西,那么您需要添加大括号,这在您第一次编码时更难,因为大多数编辑器会为您添加右大括号。
Also, yes, the colon syntax is valid. The alternatives can be found here: http://php.net/manual/en/control-structures.alternative-syntax.php
另外,是的,冒号语法是有效的。替代方法可以在这里找到:http: //php.net/manual/en/control-structures.alternative-syntax.php
回答by chaos
Braces (not brackets) are optional in PHP, as in most C-like syntax. Maybe you're thinking of Perl; they're required there, for that form of if syntax.
大括号(不是括号)在 PHP 中是可选的,就像在大多数类似 C 的语法中一样。也许你在想 Perl;对于这种形式的 if 语法,它们是必需的。
The colon thing is an alternate control structure form that PHP supports. I hate it, but some people (particularly template system designers, apparently) love it.
冒号是 PHP 支持的另一种控制结构形式。我讨厌它,但有些人(尤其是模板系统设计师,显然)喜欢它。
回答by Scott Evernden
brackets are needed to enclose what you want to do if the condition is true
I can't think of any language that requires this
我想不出任何需要这个的语言

