Fast PHP
by Justin Silverton
The following methods can help improve scalability with php applications.
1) object code caching
Each time a request comes to your server for a php script, it has to go through the compiler and then execute the object code. If this is cached, the 1st step is skipped and you end up with a faster and more responsive script.
There are many object code caching packages available on the Internet (some free, some commercial):
A) Ioncube: http://www.ioncube.com/
B) Zend Encoder: http://www.zend.com/products/zend_safeguard
C) Turckl MMCache: http://freshmeat.net/projects/turck-mmcache/
2) Template systems
Template systems provide a different type of caching. Content caching. Template systems work well in a situation where there is static data on one or many of your pages that doesn’t have to be reloaded. Caching systems also provide a separation of code and html, which will not only improve completion time of the overall project, but make it easier for future improvments. Most template systems for php are available for free:
A) Smarty Templates: http://smarty.php.net/
B) Pear Templates: http://pear.php.net/package/html_template_it/redirected
C) PHP savant: http://phpsavant.com/yawiki/
3) Distributed object caching systems
The most widely used system of this type is memcached (http://www.danga.com/memcached/).
This type of system makes your overall site faster by caching the majority of your database data into a large memory pool.
an interesting excerpt from their site:
“Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.”
4) PHP variables that can be set
variables_order = ‘GPC’
register_argc_argv = ‘Off’
register_globals = ‘Off’ (this is a good idea to keep off for security purposes as well)
always_populate_raw_post_data = ‘Off’
magic_quotes_gpc = ‘Off’
Disable Error Logging. This is a good idea to keep on when you are developing your scripts, but it has been known to decrease overall performance.
Use IP address to access your database. Although this is sometimes not possible, you will get a slight boost in lookup speed if the IP address is used to access your database rather than its hostname.
5) Output Compression
Almost all browsers these days support something called gzip compression. Gzip compression can decrease the overall size of your output by up to 80%, but with a tradeoff: cpu usage will go up by around 10%. The benefit of using this compression type is the fact that not only will your bandwidth be decreased, but your pages will load faster.
enabling it in php (add the following lines to php.ini):
zlib.output_compression = On
zlib.output_compression_level = (level) (where level is 1-9. Youy may want to try different values to see what is best for your system).
if you are using apache, you can also enable the mod_gzip module. It is highly configurable, with the ability to modify output based on MIME types, files, or browser settings.
6) Other things that may help
when using a database, only retrieve the data that you are actually going to use. This may sound like a no-brainer, but I have often times worked on projects where the original programmer used (select * from mytable) when they could have used (select fieldIneed from mytable).
index database tables whenever possible
Learn more about this Here
specific language tricks
An interesting blog article I found mentions many interesting tricks that can be used: http://ilia.ws/archives/12-PHP-Optimization-Tricks.html
an article on zend.com about measuring performance: http://www.zend.com/zend/trick/trick-optimizing-php.php
36 Comments so far
Leave a reply

