Over a million developers have joined DZone.

Optimization - PHP Benchmarks

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

A lot of the gripes about PHP as a language come down to the fact that there are a million ways to accomplish the same task, but only a couple are truely 'correct' - as in significantly better for one reason or another. Out of all those reasons script efficiency and speed are the driving force behind all experienced programmers.  That being said it's hard to know what the right way to do things are. Sure there are 100 ways to do something but all but 2 or 3 of those take an obscene amount of time. Normally, during development, you won't notice long load times because it's not getting hammered by thousands of users requesting the same page.

Chris Vincent has set up The PHP Benchmark to help visually show how some methods are more effecient than others.

PHPBench.com was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed.

A few highlights:

foreach($aHash as $val); 


while(list(,$val) = each($aHash)) ; 

  • foreach($array as $value) is the fastest way to loop through an array.
  • foreach($array as $key => $value) is slightly slower by about 10-15% so use it only when necassary.
  • Using while() and for() loops dramatically increases the server load especially when used in conjuction with list() and each().
$a = aaaaaaa;  
echo $a,$a,$a,$a; 


$a = aaaaaaa;
print $a,$a,$a,$a; 


  • echo is slightly faster than print.
  • Whenever possible use single quotes with echo.
  • Single quotes, as you may already know, will not evaluate variables placed inside them so you can why this would save time.
  • It appears that when chaining several variables together you should seperate them with commas(,) but if you are chaining strings together periods(.) are more effecient. I would be interested to learn more about this oddity.

if and elseif (using ===)



if and elseif (using ==)


  • As far as comparison operators, the data suggests that 3 equal signs (Identical) is the most effecient. However all the other options are not that far behind.
  • I wish there were more examples using more PHP operators like less than, not equal to, etc.
isSet() with array-var that was *not* set


is_array() of a non set value 


  • It is always wise to check if a variable is set before attempting to check its type.
  • For example: if (isSet($var) && is_array($var)) {}
  • is_array() and similar functions are pretty heavy and there is no point in calling them if the variable doesn't even exist. Since PHP short-circuits the second comparison would not even be made.

Overall, I loved all the information this page has to offer and look forward to more of this type of thing popping up. Apparently the author has been getting a lot of great feedback from the community which I hope will result in some better explinations for the results.

Check out the full report for all the details [The PHP Bench].

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Mike Bernat, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}