HTML
  Home arrow HTML arrow Page 3 - Combating Coding Errors
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
HTML

Combating Coding Errors
By: Steve Adcock
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2002-11-28

    Table of Contents:
  • Combating Coding Errors
  • The 4 types of Programming Errors
  • PHP Specific Error Messages and Reporting
  • Conclusion

  • 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


    Combating Coding Errors - PHP Specific Error Messages and Reporting


    (Page 3 of 4 )

    PHP error messages, in most cases, are fairly user-friendly. Let's take a look at a few error messages and the code that produced them.

    Code:
    includ("file.txt");

    Error:
    Fatal error: Call to undefined function: includ() in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12

    Comment:
    Since includ() is an undefined function, a fatal error (or a semantic error) occurs and execution stops. As you can see, PHP catches the error and details the problem (undefined function: includ()) and even provides the line that the error occurred on.

    Since fatal errors are semantic errors, the script does execute up until the error. However, since PHP needs to compile and execute each line of code to continue, it will immediately stop upon encountering a fatal error.

    Code:
    $x = 1
    echo $x;


    Error:
    Parse error: parse error in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 13

    Comment:
    Using our syntax error example from above, the compiler did indeed catch the missing semicolon in our first statement ($x = 1). Although the error does not specifically state "missing semicolon", a parse error tells the programmer a syntax error has occurred, which allows the programmer to hone his or her debugging skills to a specific type of error.

    Remember that since parse errors are syntax errors, no execution was performed on the script. Once the semicolon is added to the first line of code, the script will execute successfully and as intended.

    Code:
    // $x was never declared
    echo $x;


    Error:
    Warning: Undefined variable: x in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12

    Comment:
    As you can see, this is a warning, not an error (in this case, the warning message is also known as a notice). Warnings occur when PHP determines something is wrong within the script but is not serious enough to cease execution of the script.

    It is essential to understand that warnings do not prevent the execution of the script. When a warning is encountered, a warning message will be placed within the output of a script where the line of code was read. PHP will continue executing the remainder of the script until the end of line is reached.

    For example, the following code:

    $i = 1;
    echo $x;
    echo $i;


    Will produce this output:

    Warning: Undefined variable: x in c:\program files\apache group\apache\htdocs\testserver\operator.php on line 12
    1

    Notice the 1 at the end of the output. After PHP encountered the warning on the second executable line ($x not defined), it correctly interpreted and executed the third line.

    Using error_reporting()
    The three examples above detail the types of error messages present within PHP. In an effort to give the programmer absolute control, PHP allows the programmer the ability to select which errors and warnings to display. If the programmer has access to the PHP.INI file on the host machine, errors can be globally modified. If, however, the programmer wishes to modify output relative to specific files, the error_reporting() function can be used instead.

    The error_reporting() function takes one parameter that signifies the type of error/warning output to support and is usually written at the top of any PHP document. The error reporting parameters are as follows:
    • 0: No error reporting
    • 1: Fatal errors
    • 2: Warnings
    • 4: Parse errors
    • 8: Notices
    So, if you'd rather only display fatal errors, then the function code looks like this:

    error_reporting(1);

    What if the programmer wants fatal errors and parse errors together? Simply add the two values together.

    error_reporting(5); // 1 + 4 = 5

    More HTML Articles
    More By Steve Adcock


     

    HTML ARTICLES

    - Comparing Browser Response to Active Client ...
    - Testing Browser Response to Active Client Pa...
    - Active Client Pages: Completing the Code for...
    - ACP and Browsers: Setting up an Example
    - How Browsers Respond to Active Client Pages
    - Completing a Tree with Active Client Pages
    - HTML Form Verification and ACP
    - Building an ACP Tree
    - Completing an ACP 3D HTML Table Image Gallery
    - Building an ACP 3D HTML Table Image Gallery
    - A Multiple Page Image Gallery with Active Cl...
    - Building an Image Gallery with Active Client...
    - Concluding a Menu for All Browsers
    - A Vertical Menu for All Browsers
    - Downloading Long HTML Pages with ACP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek