Optimising PHP code

This article helps you to improve the performance of your PHP scripts, by optimising php code.  You can apply these few best practices to speed up your script.

PHP engine have made a number of significant performance improvements over the years.  If you are using old versions of PHP such as PHP4 or below, you might have to consider upgrading to the latest version of PHP.

 

  • Use static methods when possible as they are much faster by a factor of 4.
  • Echo is faster than print
  • Unset your variables after usage to free up the memory
  • Close database connections when you’re done with them
  • Don’t call the function as part of the loop condition

Consider this example:

for ($i = 1; $i < count($myarr); ++$i) {

//

}

A much better solution is to calculate the array size just once, outside of the loop, like this:

$len = count($myarr);

for ($i = 1; $i < $len; ++$i) {

//

}

 

  • Methods in derived classes run faster than ones defined in the base class
  • Use predefined functions of PHP as much as possible
  • Use single quotes rather than double quotes to produce strings without the use of variables to make things little fatser, as PHP will parse the string with double quotes.
  • Do NOT use SQL wildcard select. eg. SELECT *
  • Try to index the fields that are commonly used in your SQL WHERE statements.

 

  • Avoid using SQL queries within a loop

Consider this example:

foreach ($userList as $user) {

$query = ‘INSERT INTO users (first_name,last_name) VALUES(“‘ . $user[‘first_name’] . ‘”, “‘ . $user[‘last_name’] . ‘”)’;

mysql_query($query);

}

Instead of using a loop, you can combine the data into a single database query.

$userData = array();
foreach ($userList as $user) {
$userData[] = ‘(“‘ . $user[‘first_name’] . ‘”, “‘ . $user[‘last_name’] . ‘”)’;
}
$query = ‘INSERT INTO users (first_name,last_name) VALUES’ . implode(‘,’, $userData);
mysql_query($query);

  •  str_replace() is much more efficient than regular expressions like ereg_replace() and preg_replace() at replacing strings.
  • If you’re using regular expressions, then ereg_replace() and preg_replace() will be much faster than str_replace()
  • Consider using a  ternary operator, instead of using an if/else statement altogether. The ternary operator frees up line space and makes your code less cluttered, making it easier to scan 
  • Using Suppression Operator @ is rather slow
  • Use Pre-increment where possible, as pre-increment (++$i) is much faster than post-increment ($i++)
  • enable output buffering, and to use gzip compression for the buffers.

To enable output buffering for all your scripts, open up your php.ini file and set output_buffering to 1 and output_handler to “ob_gzhandler” (without the quotes).

  • Use the array and string functions where possible. They are faster than writing equivalent code in PHP.
  • Use HTML caching if you have data that rarely changes. Even if the data changes every minute, caching can help provided the data is synchronized with the cache. Depending on your code complexity, it can improve your performance by a factor of 10.
  • Optimize your loops first. Move loop invariants (constants) outside the loop.
  • PHP script is slower than HTML page, so try to use HTML pages when possible
  • For searching for substrings, the fastest code is using strpos(), followed by preg_match() and lastly ereg().

Leave a Reply

Your email address will not be published. Required fields are marked *