PHP
  Home arrow PHP arrow Page 2 - Writing Clean and Efficient PHP Code
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Writing Clean and Efficient PHP Code
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 138
    2004-05-26

    Table of Contents:
  • Writing Clean and Efficient PHP Code
  • Loops
  • Objects
  • Code Reusability

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Writing Clean and Efficient PHP Code - Loops


    (Page 2 of 4 )

    One way or another, all pseudo code is eventually converted to machine code. This code will occupy some finite amount of memory. Some languages, such as Java and the .NET Framework go through some extra steps for portability sake before producing machine code by generating IL (Intermediary Language) code. Although it is not always a point of concern, the machine code produced can vary wildly in size based on the pseudo code a developer produces. In PHP, there are a few key ways to minimize the amount of memory and machine code overhead generated by your application. In a small personal site this may not matter, but in a performance-critical situation, the savings here can be just as valuable as the savings earned by writing efficient database queries. The best place to start the discussion is with loops and conditional statements.

    Case Statements - A case statement produces roughly 1/5th the amount of machine code as an if/else structure that completes the same task. Use case statements whenever possible! You can use a case statement when you are expecting an object to contain a particular value chosen from a range of known values. Case statements also allow you to provide a default fork for unknown values, which makes it possible to use a case statement in the majority of places where you may want to use an if/else statement. The difference in memory overhead between these two statements is fairly nominal, but the case statement is more efficient to some small degree.

    Loops - Loops vary more drastically than conditionals due to the nature of the operations they perform. PHP offers a number of loops, including some higher level looping methods based on the each() function. When deciding what type of loop to use, consider the operations taking place. Let's look at the following example, where we have a query that will return roughly 1,500 results.

    $r = mysql_query('SELECT * FROM someTable LIMIT 1500');
    //OPTION 1
    for ($i = 0; $i < mysql_num_rows($r); $i++) {
     print mysql_result($r, $i, 'ColA').mysql_result($r, $i, 'ColB');
    }

    // OPTION 2
    while (($row = mysql_fetch_assoc($r)) !== false) {
     print $row['ColA'].$row['ColB'];
    }

    The first option requires simply one scalar variable used as a counter for iteration, using a lightweight access method to read buffered data from a result handle. The second example uses an array constructor function to build an array and evaluate it's contents on each iteration, and then references the array two times per iteration. Despite what you may guess, "OPTION 2" runs significantly faster. It has lower memory and machine code overhead. The examples are somewhat poor in that they demonstrate poorly written code; database access handled in-line with iteration, and output mixed in. Ignore that. The building blocks are what contribute to the overall overhead of the application, and understanding that will help you make the right choices for your application. You may find it easier to use an associative array in iterations than a result handle cursor, but in a proper app, you would be performing this operation at the object level, and output would be handled elsewhere, driven by object oriented iteration of the values that were loaded into an object from our query.

    More PHP Articles
    More By David Fells


       · I have been scripting with php 4, and always thought it was ok do just do what i...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT