| only for RuBoard - do not distribute or recompile |
If you come from a non-Web programming background, optimization can seem really important. When using PHP, most of the time that a user waits for a Web application comes from connection and download times. Optimization of your code will have little effect on these times.
There are, however, a few simple optimizations that you can do that will make a difference. Many of these relate to applications that integrate a database such as MySQL with your PHP code, and some are listed as follows:
Reduce database connections. Connecting to a database is often the slowest part of any script. You can get around this by using persistent connections.
Speed up database queries. Reduce the number of queries that you make, and make sure that they are optimized. With a complex (and therefore slow) query, there is usually more than one way to skin a cat. Run your queries from the database's command-line interface and experiment with different approaches to speed things up. In MySQL, you can use the EXPLAIN statement to see where a query might be going astray. (Use of this statement is discussed in Chapter 11, "Advanced MySQL." ) In general, the principle is to minimize joins and maximize use of indexes.
Minimize generation of static content from PHP. If every piece of HTML you produce comes from echo or print(), it will take a good deal longer. (This is one of the arguments for shifting toward separate logic and content as described previously.) This also applies to generating image buttons dynamically—you might want to use PHP to generate the buttons once and then re-use them as required. If you are generating purely static pages from functions or templates every time a page loads, consider running the functions or using the templates once and saving the result.
Use string functions instead of regular expressions where possible. They are faster.
Use single-quoted strings instead of double-quoted strings where possible. PHP evaluates double-quoted strings, looking for variables to replace. Single-quoted strings are not evaluated. On the other hand, if it's in single quotes, it's probably static content. Review what you are doing and see if you can get rid of the string altogether by turning it into static HTML.
Zend Technologies own the (Open Source) PHP scripting engine for PHP 4 onward. This is a good deal faster (quoted as being two to ten times) than the version 3 engine, so if you haven't upgraded yet, do yourself a favor and install version 4.
In addition to the basic engine, you can also download the Zend Optimizer. This is a multi-pass optimizer that will optimize your code for you, and can increase the speed at which your scripts run from 40% to 100%. You will need PHP 4.0.2 or upward to run the optimizer. Although closed source, it is free for download from Zend's site:
This add-on works by optimizing the code produced by the runtime compilation of your script. Upcoming Zend products include the Zend Cache, which will compile your PHP scripts and cache them so they are only recompiled when they are rewritten. This should offer another improvement in performance.
| only for RuBoard - do not distribute or recompile |