Tips to Optimize Your PHP Script for Better Performance for Developers

Following the best practices while coding your PHP script is a good starting point to write a well optimized PHP code.

This tutorial provides few tips to optimize PHP code from a developer point of view.



  1. Use the latest version of PHP
  2. Use PHP's native functions will not only help you write code faster, but will also make it more efficient
  3. Use “= = =” instead of “= =”, as the former strictly checks for a closed range which makes it faster.
  4. Disable Debugging Messages
  5. If a method can be static, declare it static. Speed improvement is by a factor of 4
  6. Set the maxvalue for your for-loops before and not in the loop.
  7. Unset your variables to free memory, especially large arrays.
  8. Use Caching Techniques
  9. See if you can use strncasecmp, strpbrk and stripos instead of regex
  10. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
  11. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
  12. Get into the habit to unset the variables and close database connection in your PHP code. It saves memory.
  13. Reduce Number of Hits to DB
  14. Do use switch over lots of if statements
  15. Use JSON instead of XML while working with web services as there are native php function like json_encode( ) and json_decode( ) which are very fast. 
  16. Use isset( ) where ever possible instead of using count( ), strlen( ), sizeof( ) to check whether the value returned is greater than 0.
  17. foreach > for > while  - According to our benchmarks, the foreach loop is much faster than the for loop, and the for loop is faster than the while loop.
  18. Make use of the countless predefined functions
  19. Dont define too many indexes in the tables. It will slow down the DB operations(create, edit, update, read)
  20. Do consider using the Singleton Method when creating complex PHP classes.
  21. Do use POST over GET for all values that will wind up in the database for TCP/IP packet performance reasons.
  22. Do use ctype_alnum,ctype_alpha and ctype_digit over regular expression to test form value types for performance reasons.
  23. Do use tmpfile or tempnam for creating temp files/filenames
  24. Do use error_reporting (E_ALL); during debug.
  25. Do set Apache allowoverride to “none” to improve Apache performance in accessing files/directories.
  26. Do use a fast fileserver for serving static content (thttpd). static.mydomain.com, dynamic.mydomain.com
  27. Do serialize application settings like paths into an associative array and cache or serialize that array after first execution.
  28. Do use PHP output control buffering for page caching of heavilty accessed pages
  29. Do use PDO prepare over native db prepare for statements. mysql_attr_direct_query=>1
  30. Do NOT use SQL wildcard select. eg. SELECT *
  31. Do use database logic (queries, joins, views, procedures) over loopy PHP.
  32. Do use shortcut syntax for SQL insers if not using PDO parameters parameters. eg. INSERT INTO MYTABLE (FIELD1,FIELD2) VALUES ((“x”,”y”),(“p”,”q”));

Comments