Search code examples
phpperformanceconditional-statementscurly-brackets

Which is Better Performance Wise - if(condition){result} VS if(condition) result;


I know the performence difference will be very slight in both scenarios, but I was wondering which is a more practical, performance-improving version to write conditions in PHP.

if(condition){
  result;
}

VS

if(condition)
  result;

Solution

  • Both are exactly the same, it's coding style and has nothing to do with performance. Adapt the style you like better.

    Some hint tools suggest to use the first style to avoid mistakes like this:

    if(condition)
      result;
      foo; // Nothing to do with the condition but you can get confused.
    

    While if you were using curly braces, this wouldn't have happen:

    if(condition){
        result;
    }
        foo; // Nothing to do with the condition but now it's clear.
    

    I'm not using curly braces by the way for one statement in if, as this scenario isn't too difficult to avoid for non noobs.