PHP performance tips & tricks

  1. Prefer using single-quotes 'instead of double-quotes'. Between double-quotes, php parse several times the chain to substitute the variables and their values: this method is slower than the use of single-quote.
Opt for $ x = 'test' instead of $ x = "test" 
echo "I'm going to test {$ tab ['index']}";  ==> slow 
echo 'I'm going to do a test', $ tab ['index'];  ==> + fast 
  1. Comma is faster than concatenations. Find the fastest code:
  print "Hi my name is $ a. I am $ b"; 
  echo "Hi my name is $ a.I am $ b"; 
  echo "Hi my name is". $ a. ". I am". $ b; 
  echo "Hi my name is", $ a, ".I am", $ b; 
  echo 'Hi my name is', $ a,'.  I am ', $ b;  => The fastest + 
  1. Always use quotes in table indexes:
  $ x ['index'] is 7 times faster than $ x [index] 
  1. Do not use functions in loop declarations
  for ($ i = 0; $ i <count ($ array); $ i ++) { 
  // code 

  $ count = count ($ array); 
  for ($ i = 0; $ i <$ count; $ i ++) { 
  // code 
  1. The echo function is faster than print. See http://www.phpbench.com/
  2. If possible, declare your methods 'static'. Gain in performance multiplied by 4.
  3. Delete variables you no longer need to free memory.
  unset ($ data) or unset ($ array); 
  1. Avoid the magic methods get, set, autoload and call with PHP 5.
  2. Use absolute links for your inclusion of 'include' and 'require' files.
  include ( '/ home / example / public_html / config.php ");  // absolute path 
  include ( "config.php");  // relative path 
  define ('ABSPATH', dirname (__ FILE __). '/'); 
  require_once (ABSPATH.'options.php '); 
  1. Avoid using the time () function while you can have the same thing with
  2. Avoid unnecessary function calls such as:
  php_version () while we can use the constant PHP_VERSION 
  php_uname ('s') while we can use the constant PHP_OS 
  php_sapi_name () while we can use the PHP_SAPI constant 
  1. In your php.ini, put short_open_tags = Off. Do not use short open tags (): Conflicts with XML files.
  2. STOP regular expressions if it is not essential. Use strncasecmp, strpbrk and stripos instead of regex
  // SLOW 
  if (preg_match ("! ^ foo_! i", "FoO_")) {} 
  // FASTER 
  if (! strncasecmp ("foo_", "FoO_", 4)) {} 

  // SLOW 
  if (preg_match ("! [a8f9]!", "sometext")) {} 
  // FASTER 
  if (strpbrk ("a8f9", "sometext")) {} 

  // SLOW 
  if (preg_match ("! string! i", "text")) {} 
  // FASTER 
  if (stripos ("text", "string")! == false) {} 

  // Proper way 
  if (! strncmp (PHP_OS, 'WIN', 3)) { 
  if (! strncasecmp (PHP_OS, 'WIN', 3)) { 

  // Bad way 
  if (substr (PHP_OS, 0, 3) == 'WIN') { 
  if (strtolower (substr (PHP_OS, 0, 3))) == 'win') { 

  // To avoid 
  if (preg_match ('! ^ WIN!', PHP_OS)) { 
  if (preg_match ('! ^ WIN! i', PHP_OS)) { 

str_replace is faster than preg_replace, but strtr is 4 times faster than str_replace.
  strtr> str_replace> preg_replace 
  1. Do not rely on register_globals or magic quotes. You must always escape your data.
  2. Initialize your variables before using them.
  3. Comment your code AND create your documentation with phpDocumentor http://www.phpdoc.org/
  4. Code according to a standard.
  5. Do not reinvent the wheel, PHP has many libraries and extensions. Take the time to find out if what you need does not already exist.
  6. Use an OPCodes cache for PHP: APC, XCache, eAccelerator ...
  7. Never trust your users. Check and filter all data sent by them.
  8. Use an IDE development environment such as Eclipse PDT or Visual Studio Code to speed up your development.
  9. Use a PHP Framework: Laravel, Zend, ...
  10. When developing your application, put error_reporting = E_ALL in your php.ini file. Good error handling and a strict error management policy will allow you to test the functionality of your application and quickly locate errors.
  11. The PHP manual is your best friend. http://www.php.net/manual/fr/index.php
  12. DRY: Do not repeat yourself. Avoid the redundant code.
  13. Make your code readable: use the indentations and spaces to air your code.
  14. Use only <?Php PHP code tags
  15. Use OOP: object-oriented programming, as well as design patterns. All must not be objects either: there are also tables. Each call to a method or an object requires a lot of memory.
  16. Store encrypted and non-clear passwords in your database.
  Example: echo SHA1 ('password'); 
  1. Use Xdebug for debugging your application.
  2. To increase the performance and speed of your PHP scripts, use the output buffering ob_start (), ob_end_flush (); Use ob_start () at the beginning of your code will allow you to boost the speed of your website by 5 to 15%. You can also use gzip compression with ob_gzhandler () for faster downloads.
  3. The removal of errors by the use of @ in front of the function is to be avoided.
  To avoid: @todo(); 
  1. Activate mod_deflate in APACHE2 for data compression.
  2. In your php.ini, configure:
  register_globals = Off 
  magic_quotes_gpc = Off 
  expose_php = Off 
  register_argc_argv = Off 
  always_populate_raw_post_data = Off 
  session.use_trans_sid = Off 
  session.auto_start = Off 
  session.gc_divisor = 1000 
  1. PHP Tip: isset () is faster than strlen ()
  if (strlen ($data) <5) {echo "blah blah";  } 
  is equivalent to 
  if (! isset ($data{5})) {echo "blah blah";  } ==> + fast 
  1. Good to know:
  Prefer incrementing ++$i to $i++ BECAUSE 
  PHP uses 4 OPcodes for $i++ WHEN 
  it only use 3 for ++$i. 
  1. Use a template engine to facilitate the internationalization of your application as well as cache management. Like Smarty or Twig that separates the application logic from the presentation.
  2. For PHP scripts, the most important bottleneck is the CPU: microprocessor of the server. Generally, a more powerful processor is more useful than adding RAM.