Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly format PHP 'IF ELSE' statements?

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);
}
like image 250
privateace Avatar asked Feb 15 '09 04:02

privateace


People also ask

What is the difference between if and if-else in PHP?

In PHP we have the following conditional statements: if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more than two conditions.

What is the use of if statement in PHP?

The if statement is used to execute a block of code only if the specified condition evaluates to true. This is the simplest PHP's conditional statements and can be written like: The following example will output "Have a nice weekend!" if the current day is Friday: <?php $d = date ("D"); if ($d == "Fri") { echo "Have a nice weekend!"; } ?>

How to use conditional statements in PHP?

You can use conditional statements in your code to do this. In PHP we have the following conditional statements: if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if...elseif...else statement - executes different codes for more ...

What is the use of elseif in PHP?

There may be a series of cascaded if - else blocks which makes the program tedious. PHP provides elseif statement to address this problem. As the keyword indicates, elseif is combination of if and else keywords. It works similar to else keyword, with a little difference. Conditional logic of the code has multiple if conditions.


3 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.

like image 73
Darryl Hein Avatar answered Oct 21 '22 02:10

Darryl Hein


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.

like image 39
Paolo Bergantino Avatar answered Oct 21 '22 01:10

Paolo Bergantino


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.

like image 44
Steve Clay Avatar answered Oct 21 '22 00:10

Steve Clay