PHP
  Home arrow PHP arrow Page 5 - 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 - Testing for Resource Allocation


    (Page 5 of 5 )

    One of the most common mistakes that causes code to become unreliable consists of using external resources without ensuring that they are available. For example, look at the following code:

    $res = mysql_query("select foo from bar");
    while ($row = mysql_fetch_array($res)) 
    {
    print $row['foo']."<br>";
    }

    See what's wrong? The author doesn't test for the query's failure before moving on to perform other tasks that use the resource returned by mysql_query(). The query could fail for a number of reasons, even though it is syntactically correct—for example, the server might be unavailable, or there could be a network interruption. What's worse in this particular case, the MySQL extension does not cause a fatal error if a query cannot be executed. Therefore, the script moves on, and a cascade of additional problems could be caused by this initial blunder.

    If, on the other end, error conditions are properly tested for, this issue doesn't even present itself:

    if (!$res = mysql_query("select foo from bar")) 
    {
    /**
    * no valid result, log/print error,
    mysql_error() will tell you */ } else { while ($row = mysql_fetch_array($res)) { print $row['foo']."<br>"; } }

    It's undoubtedly hard to write an if statement every time you execute a query—but also necessary if you are serious about error management. To make things a bit easier on yourself (and your entire team), you could adopt one of the many abstraction layers available or write one yourself. This way, the actual error management can be performed in a centralized location (the abstraction layer), and you won't have to write too much code.

    It's important to keep in mind that this process is required whenever you interact with an external resource, be it a database, a file, or a network connection.

    Starting with PHP 5, you can use other error-control structures known as exceptions. However, remember that these are not available in PHP 4 and, therefore, cannot be used to solve a problem that appears in the exam.

    Ternary Operators and if Statements

    if statements are necessary control structures for all but the simplest of PHP scripts. As a result, sometimes they will tend to be very complex, even if you nest them on various levels.

    Luckily, the ternary conditional operator that you saw in Chapter 1 can be used to simplify the use of if statements by embedding them directly in a larger expression. For example, consider the following snippet of code:

    function is_my_country($country) 
    {
    if (strlen($country) == 3) 
    {
    return 1;
    } 
    else 
    {
    return 0;
    }
    }

    It could also be written as

    function is_my_country($country) {
    return (strlen($country)==3) ? 1 : 0;
    }

    As you can see, the function is much shorter than the if statement in the preceding example. This can be very valuable if you're dealing with a complex piece of code such as the following:

    $db->query(sprintf("insert into foo(f1,f2,f3)
    values('%s','%s','%s')", (isset($_SESSION['foobar'])) ? 'yes' : 'no', (isset($_POST['myfoo']) && $_POST['myfoo']!='')
    ? $_POST['myfoo'] : 'no', 'foo'));

    A call such as the preceding one would have been a lot more complex if it had been written using traditional if statements—not to mention that you would have needed either a number of new variables to hold the information, or a different set of function calls for each possible scenario.

    Please check back next week for the conclusion of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 6 hosted by Hostway
    Stay green...Green IT