PHP
  Home arrow PHP arrow Page 2 - Making Sense Of PHP 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 
Sun Developer Network 
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

Making Sense Of PHP Errors
By: Elan Bechor
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2002-09-26

    Table of Contents:
  • Making Sense Of PHP Errors
  • The Way of The Interpreter
  • Other Common Errors
  • 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


    Making Sense Of PHP Errors - The Way of The Interpreter


    (Page 2 of 4 )

    To find out why the interpreter reports an error on a certain line, first one must learn how the interpreter parses PHP code. I wont go into detail in this article, however I will discuss the simple concepts that cause errors more often than not.

    Variable Declaration
    When declaring a variable in a statement such as:

    $variable = 'value';

    The interpreter first evaluates the right hand side of the statement (all code to the right of the equals sign) first. In programming books this is referred to the RHS (right hand side) of the statement. It is this part of the statement that often causes errors. If improper syntax is used, a parse error occurs.

    Parse Errors
    Parse error: parse error, unexpected T_WHILE in c:\program files\apache group\apache\htdocs\script.php on line 19

    Ugh. Parse errors, one after another, keep on showing up every time you fix the previous error. Since PHP stops executing the script after the first parse error, they are especially annoying to debug.

    Additionally, parse errors are horribly un-informative, rarely ever reporting the line number where the error was picked up. The reason for this is that when you make a mistake, it might seem like valid syntax to the interpreter for several lines until it encounters invalid syntax, most likely a reserved word used in an expression, such as:

    while = 10; // Bad –- while is a reserved word and can't be assigned a value

    Reserved words are words like for, while, function, and if that PHP uses to evaluate your code. You can not name variables after these reserved words and if you do then PHP will spit more errors than you can chew.

    An example might be helpful at this point. Take a look at the PHP code shown below:

    <?php

    $b = "somevalue"

    if($b == "somevalue"){
    print "Hello world!";
    }

    ?>


    The mistake is on the "$b =" line (no semicolon ending the statement), so the error should be "parse error: expected ; on line 3", right? Not according to the interpreter.

    Parse error: parse error, unexpected T_IF in c:\program files\apache group\apache\htdocs\ereg2.php on line 4

    On line 4, the if() syntax is perfect. So what is the interpreter getting confused from? The clue is the "unexpected T_IF" part. When an "unexpected T_???" error occurs, it means that the interpreter encountered its respective reserved word where it shouldn't be. T_IF for if(), T_WHILE for while(), T_FOR for for(), etc.

    Luckily, there are a few easy causes for these errors:
    1. The statement wasn't ended with a semicolon (;), like in the example above.
    2. A quote was not properly escaped (' instead of \' or " instead of \") in a string.

    More PHP Articles
    More By Elan Bechor


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






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