Monday, 4 March 2013

10 PHP tips for faster execution

1. Unset or null your variables to free memory, especially large arrays.
2. echo is faster than print.
3. Use require() instead of require_once() where possible.
4. “else if” statements are faster than select statements aka case/switch.
5. Close your database connections when you’re done with them.
6. $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant.
7. ++$i is faster than $ i++, so use pre-increment where possible.
8. Make use of the countless predefined functions of PHP, don’t attempt to build your own as the native ones will be far quicker; if you have very time and resource consuming functions, consider writing them as C extensions or modules.
9. Methods in derived classes run faster than ones defined in the base class.
10. Just declaring a global variable without using it in a function slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.

No comments:

Post a Comment