TurckMMCache is no longer maintain. eAccelerator is a fork of it and this project live.
Thanks for the information!
[…] Jaslabs: High performance php - Fast PHP September 4th, 2006 […]
[…] Jaslabs: High performance php » Fast PHP September 4th, 2006 […]
Don’t forget APC: “APC is a free, open, and robust framework for caching and optimizing PHP intermediate code.” — http://pecl.php.net/package/APC
I Would have liked to see PHPTAL in the template section.
If you want to edit your templates in a WYSYWIG, PHPTAL is ideal as it uses attributes that do not break the scemantics of the XHTML.
TinyButStrong is another template that supports page caching.
Due to its dynamic nature, its non-cached execution speed is not impressive, however it speed up development considerably.
If performance is really critical but cache is out of question, Smarty and PEAR_Flexy allows compilation of template.
But when you’re compiling template, you may consider using straight PHP instead of going through another system.
Profiling is a must in optimisation & scaling. If you’re not attacking the source of problem you’re really not helping much.
Almost all PHP debugger supports profiling, but I personally found XDebug the easiest to use.
Most op-code cache also supports in-PHP caching of data. This can be utilised to cache static data like configurations or results of complex calculations.
Since disk is the bottleneck of modern computer, it also helps if number of source files is reduced. The performance gain can be considerable on a strained server that miss disk cache a lot.
Quick and enjoyable read, thanks.
You really must mention APC as it will be bundled with PHP 6
I’m suprised you didn’t mention APC. It seems to be way more popular than the other opcode caches you’ve mentioned and seems to be well maintained. I’ve heard rumors that it will be included into php6.
For output compression, mod_gzip is meant for apache 1.x mod_deflate was introduced for apache 2.x.
Disabling error-logging shouldn’t be recommended/suggested IMHO. Even in a well tested environment, errors/exeptions may still occur. A production website in general will have more permutations of events happening, thus a higher chance for errors. Good developers would proactively scan through error logs and fix it.
A much better (free/open source) caching system is APC. It’s in PECL, so it’s well supported and easy to install and use. It’s also maintained by a number of very well known PHP people, so I highly recommend it.
One thing to always remember is that premature optimisation is the root of all evil - only optimise things after you’ve benchmarked and found out EXACTLY where the bottleneck is. Another commenter above recommended XDebug which is fantastic for this.
–Simon
[…] read more | digg story […]
> Use IP address to access your database.
If your application and database share the same server, it’s faster to use a socket (instead of TCP/IP). You can accomplish this by specifying the socket manually, or simply connecting to the named “localhost” instead of 127.0.0.1 will achieve the same effect.
Wisdom of the crowds: http://trac.lighttpd.net/xcache/wiki/
It should be noted, that generally PHP is very fast - and its not the bottle neck, I/O is the bottleneck things like databases are where your site will slow down - if you are having to use things like eAccelerator or APC to speed up your PHP code, it would probably be more helpful to do a code review of your bottleneck pages, or if that doesn’t work - get more servers.
Also if you are having I/O problems, things like Memcached are life savers - and I strongly suggest using it.
[…] Here is an article that details 6 things that you can try in an attempt to make your PHP scripts faster! Hey Erik can this help you with your Senior Project?read more | digg story Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages. […]
eAccelerator is a little buggy, and can crash the server easilly… So I wouldn’t really advice anyone to use it
[…] Improve scalability with php applications. […]
Point is: php does not scale for very large applications. Use a language like Java for real large scale distributed systems.
Php is perferct for smale scale websites.
Good stuff. Thanks for the info!
[…] Jaslabs shares six key points on improving the speed of your dynamic PHP scripts including: object code caching, template system, distributed object caching system, turning off some global PHP variable settings, output compression and database query optimization. Listen to this post […]
You should check out Phalanger.
http://www.codeplex.com/Phalanger
It integrates PHP with ASP.NET, pre-compiling your PHP into MSIL, the same way eAccelerator and others pre-compile scripts. The difference is, it’s done by ASP.NET, and your scripts run on IIS6. With your scripts running on ASP.NET, they’re running managed which protects you from a great deal of security issues.
As for performance, I suggest you give it a try, because our tests show PHP running under ASP.NET under IIS6 under Windows 2003 completely destroys the same hardware running PHP with eAccelerator under Apache Linux 2.6 or FreeBSD. Sounds hard to believe, but we’re a shop full of linux/unix guys and all our stuff is currently on Apache and we gave ASP.NET a chance at running our PHP and the results were stunning.
Phalanger gives you the ability to use the .NET framework in your PHP scripts, and to use the code behind model of ASP.NET, but you don’t have to use that stuff if you don’t want to! It is 100% compatible with existing classic style PHP scripts, and PHP modules.
I’m no microsoft fanboi, but if you’re a real developer you shouldn’t be a unix fanboi either, you should test things out and see what works best for you. I highly recommend you test Phalanger with your existing PHP codebase.
[…] High Performance PHP (NÖRDAdót) […]
Thanks for the great tips.
Good stuff. Two small things that didn’t get mentioned but can have a big effect on large applications: includes and php’s parsing model.
Includes (and requires) can take increase load time dramatically. While they’re good for code separation, they suck on performance. Use them carefully.
PHP’s parsing model is interesting and you can get some performance gains by knowing how it works. Make sure you use single quotes as much as possible since PHP does not parse anything inside of them. This is really useful when accessing array elements. Seems like a small tweak but can save a ton on processing and can increase speed when you’re working with large arrays.
[…] 6 Ways to Make PHP Scripts Lightning fast! Just found this article over at digg - thought it’d be of some use to people […]
FastPHP y métodos para escalar aplicaciones PHP…
Justin Silverton publico en Agosto un artÃculo sobre FastPHP y métodos para mejorar la escalabilidad de aplicaciones PHP que me ha gustado y paso a traducir:
1) Cache de código objeto
Cada vez que una petición llega a nuestro servidor a por un sc…
The Zend product is called the Zend Accelerator. It is a part of the Zend Platform(http://www.zend.com/products/zend_platform)
[…] Jaslabs: High performance php » Fast PHP The following methods can help improve scalability with php applications (tags: php caching optimization) […]
[…] Jaslabs […]
I’d like to say that using template systems is slow itself! And why we are using them? It’s just a voice of the crowd!
Read a bit of PHP’s history. PHP is a great template engine itself. Try doing the same you’ve done with, for example, Smarty with pure php and ob_*() functions. You will see the difference.
don’t forget xcache http://trac.lighttpd.net/xcache/ (which also exposes an interesting api for caching variables and stuff, dunno if the others do)
[…] 6 things you can try to make your php scripts fast!read more | digg story […]
[…] Jaslabs: Fast PHP […]
[…] Programming: 6 Ways to Make your PHP lightening Fast A guide to optimizing your php scripts to reduce load times and increase performance. […]