PHP Optimization
Remember, some of these are negligible, and can change at any time. The PHP engine may optimize things differently as new versions come out. A lot of these could probably just be considered "micro-optimizations" and usually are not worth substantial work to implement.
Part of coding is maintenance of the code. Making things overly complex to cut down on speed can sometimes be more of a headache than it is worth!
Optimization priorities/levels are *low* *medium* *high* below, with references. I hope to continue to build this case with examples and any changes/additions.
This page really needs to be cleaned up, I know :)
' is faster than " with strings, because it does not process $variables \[[1](#references)\] \*low\*
echo is faster than print \[[1](#references)\] \*low\*
BAD:
```php
for($i=0;$i< sizeof($array); $i++)
```
Why? It will call sizeof() for each iteration!
BETTER: \[[1](#references), [6](#references)\] \*medium\*
```php
for($i=0, $k=count($j); $i< $k; $i=$i+1)
```
EVEN BETTER (when needing string concatenation) \[[3](#references)\] \*medium\*
```php
for ($i=0, $k=count($j); $s=''; $i<$k; $i=$i+1)
```
EVEN BETTER (looping through an array) - needs no additional temporary or incremented variables \*high\*
```php
foreach($array as $value)
```
(if you only need values)
```php
foreach($array as $key => $value)
```
(if you need the keys as well)
`for($i=0; $i++< $count;)` might be the fastest `for` loop (not tested) \[[4](#references)\]
`strcmp` (and related) > `substr` > `pcre` > `ereg` \*high\* - ereg is being deprecated anyway!
`strpos` > `preg_match` > `ereg` \[[3](#references)\] \*high\* - ereg is being deprecated anyway!
`str_replace` > `preg_replace` > `ereg_replace` typically \[[3](#references)\] - ereg is being deprecated anyway! \*high\*
cache- and structure-wise, arrays > apc > files on local filesystem > memcached \[[2](#references)\] \*high\*
`++$i` is faster than `$i++` - 3 opcodes instead of 4 \[[1](#references)\] \*medium\*
`if(!isset($foo{5}))` is faster than `if(strlen($foo) < 5)` \[[1](#references)\]
to search an array `$value = isset($foo[$bar])) ? $foo[$bar] : NULL;` is faster than `in_array()` \[[1](#references)\]
`if (42 == $foo)` is better (maybe faster) than `if ($foo == 42)` \[[1](#references)\]
`true` is faster than `TRUE` \[[1](#references)\]
concatenating strings with single quotes `('foo '.$string)` is faster than `"foo $string"` \[[5](#references)\]
Switch is better than long lists of if/else statements \[[6](#references)\]
`if(empty($var))` is much faster than `if($var == '')` (reported to be 20% faster)
MySQL: Selecting the primary key and limiting to 1 result reported to be 10x faster than using COUNT(\*) \[[7](#references)\]
`if(isset($myArray[$key]))` > `if(array_key_exists($key, $myArray))` > `if( $myArray[$key])` - the last one is not really legitimate anyway \[[8](#references)\]
`mysqli_stmt` > `mysql_query` > `mysqli_query` > `mysqli_multi_query` \[[9](#references)\] - NOTE: is this still true for mysqlnd? What about PDO then too?
TODO: branch prediction - is it better to go TRUE or FALSE first?
References
- http://ilia.ws/archives/12-PHP-Optimization-Tricks.html
- http://www.mysqlperformanceblog.com/2006/08/09/cache-performance-comparison/
- http://phplens.com/lens/php-book/optimizing-debugging-php.php
- http://cosminb.blogspot.com/2004/09/performance-tweaking-for-vs-while-vs.html
- http://blog.libssh2.org/index.php?/archives/28-How-long-is-a-piece-of-string.html
- http://www.php.lt/benchmark/phpbench.php
- http://mysqldba.blogspot.com/2007/06/to-count-or-not-to-count.html
- http://news.php.net/php.general/264211
- http://www.johnjawed.com/benchmarks/