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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 139
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT