提问人:privateace 提问时间:2/15/2009 更新时间:4/25/2015 访问量:13724
如何正确格式化 PHP 'IF ELSE' 语句?
How to correctly format PHP 'IF ELSE' statements?
问:
这是一个长期存在的问题,我在许多热气腾腾的编码会议中遇到了这个问题。
一个人以这种方式编码,另一个人以这种方式编码。所以经过多次推拉,我很好奇...... 是否有任何正确的方法来表达 PHP 'IF ELSE' 语句?
就我个人而言,我使用:
if ($variable == 'setvalue')
{
$variable = executefunctiononvariable($variable);
} else {
$variable = executedifferentfunctiononvariable($variable);
}
经过多次争论,我得到了其他选择,例如:
if ($variable == 'setvalue')
{
$variable = executefunctiononvariable($variable);
}
else
{
$variable = executedifferentfunctiononvariable($variable);
}
或
if ($variable == 'setvalue')
$variable = executefunctiononvariable($variable);
else
$variable = executedifferentfunctiononvariable($variable);
或
if ($variable == 'setvalue') {
$variable = executefunctiononvariable($variable);
} else {
$variable = executedifferentfunctiononvariable($variable);
}
答:
简而言之,这不是正确的做法。只要它有效,无论你觉得最好的是什么,你都可以使用。你应该选择一个,然后坚持下去,这将使你的代码更容易识别。
唯一的问题是,如果您不包含“{”字符,则仅限于一个表达式或函数。
此外,如果您只想定义变量,则可以使用以下代码:
$variable = (CONDITIONAL STATEMENT) ? "It was true" : "It was false";
最重要的是,从事项目的程序员几乎都遵循相同的编码准则。因此,开个会,选择一个或另一个,然后坚持下去。
我个人格式化我的if/else,就像上一个一样:
if ($variable == 'setvalue') {
$variable = executefunctiononvariable($variable);
} else {
$variable = executedifferentfunctiononvariable($variable);
}
在我看来,你的版本是 1 和 3 的混合体。
我还与完成所有这些工作的编码人员一起工作,但从未听说过标准程序。
php 网站使用最后一个:https://www.php.net/manual/en/control-structures.elseif.php
在某些情况下,我还使用第二个示例,因为 if 语句总是很短。如果它有可能变得更长(每行超过 1 行),我会做 #1。我尽量避免#2,因为以后很难添加{}。
评论
我使用最后一个:
if ($variable == 'setvalue') {
$variable = executefunctiononvariable($variable);
} else {
$variable = executedifferentfunctiononvariable($variable);
}
话虽如此,你选择哪一个并不重要,只要确保你是一致的。
没有对错之分,这是一种意见。就个人而言,我最喜欢最后一个(1TBS???)。我从不使用没有牙套的那个,我认为它一般都是不好的风格。
唯一能真正为你回答这个问题的人是其他将要处理代码的人。重要的是,每个人都同意编码标准。你选择哪个标准不如每个人都使用它的事实重要。
正确的方法是遵循项目的编码标准。如果你没有,请从 PHP-FIG、Zend、Symfony 等中采用一个。
这种形式看起来很受欢迎:
if (condition) {
statements
} else {
statements
}
对于变量赋值,只有当语句可以清晰地放在一行上时,我才会使用三元:
$variable = !empty($foo) ? $foo : 'default';
更新:我已经删除了关于多行三元语句的部分,因为我不再认为这是一个明智的做法。
评论
The PEAR coding standard is the PHP coding standard. I would recommend to get used to it as you will find it in other projects such as Zend, Doctrine, Symfony, Horde and many, many more.
评论
I used to do (2) all the time but got it beaten out of me from Java programming as Sun's coding conventions use (4). So now I'm pretty used to (4). I've been doing a bit of C# lately and it seems to use (2) by default (sigh, here we go again).
In PHP from habit I do (4) but (2) is fine too. I don't like (1) at all.
And (3) is dangerous. Personally I think braces should be required by the syntax of the langauge even if its just for one statement. Saves you getting into trouble. I think that's how Perl does it from memory.
What I also hate is when people do this:
if (something) {
// do something
}
else if (something else) {
}
That one drives me batty. So I only find (2) and (4) acceptable. I don't care which one it is, as long as it's done consistently, preferably within the conventions for the language.
评论
I personnally prefer:
if(something){
doSomething();
}
elseif(somethingElse){
doSomethingElse();
}
else{
doAnotherThing();
}
评论
At my company we use:
if ($variable == 'setvalue')
{
$variable = executefunctiononvariable($variable);
}
else
{
$variable = executedifferentfunctiononvariable($variable);
}
I doesn't really matter aslong as there is a standard
Don't forget about
if (expression):
// code goes here
elseif (another expression):
// code goes here
else:
// code goes here
endif;
I personally like this structure when I'm cooking some tag soup.
评论
Really to me... it just doesn't matter. I believe you should be able to read either way without issues. Does it really matter if the curly brace is on a new line or not? Does it really matter if there's a space after the closing parenthesis or not?
As long as the code is done in a such way that there's been at least an attempt at making it readable, I really just don't care.
Is there a correct way? Well if there was, then why do we have options of doing it differently?
下一个:?:操作员与。If 语句性能
评论