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

Debugging and Performance
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    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:
      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


    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.



     
     
    >>> More PHP Articles          >>> More By Sams Publishing
     

       

    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 3 Hosted by Hostway
    Stay green...Green IT