An Introduction with PHP

Published by: 0

PHP is the fourth most-popular computing language and  I’d bet more consumer-driven web applications are built using it compared to the top three.  Given its popularity, highlighting areas of optimizations is helpful for a lot of people.  Below, you will find 5 general areas of PHP optimizations in no particular order.

1) Install a PHP Opcode optimizer (like XCache, APC, or memcache)

PHP scripts need to be compiled each time your webpage is accessed so that your site’s operating system can understand the request.  Opcode optimizers cache the compiled results so that the compiled step can be avoided.

There a handful of opcode optimizers and you should try out each to see which gives the best optimization for your system.  The easiest to install (in my opinion) is XCache., but they all will make your site much, much faster.

2) Configure your php.ini file

php.ini is the configuration file for PHP (side note: to see what your current PHP configurations are, create a PHP file on your site, add “phpinfo”, then open it in your browser.) Two PHP configuration parameters I look for first are “memory_limit” and “max_execution_time”.   The memory_limit value is the amount of memory your system will allocate to PHP.  A good base value is about 16MB.  However, if you are running PHP opcode programs (like XCache or APC above), a better value for memory_limit would be 32MB.  Setting this value too low will make your PHP very slow or not work at all.

The max_execution_time parameter is the maximum amount of time your site will allow your code to run.  I usually set this value to 30 seconds.  However, if I need to run a data-intensive script that a user of my site won’t interact with, I temporarily set it to 60 seconds.  If this value is too high, a bad PHP script could continually run without dying, which would slow down the site for other users.

3) Test PHP execution times by printing timestamps

When rendering a page on your website, hundreds of lines of code can be used.  Finding out which lines are slow is imperative for optimizations.  What I recommend is printing the current time every  twenty lines or so by writing “echo time();”.  Using subtraction, you’ll be able to tell which parts of your code are going the slowest and optimize from there.

4) Small code tricks

Here is a list of a handful of PHP optimizations.  These aren’t game-changers by themselves, but using them everywhere in your site might make a difference.

a) use ‘echo’ instead of ‘print’:

echo $var;  //faster
print $var; //slower

b) use single quotes instead of double quotes:

echo ‘hello’;    //faster
echo “hello”;  //slower

c) reference files using the absolute path instead the relative path

require ‘/var/www/my_file.php’;    //faster
require ‘my_file.php’;  //slower

5) Reduce calls to your database

When you call your database, PHP has to wait for the response.  Therefore, 20 calls to the database will go slower than 15.  Instead of writing 20 small MySQL queries in your PHP code, try combining them efficiently into 15.

Next week, I will highlight a few MySQL query optimizations you can incorporate to make your site faster as well.