如何正确格式化 PHP 'IF ELSE' 语句?

How to correctly format PHP 'IF ELSE' statements?

提问人:privateace 提问时间:2/15/2009 更新时间:4/25/2015 访问量:13724

问:

这是一个长期存在的问题,我在许多热气腾腾的编码会议中遇到了这个问题。

一个人以这种方式编码,另一个人以这种方式编码。所以经过多次推拉,我很好奇...... 是否有任何正确的方法来表达 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);
}
PHP 代码格式化可 读性 if-statement 语义

评论


答:

1赞 Sam Becker 2/15/2009 #1

简而言之,这不是正确的做法。只要它有效,无论你觉得最好的是什么,你都可以使用。你应该选择一个,然后坚持下去,这将使你的代码更容易识别。

唯一的问题是,如果您不包含“{”字符,则仅限于一个表达式或函数。

此外,如果您只想定义变量,则可以使用以下代码:

$variable = (CONDITIONAL STATEMENT) ? "It was true" : "It was false"; 
3赞 gahooa 2/15/2009 #2

最重要的是,从事项目的程序员几乎都遵循相同的编码准则。因此,开个会,选择一个或另一个,然后坚持下去。

22赞 Darryl Hein 2/15/2009 #3

我个人格式化我的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,因为以后很难添加{}。

评论

1赞 Ross 2/16/2009
PHP.net 手册使用最后一种方法,因为我们遵循PEAR编码标准。(pear.php.net/manual/en/standards.php)也是我个人的最爱。
8赞 Paolo Bergantino 2/15/2009 #4

我使用最后一个:

if ($variable == 'setvalue') {
    $variable = executefunctiononvariable($variable);
} else {
    $variable = executedifferentfunctiononvariable($variable);
}    

话虽如此,你选择哪一个并不重要,只要确保你是一致的。

2赞 gpojd 2/15/2009 #5

没有对错之分,这是一种意见。就个人而言,我最喜欢最后一个(1TBS???)。我从不使用没有牙套的那个,我认为它一般都是不好的风格。

唯一能真正为你回答这个问题的人是其他将要处理代码的人。重要的是,每个人都同意编码标准。你选择哪个标准不如每个人都使用它的事实重要。

7赞 Steve Clay 2/15/2009 #6

正确的方法是遵循项目的编码标准。如果你没有,请从 PHP-FIG、Zend、Symfony 等中采用一个。

这种形式看起来很受欢迎:

if (condition) {
    statements
} else {
    statements
}

对于变量赋值,只有当语句可以清晰地放在一行上时,我才会使用三元:

$variable = !empty($foo) ? $foo : 'default';

更新:我已经删除了关于多行三元语句的部分,因为我不再认为这是一个明智的做法。

评论

0赞 strager 2/15/2009
A problem I have with your use of ?: is that there may be side effects to those function calls. I did not see the assignment right away, either -- perhaps an indention to past the = would make it more apparent. Personally, I disagree with constants before variables in comparisons, as it makes ...
0赞 strager 2/15/2009
... the code read unnaturally. "count($array) > $i" is harder to understand than "$i < count($array)", for example.
1赞 Sydius 2/15/2009
@strager: You only put the literal on the left when it's checking for equality (==), as the whole point is to avoid accidental assignment (which can cause very odd bugs).
0赞 Steve Clay 2/16/2009
@strager: no need to worry about side effects, the conditional operator does NOT execute both the two options; it's operationally identical to the if/else.
0赞 Bob Fanger 2/16/2009
I personally only use the "? : " when the entire statement easily fits on 1 line. Anything larger/complex becomes an "if" for readability
2赞 Michael Ekoka 2/15/2009 #7

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.

http://framework.zend.com/manual/en/coding-standard.coding-style.html#coding-standard.coding-style.control-statements.if-else-elseif

评论

1赞 Gumbo 2/15/2009
At least someone mentioned that there already is a coding standard.
3赞 cletus 2/15/2009 #8

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.

评论

0赞 snuggles 2/25/2015
Why does it drive you batty? It's the most logical way to write it. Using } else { means it doesn't line up under the if, which is just weird in my opinion.
4赞 Pim Jager 2/15/2009 #9

I personnally prefer:

if(something){
    doSomething();
}
elseif(somethingElse){
    doSomethingElse();
}
else{
    doAnotherThing();
}

评论

2赞 mpen 7/6/2009
There is no "correct" way, and this is the ugliest of all of them. Why? You're creating extra space by leaving the closing brace on its own line as if to give it some added visual separation, but then you nudge the if statement against the code block anyway. It looks like an inconsistent mess to me. And the lack of a space after your else statements also bothers me :D
1赞 Carson Myers 8/6/2009
Agreed with Mark. I don't like this way.
0赞 Pim Jager 2/4/2011
actually, I switched to mrclay's method since writing this answer (his first code block, not the second) And now two think this is ugly.
0赞 snuggles 2/25/2015
The problem with this is the spacing, not the extra linefeed after the block close. Having the extra linefeed means that the elsif and else line up directly below the if. That looks most correct to me, as opposed to doing } else {, which means the else is indented two spaces. But yeah, the lack of spaces before the tests and the open block is horrible.
0赞 Thomaschaaf 2/15/2009 #10

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

4赞 Ryan Rodemoyer 5/15/2009 #11

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.

评论

0赞 mpen 7/6/2009
I only like this if you're tossing HTML inbetween.
0赞 kastermester 8/6/2009 #12

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?