Home » Php » code formatting – How to correctly format PHP 'IF ELSE' statements?

code formatting – How to correctly format PHP 'IF ELSE' statements?

Posted by: admin July 12, 2020 Leave a comment

Questions:

It’s been a long running issue that I’ve come across in many-a-hot-and-steamy coding sessions.

One person codes this way another codes that way. So after much push and pull I’m curious…
Is there any correct way of phrasing a PHP ‘IF ELSE’ statement?

Personally I use the:

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

After many arguments though I’ve been presented with other options 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);
}
How to&Answers:

I personally format my if/else like the last one:

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

Your version is kind a mixture of 1 and 3, in my mind.

I have also worked with coders that do all of them and have never heard of a standard one.

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

I also use the second example in some cases when the if statement will always be very short. If there’s ever a possibiltiy of it getting longer (more than 1 line each) I’ll do #1. I try to avoid #2 when possible cause it’s hard to add the {} later.

Answer:

I use the last one:

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

That being said, it is pretty unimportant which one you go with, just make sure you are consistent.

Answer:

The Right Way is to follow your project’s coding standard. If you don’t have one, adopt one from PHP-FIG, Zend, Symfony, etc.

This form appears very popular:

if (condition) {
    statements
} else {
    statements
}

For variable assignment I’ll use a ternary only if the statement can fit legibly on one line:

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

Update: I’ve removed the bit about a multi-line ternary statements as I no longer consider this a wise practice.

Answer:

I personnally prefer:

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

Answer:

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.

Answer:

The most important thing is that the programmers working on a project pretty much adhere to the same coding guidelines. So have a meeting and pick one or the other, and then stick with it.

Answer:

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.

Answer:

There is no right or wrong way, it is an opinion. Personally, I like the last one best (1TBS???). I never use the one without braces, I consider it bad style in general.

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

Answer:

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

Answer:

In short, the is no correct way of doing. As long as it works, whatever you feel is the best, you can use. You should pick one and then stick to it, it will make your code easier to recognise.

The only thing is, if you don’t include the “{” character you are limited to one expression or function.

Also, if you are only looking to define variables you can use the following code:

$variable = (CONDITIONAL STATEMENT) ? "It was true" : "It was false"; 

Answer:

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

Answer:

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?