SoFunction
Updated on 2025-03-09

All strategies to improve PHP execution speed


Php setup issues & acceleration suggestions If it cannot be used due to php setting errors during the application process, please check the following parameter settings.
All the following assume that your PHP is installed in d:/php/

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;

; error_reporting is a bit-field. Or each number up to get desired error
; reporting level
; E_ALL - All errors and warnings
; E_ERROR - fatal run-time errors
; E_WARNING - run-time warnings (non-fatal errors)
; E_PARSE - compile-time parse errors
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup
; E_COMPILE_ERROR - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR - user-generated error message
; E_USER_WARNING - user-generated warning message
; E_USER_NOTICE - user-generated notice message

; Examples:

; - Show all errors, except for notices

;error_reporting = E_ALL & ~E_NOTICE

; - Show only errors

;error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR

; - Show all errors except for notices

error_reporting = E_ALL & ~E_NOTICE
; Print out errors (as a part of the output). For production web sites,
; you're strongly encouraged to turn this feature off, and use error logging
; instead (see below). Keeping display_errors enabled on a production web site
; may reveal security information to end users, such as file paths on your Web
; server, your database schema or other information.
display_errors = On

; You should do your best to write your scripts so that they do not require
; register_globals to be on; Using form variables as globals can easily lead
; to possible security problems, if the code is not very well thought of.
register_globals = On

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
session.save_path = "c:/winnt/temp" (can be changed to your existing directory)

; cgi.force_redirect is necessary to provide security running PHP as a CGI under
; most web servers. Left undefined, PHP turns this on by default. You can
; turn it off here AT YOUR OWN RISK
; **You CAN safely turn this off for IIS, in fact, you MUST.**
cgi.force_redirect = 0

; Directory in which the loadable extensions (modules) reside.
extension_dir = ./extensions/
; Or directly set it to your absolute directory, such as:d:/php/extensions/

; GD library, such as the image management system, will use it.
; This file is under d:/php/extensions/.
extension=php_gd.dll
One of the advantages of PHP is its fast speed, which can be said to be sufficient for general website applications. However, if the site has a high number of visits, narrow bandwidth or other factors cause the server to have performance bottlenecks, you may have to think of other ways to further improve the speed of PHP. This article will introduce how to do this from several aspects, so that users can browse more "happy".

Code optimization


I don't want to tell you again here
I think everyone knows how to write cleaner code. When speed is needed, you may have done a lot of work in optimizing PHP source code. What we propose here is that this tedious work can be done by other tools. This is Zend Optimizer, this program is available for free from Zend Technologies' website (/). Its principle is simple, by detecting the intermediate code generated by the Zend engine and optimizing it to achieve higher execution speed. I think optimizing code is a rather tedious task, and the optimized code may become difficult to understand, especially when you put down the PHP program for a while and suddenly the client asks you to make some modifications, you may not understand it yourself;-). Therefore, I suggest that when PHP's source code is more complicated, use Zend Optimizer to do this optimization work. The advantage is that it will not make your code complicated and difficult to understand.

Installing Zend Optimizer is very simple. Just download the relevant precompiled library according to the platform you are using, add two lines to yours, and restart your web server!

zend_optimizer.optimization_level=15zend_extension="/path/to/" zend_loader.enable=Off

You may be a little strange, didn’t you say two lines? How did it become three lines? However, the third line is optional. It seems that forbidding this zend_loader will make the optimization faster, so you might as well add this line to your file. It should be noted that zend_loader can only be disabled if you do not use Zend Encoder Runtime. Regarding Zend Encoder Runtime, it will also be mentioned below.

Want to be faster? Use cache (buffer)

If your PHP application still needs faster speeds, the next solution is buffering. There are several different ways to achieve this. I've tried Zend Cache (reviewed version), APC and Afterburner Cache myself.

All the above mentioned "buffer modules". Their principles are similar. When the php file is requested for the first time, by storing the intermediate code of your PHP source code in the memory of the web server, the "compiled" version in memory is directly provided for the same request in the future. Since it minimizes disk access, this method can indeed greatly improve PHP performance. More conveniently, when your PHP source code is modified, the buffered module can detect these changes and reload the same, so you don't have to worry about the old version of the program that the customer gets. These buffered modules are really good, but which one should I choose? Here are some introductions:

