PHP: switch vs if

if-statement, php, switch-statement

Solution

The performance aspect is completely irrelevant.

As PHPBench shows, even with 1,000 operations, the difference between the two is about 188 microseconds, that's 188 millionths of a second. PHP code usually has much bigger bottlenecks: a single database call will often take tens of milliseconds, that's tens of thousands of times more.

Use whichever you like, and whichever is better for your code's readability - for many checks, most likely the `switch`.

Problem

Which form is more efficient This one: ``` switch($var) { case 1: break; case 2: break; } ``` ..or this one: ``` if( $var === 1 ) { } elseif( $var === 2 ) { } ``` in terms of performance?

Original source

Related problems