Optimizing PHP on Apache

PHP Logo

There are ways webmasters can optimize Apache to reduce the load on their servers and to decrease website loading time for users. We will discuss optimizations on an Apache server, although many of these will also work on other server operating systems. This tutorial is written assuming that you have administrative permissions on a server. We have another article about optimizing your site for speed, which deals less with PHP and more with server configurations.

Duplicate Variables

The first thing we want to talk about is creating “duplicate variables” in PHP. An example of this is retrieving data from page headers, assigning it to a variable, and using the variable to process this information. (Another example would be taking data from a database and assigning it to variables when it does not need to be in variable form). This may get your code to look cleaner, but it is actually doubling the amount of memory PHP needs to execute the script (since the data is now in the memory twice). To avoid this, simply inline calling the data to avoid the extra variable.

Loops and Databases

You should avoid using loops when making database queries. A separate query for each row is extremely inefficient and results in slow scripts and unnecessary server load. Instead of contacting databases in loops, you could use a loop to combine everything into a single query, and execute the single SQL query after the loop.

Opcode Cache

Caching compiled PHP scripts can save you a lot of time and server load. Software exists to make this easy for you; look into PHP cache solutions like eAccelerator and xCache. These will cache PHP scripts in their compiled state so the same script will not have to be executed repeatedly for each client.

Output Buffering

PHP buffers all data before it is sent out to the client.  You can make your memory buffer smaller to force PHP to send data to clients more often, making your site feel faster. You can do this in your php.ini file – the setting is “output_buffering”.


You can optimize PHP by optimizing your limits. We want to focus on PHP’s memory limit; it sets the amount of memory a script can consume before being killed. It is nice to have this set as low as possible in your php.ini to avoid memory leaks wasting server resources. You can also set the memory limit for directories using the .htaccess.

Less PHP Queries

Although this does not have anything to do with PHP optimization, you can get pages to load faster and reduce server load by minimizing the amount of PHP in your code. If you use any CMS system, you can usually replace PHP code with static code (in places like your website’s name or webpage encoding). Removing this unnecessary PHP will help you decrease webpage load time.

Print vs. Echo

Many benchmarks show that using “echo” is faster than “print” when displaying information from PHP. This is because “print” returns a success status and “echo” does not have any return value. If you do not need a status to be returned, use “echo” for a small performance increase.

MySQL Select

When you learn how to use PHP and mySQL you will probably get in the habit of using “SELECT *” when selecting data. Since you rarely need to select everything, you should only select the rows you need. This will give you a significant performance boost.


I am Techie, the webmaster and main author for the w3techie blog.

You may also like...