How to format PHP "IF ELSE" statements correctly?

This was a long problem that I encountered in many coding sessions with hot and pair modes.

One person encodes this code this way. So after much jolting and stress, I'm curious ... Is there any correct way to express the PHP expression “IF ELSE”?

Personally, I use:

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

After many arguments, although other options were presented to me, such as:

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

OR

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

OR

 if ($variable == 'setvalue') { $variable = executefunctiononvariable($variable); } else { $variable = executedifferentfunctiononvariable($variable); } 
+6
code-formatting php semantics if-statement readability
source share
12 answers

I personally format my if / else as the last:

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

Your version is a good mixture of 1 and 3, in my opinion.

I also worked with encoders who do everything and have never heard of the standard.

The php website uses the latter: http://ca2.php.net/manual/en/control-structures.elseif.php

I also use the second example in some cases where the if statement will always be very short. If there is ever an opportunity to get a longer one (more than 1 line each), I will do # 1. I try to avoid # 2 when possible, because it is difficult to add {} later.

+21
source share

I am using the latter:

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

If you say that it doesn’t matter who you go with, just make sure you are consistent .

+8
source share

The right way is to follow the project coding standard. If you do not have one, use one of PHP-FIG, Zend, Symfony, etc.

This form looks very popular:

 if (condition) { statements } else { statements } 

To assign a variable, I will use a triple only if the operator can be legible on one line:

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

Refresh . I removed the bit about multi-line three-dimensional expressions, as I no longer consider this a reasonable practice.

+7
source share

I personally prefer:

 if(something){ doSomething(); } elseif(somethingElse){ doSomethingElse(); } else{ doAnotherThing(); } 
+4
source share

Do not 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 cook some kind of tag soup.

+4
source share

Most importantly, programmers working on a project adhere to the same coding rules to a large extent. So, meet and choose one or the other, and then stick to it.

+3
source share

I always did (2) all the time, but I was beaten from Java programming, as Sun's coding conventions use (4). So now I'm pretty used to (4). I have been doing a bit of C # lately and apparently used (2) by default (sigh, here we are again and again).

In PHP, out of habit, I do (4), but (2) is also good. I don't like (1) at all .

And (3) is dangerous. Personally, I think curly braces should be required by langauge syntax, even if it is for just one statement. Saves you trouble. I think Perl does this from memory.

I hate it too when people do this:

 if (something) { // do something } else if (something else) { } 

It turns me on. Therefore, I only find (2) and (4) acceptable. I don’t care what it is if it is executed sequentially, preferably within the framework of conventions for the language.

+3
source share

There is no right or wrong way, this is an opinion. Personally, I like the latest best ( 1TBS ??? ). I never use one who does not have braces, I consider this a bad style in general.

The only people who can really answer this question for you are other people who will work on the code. It is important that everyone agrees to the coding standard. Which standard you choose is less important than the fact that everyone uses it.

+2
source share

The PEAR encoding standard is the PHP encoding standard. I would recommend getting used to it, as you will find it in other projects such as Zend, Doctrine, Symfony, Horde and many others.

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

+2
source share

In short, this is not the right way. As long as it works, whatever you think is best you can use. You have to choose one and then stick to it, this will make your code easier to recognize.

The only thing is, if you do not include the "{" character, you are limited to one expression or function.

Alternatively, if you only want to define variables, you can use the following code:

 $variable = (CONDITIONAL STATEMENT) ? "It was true" : "It was false"; 
+1
source share

In my company we use:

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

In fact, this is not so important, since there is a standard

0
source share

Really for me ... it just doesn't matter. I believe that you should be able to read in any case without any problems. Does it really matter if the brace is on a new line or not? Does it really matter if there is space after the closing parenthesis or not?

As long as the code is executed in such a way that at least trying to make it readable, I really don't care.

Is there a right way? Well, if it was, then why do we have options to do it differently?

0
source share

All Articles