Writing Efficient PHP
… PHP. Presented by developerWorks, your source for great tutorials … This tutorial targets the PHP developer who already understands PHP, but wants to …
More PDF Content
Writing efficient PHP Writing efficient PHP Presented by developerWorks, your source for great tutorials ibm.com/developerWorks Table of Contents If you’re viewing this document online, you can click any of the topics below to link directly to that section.
1. About this tutorial ……………………… 2
2. Writing efficient code …………………. 5
3. Tweaking PHP code …………………… 14
4. Optimizing database queries ……….. 19
5. Other performance enhancements … 23
6. Summary ………………………………. 27
Writing efficient PHP
Page 1
of 28 Section 1. About this tutorial Should I take this tutorial? This tutorial targets the PHP developer who already understands PHP, but wants towrite more efficient PHP code or to improve the performance of existing PHPapplications. It is not intended as an introduction to PHP — plenty of other resourcesare available for that. Rather, it assumes that you already have an installation of PHPavailable and are familiar with the basic PHP syntax. In this tutorial, you will learn ways to improve the performance of your PHP code. Thetutorial is in four main parts: General coding considerations that can be often applied to any language, but areillustrated with specific PHP examples (see Writing efficient code on page 5 ). Nuances of the PHP language and how you can use them to further improve theefficiency of your PHP code (see Tweaking PHP code on page 14 ). Improving the performance of your SQL queries since, for larger applications, PHPoften is used in conjunction with a database (see Optimizing database queries on page 19 ). Making your PHP code run faster and references for further reading (see Other performance enhancements on page 23 ). Background and motivation For a recent consulting engagement, I reviewed a Web application nearing completion.The application was written in PHP, and had some serious performance problems.After a brief look at the code, I decided to search the Internet for any articles orinformation on tuning the performance of PHP code. To my surprise, I found very littleinformation available. The PHP manual (see Resources on page 27 ) suggested simply throwing in more hardware. In my client’s case, this was not always an option. Manysystems were already in the field, and this application was a product upgrade. It wasnot feasible to upgrade every system in the field for this relatively minor productupgrade. I had to devise my own tests to find ways of improving PHP. Thanks to IBM’sdeveloperWorks, I can share these findings with a much broader audience. ibm.com/developerWorks Presented by developerWorks, your source for great tutorials
Page 2
of 28 Writing efficient PHP Tools You can read through this tutorial from beginning to end and pick up a wide variety oftips and advice on how to improve your PHP code without having anything in particularinstalled. For the most value, you may want to construct and run some of your own test cases.To do this, you will need a working installation of PHP on your machine. You shouldprobably have the following: Any OS supported by PHP (http://www.php.net/manual/en/installation.php). PHP (http://www.php.net/) A Web server under which you run PHP is optional. Alternatively, you can run PHPfrom the command line. Getting help For technical questions about PHP, visit the php.net Web site. Read the PHP manual and pay particular attention to the user-contributed notes at the bottom of each manualpage. They often contain valuable lessons learned that may help answer your question. Refer to Resources on page 27 at the end of this tutorial for these and other useful links. About the author Steven Gould is a principal consultant with a large, international IT consulting and professional services company. Based in Dallas, he is a systems architect and seniordeveloper, focusing primarily on Java and C++ development under Windows, Linux,and various UNIX platforms. Having worked on a variety of Internet and intranetapplications, he also is very well versed in many other Internet-related technologies. Tip: You also might be interested in the PHP Quick Reference Card available for freefrom the author’s Web site . Acknowledgements Presented by developerWorks, your source for great tutorials ibm.com/developerWorks Writing efficient PHP
Processing your request, Please wait....