Specifications

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 databases 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 argu-
ments for shifting toward separate logic and content as described previously.) This also
applies to generating image buttons dynamicallyyou might want to use PHP to gener-
ate 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 func-
tions 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 its in single quotes, its 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.
Using Zend Products
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 havent
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 Zends site:
http://www.zend.com
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.
Using PHP and MySQL for Large Projects
C
HAPTER 22
22
USING PHP AND
MYSQL FOR
LARGE
PROJECTS
473
28 7842 CH22 3/6/01 3:37 PM Page 473