PHP Best Practices in Performance – Part 3
After the PHP Best Practices in Performance Part 1 and Part 2, here’s the last post from the series:
1. Instead of print(), use echo. It is a statement, so you avoid the function overhead of print().
2. Incrementing a pre-initialized local variable is 9-10 faster than incrementing an undefined local variable.
3. Incrementing a local variable in method is the fastest. Almost the same as calling local variable in a function.
4. Use echo multiple parameters instead of string concatenation:
<?php
echo ‘This ‘, ‘is ‘, $faster;
echo ‘Tish ‘ . ‘is ‘ . $slower;
?>
5. Where it is possible use ++$i, instead of $i++.
<!–adsense–>
6. Regex could consume a lot of time, so think twice about each regex in your code.
7. It’s a good practice to profile your code. The profiler will show you, which parts of your code how much time consume. The Xdebug debugger already contains a profiler.
8. Cache as much as possible. You can use memcached, it is a high-performance memory object caching system intended to speed up dynamic web apps by alleviating database load. Caching is useful because your script won’t have to be compiled on every web request.
<!–adsense–>
I hope you’ve found something useful in the tips above. Soon, I’ll post new post about general php best practices.
