0

I have a PHP code with a lot of single IF statements. The TO-DO's are mostly single statements that would end after a single semicolon. Currently they all have curly braces. I'd like to know if removing them would at all affect the execution speed of the code.

Compare

if( $a == $b ) {
    $c++;
}

with:

if($a == $b) $c++;

The code is huge, so it would take me ages to remove them, so I thought I'd ask before spending hours on it.

Pringles
  • 4,355
  • 3
  • 18
  • 19

5 Answers5

5

The answer is no. Removing or adding them will not affect the execution speed of the code, at all. Not even one bit.

If you are concerned with performance - the first step is to measure it, then you can optimize.

Community
  • 1
  • 1
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
3

If you have performance issues you will not be able to cope with them by removing curly braces!

Martin Mandl
  • 721
  • 3
  • 11
2

Don't worry, the compiler optimizes them for you. Better keep them for readability.

mohkhan
  • 11,925
  • 2
  • 24
  • 27
2

No, but if you want to optimize replace

$c++;

with

++$c;

Also, depending on your scenario (if this script gets executed often and it's not a one-time execution performance issue), you could use a PHP Accelerator so you don't have to compile your script every time. (I used APC in the past, worked nicely)

See this list of PHP Accelerators: http://en.wikipedia.org/wiki/List_of_PHP_accelerators

Sharky
  • 6,154
  • 3
  • 39
  • 72
0

The thing is both are same.In this case

if( $a == $b ) {
    $c++;
}

If you have multiple lines of code to be included in the if statement you have to enclose them in braces.Otherwise the next line after the if statement will be considered as the body of if().

if( $a == $b )
    $c++;

If you written another line after this it will be outside the scope of if() and that is the difference.

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91