Zend Cache is a commercial product of Zend Technologies (it is also a company that provides us with PHP engine and Zend Optimizer for free). It's really good. After the first run, you can clearly realize that PHP has improved a lot and the server has more idle resources. The disadvantage is that you have to pay for it, but it is still very worth it in terms of cost-effectiveness.

Afterburner Cache is a free buffering module provided by Bware Technologies(). It is currently only a beta version, and the work it does seems to be similar to Zend Cache, but the performance improvement is not as good as Zend Cache, and the existing version cannot work with Zend Optimizer, but it is free.

APC (Alternative PHP Cache) is another free module provided by Community Connect(). Its work is very stable and the speed has improved a lot. It should be noted that I have not found an official test data. These are only tested on my application, so I cannot make a conclusion. Compression of web content (makes your customers more "feeling")

After the above two methods, I believe that the performance of your PHP application has been greatly improved. Now it is time to consider it from another aspect: download speed. If your application is just running within the company, all customers use 100Mb/s Ethernet to connect to the server, this may not be a problem, but if your customers use slow modem connections, you should consider using content compression.
According to IETF specifications, most browsers support gzip's internal
Contains compression. This means that before sending the web content to the client's browser, you can use gzip to compress it first. The browser will automatically decompress the data when it receives it and let the user see the original page. Similarly, there are several different ways to compress the content of a web page.

mod_gzip is a free Apache module provided by Remote Communications(/columns/) that compresses static web pages. It works great, you just need to compile it with apache (or use it as a DSO). Remotecommunications people say it can also compress dynamic content, including mod_php, mod_perl, etc. But I tried it, and it doesn't seem to work. I learned in the mailing list of mod_gzip that this bug will be fixed in the next version (I think it should be version 1.3.14.6f). But you can still use it as compression of static content.

However, we also want to compress dynamic content, so we must find another way. One way is to use (/code/), just call this PHP class at the beginning and end of your PHP script to compress your page content. If the entire site requires such compression, you can call these functions in auto_prepend and auto_append in your file. It works well, but it obviously brings a little overhead on heavy load sites. To learn more about how it works, take a look at its class code (you need to at least add zlib support when compiling PHP). The author's explanations in it are also very detailed, and you can get anything you need to know.

Recently, I also saw an article about PHP output buffering. It says that PHP4.0.4 has introduced a new output buffering method - ob_gzhandler. Its function is the same as the class introduced above, but the difference is that you just need to use the following syntax in yours:

output_handler = ob_gzhandler ;

This will activate PHP's output buffering function and compress everything it sends. For some special reasons, if you don't want to set it here, only change this default setting where you need it (not compressed). Just modify the .htaccess file in the PHP source code directory that needs to be compressed. The syntax used is as follows:

php_value output_handler ob_gzhandler

...or call it directly in your PHP code, in the following way:

ob_start("ob_gzhandler");

This output buffering method is good and does not incur additional system overhead for the server. I highly recommend that you use this method. Its change can be illustrated by the following example. If the customer is using a 28.8K modem, after this processing, he will think that it suddenly changed to an ISDN access. One thing to note is that Netscape Communicator does not support image compression, so it will not be displayed. So unless your customers all use Internet Explorer, you must disable compressing jpeg and gif images. There should be no problem with compressing other files, but I suggest you better test it, especially if the browser uses unusual plug-ins or browsers that are rarely used by people.

Other useful things...

Zend Technologies' online store opened on January 24 this year and sells some interesting products related to PHP. Including the aforementioned Zend Cache, Zend Encoder (simply put, it is a PHP code compiler that can produce compiled classes so that you can sell them to customers without worrying about leaking source code. On web servers that need to run these classes, you will use Zend Encoder Runtime to decode them), Zend Ide (an integrated development environment for PHP with a lot of powerful performance), and support services provided for PHP developers.

in conclusion

Using the techniques mentioned in this article, you will be able to greatly improve the performance of your site, but please note the following:

1. The bottleneck may not be in PHP, you need to examine every object in the application (such as the database)

2. The performance of a web server is limited, so don’t think that poor performance is the reason for PHP. It may also be because of the large number of visits. Your server needs to be upgraded, or consider using a load balancing system (it will cost a lot of money)

3. Don’t think that content compression is not important. In a 100Mb/s LAN, your PHP application may perform well, but consider users who use slow modem.