• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Highperformance with PHP???


I wonder if there are hints and tricks for highperformance-websites programmed in PHP?

Are there points/details which can improve the performance and speed of PHP?

Or is improvement of performance only possible by proper installation and settings of the Webserver and of the Database-Server?
2 Solutions
Richard QuadlingSenior Software DeveloperCommented:
You can compile the PHP using some of the Zend tools. This will stop the interpretation of PHP and simply become execution. Which is faster.

If not, you can use the optimizer (free I think) from Zend. This makes the script a little faster.

As Richard said,
Zend tools can help you.

Also few basic tricks might help you a bit.

Lets say you are using DB for session management and where HEAP tables are used for storing session info. in that case session management becomes really faster.

Make sure that PHP is module under your webserver and not running as CGI binary. This will also help you in having persitent connection with DB. Persitent connection is good idea if you are expecting lot of interaction with DB.

Lot of time ppl use templates which are stored in files. That might cause application to slow a bit. Use smarty which can cache a template too !!

Search Google you can find lots of pointers !!!
Yes, there are a few things. My main complaint is PHP programmers who retrieve a whole recordset from a database into PHP, and then go about using PHP array methods to search through the data. This should only be used as a last resort. Do all your data manipulation in SQL. Return only the data you need to show on at that particular point.. no more.

A small point, but if you are echoing (or printing) a string with no variables, use single quotes (so PHP doesn't even look for variables).

There is the compile-time option --enable-inline-optimization, which speeds PHP code.

Zend Optimizer is free, and can speed up complex scripts by approx. 50% (simple scripts cannot really be sped up much anyway). Zend Accelerator costs money, but maintains a compiled cache of your scripts in memory, which is about as fast as it gets.

Choose a good database management system, and choose good database design. This single area is probably the MOST critical point of failure in most PHP websites. (I recommend PostgreSQL, Sybase, Oracle, etc... over MySQL if you have any complexity in your system. MySQL is fine for simple websites, though.)

As JD says, HEAP tables in MySQL provide very fast access, but there are a couple hidden costs in MySQL HEAP tables: 1. No column can be greater than 255 characters, so if you are storing larger values, you must come up with a way of spanning multiple columns or records.
2. MySQL locks HEAP tables upon update, so if you have many sessions being written at the same time to the same table, you will hit a bottleneck fast. (one solution is to create a separate, temporary table for each user session. The other solution is to forget MySQL and compile PHP with 'mm' shared memory support, and use the mm storage handler for sessions, which accomplishes the same thing without using a database)

Really, though, PHP is already a high-performance platform. It is very well optimized for speed. The main thing to think about is scaleability, and your own architecture:


- Are you repeating any code blocks that you could put into a standard include?
- Are you loading more code than you need at any one time (conditional includes are a great thing)?
- Is your design modular, so that similar things are handled by the same function/class, etc...? (This way, you can optimize one function and your whole site will speed up.)


Scaleability is still a bit of an issue with PHP. There are a couple interesting add-ons available, such as http://www.php.net/msession (clustered session daemon), and SRM (www.vl-srm.net). Other than this, you have to think about how your application can be run on multiple servers, using standard clustering technology. The simplest method is to use something like msession, or to store sessions in a central database, and then use multiple front-end webservers, and one master database server (which can be clustered/replicated according to the DBMS's abilities.) This way, your front-end webservers only contain code, without needing to maintain ANY data persistence, since that is handled by the database, for application data AND for session data. ON the other side, the SRM system actually allows for 'back-end' PHP scripts, which run independently of an HTTP request on the server, but which provide business logic and database pooling to the PHP front ends. I'm sure there will be a lot more work done in both of these areas during the coming year.

Good luck

(P.S. Hello my friend JD ;-)... )

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now