Thursday, March 15, 2007

Rasmus Lerdorf, the inventor of PHP, recently headlined the PHPCon East 2003 show at the Park Central Hotel in New York City. Along with a keynote address, Lerdorf ran a session called “tips & tricks.” The following tip is taken from that session. To get additional tips from that session, please visit http://lerdorf.com/nytips.pdf.

5 tips for optimizing PHP

1) Don't use a regex if you don't have to, instead, use PHP’s string manipulation functions. For example:

BAD:
GOOD:
BAD:
GOOD:

2) No references. Usually it is not faster to use references because of PHP's copy-on-write nature.

3) Use Persistent Database connections. Some databases are slower than others at establishing new connections, and the slower the database, the more of an impact using persistent connections will have. Keep in mind, however, that persistent connections will use resources even when not in use.

4) Take a look at MySQL unbuffered query. If you’re using MySQL, check out mysql_unbuffered_query(). You use it exactly like you would mysql_query(), the difference is that instead of waiting for the entire query to finish and storing the result in the client API, an unbuffered query makes results available as soon as possible and they are not allocated in the client API. You get access to your data quicker and use a lot less memory. But you can't use mysql_num_rows() on the result resource and it is likely to be slightly slower for small selects.

5) Keep it simple. Don't over-architect things. If your solution seems complex to you, there is probably a simpler and more obvious approach.

Copyright © 2005 Ziff Davis Media Inc. All Rights Reserved. Originally appearing in Publish.

No comments: