PHP
  Home arrow PHP arrow Page 2 - Debugging and Performance
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

Debugging and Performance
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-11-22

    Table of Contents:
  • Debugging and Performance
  • Flattening if Statements
  • Splitting Single Commands Across Multiple Lines
  • One Equal, Two Equals, Three Equals
  • Testing for Resource Allocation

  • 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


    Debugging and Performance - Flattening if Statements


    (Page 2 of 5 )

    Consider the following snippet of code:

    if ($is_allocated) 
    {
    if ($has_been_mangled) 
    {
    if ($foo == 5) 
    {
    print "foo is 5";
    } 
    else 
    {
    print "You entered the wrong data!";
    }
    } 
    else 
    {
    return false;
    }
    }
    else 
    {
    return false;
    }

    As you can see, the many nested if statements here cause the code to look very busy and difficult to read. An easy way to improve the situation consists of "flattening" your if statements so that you can achieve the minimum level of indentation without compromising either the functionality of your code or its performance. The preceding script, for example, could be rewritten as follows:

    if (!$is_allocated) 
    {
    return false;
    }
    if (!$has_been_mangled) 
    {
    return false;
    }
    if ($foo == 5) 
    {
    print "foo is 5";
    } 
    else 
    {
    print "You entered the wrong data!";
    }

    This approach gives you a better structure with fewer levels of nesting so that your code is easier to understand. Note that the type of operations performed is pretty much the same as before—and the elimination of two else statements will make the code easier to parse for the interpreter.

    More PHP Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Zend PHP Certification," published by...
     

    Buy this book now. This article is excerpted from chapter 12 of Zend PHP Certification, written by George Schlossnagle et al (Sams; ISBN: 0672327090). Check it out today at your favorite bookstore. Buy this book now.

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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