views:

1867

answers:

12

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);
}
+1  A: 

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";
Sam152
Um that always resolves to "It was true".
cletus
@cletus: Um why would it?
Mark
+2  A: 

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.

gahooa
+15  A: 

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.

Darryl Hein
The PHP.net manual uses the last method because we follow the PEAR coding standards. (http://pear.php.net/manual/en/standards.php) My personal favourite as well.
Ross
+7  A: 

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.

Paolo Bergantino
+2  A: 

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.

gpojd
+5  A: 

Answer: According to your project's Coding Standard. If you don't have one, adopt one from Zend, PEAR, etc.

The last is almost always preferred:

if (condition) {
    statements
} else {
    statements
}

Though the code you gave I would consider writing as:

$variable = ('setvalue' === $variable)
    ? executefunctiononvariable($variable)
    : executedifferentfunctiononvariable($variable);

The above has a few advantages:

  1. It's immediately clear that the intent of the statement is to assign a value to $variable.
  2. By typing the equivalence condition with the literal on the left, you reduce the spread of a whole class of bugs. This tricks dates back to C, at least.
  3. When checking equivalence for strings, the "===" operator is a bit faster (no type checking/cast needed).
mrclay
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 ...
strager
... the code read unnaturally. "count($array) > $i" is harder to understand than "$i < count($array)", for example.
strager
@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).
Sydius
@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.
mrclay
I personally only use the "? : " when the entire statement easily fits on 1 line. Anything larger/complex becomes an "if" for readability
Bob Fanger
I would wrap that whole thing in an extra set of brackets. It looks like $variable is getting assigned a boolean value, not the return of one of those functions. That said, I'm still not overly found of it, and I hate your "literal on the left" thing. I understand it's to prevent bugs, but readability is more important to me. Any bug caused by forgetting an = (or two in this case!) is usually immediately obvious... or maybe not in this case, because you made it so darn compact, that if you screwed it up it would always evaluate to true, and then take the first function.
Mark
+2  A: 

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

mike
At least someone mentioned that there already is a coding standard.
Gumbo
+2  A: 

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.

cletus
Amen. 789012345
Mark
+1  A: 

I personnally prefer:

if(something){
    doSomething();
}
elseif(somethingElse){
    doSomethingElse();
}
else{
    doAnotherThing();
}
Pim Jager
Agreed. This is the "correct" way.
jrockway
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
Mark
Agreed with Mark. I don't like this way.
Carson Myers
A: 

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

Thomaschaaf
+1  A: 

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.

rodey
I only like this if you're tossing HTML inbetween.
Mark
A: 

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?

kastermester