PHP
  Home arrow PHP arrow Page 3 - 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 
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 / 14
    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 - Other Common Errors


    (Page 3 of 4 )

    One of the most common errors I see -- and quite possibly the most confusing -- occurs when you don’t end a function or loop with a curly bracket ( } ). This code, for example:

    function UselessFunction() {

    for($i < 0; $i < 10; $i++){
    }


    Produces the following error:

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

    Because UselessFunction never ended it's function with a closing bracket (}), the PHP interpreter kept on looking for the closing bracket until the end of the file. Since it didn't find one, it reported the error at the end of the file.

    While the mistake seems very obvious with properly indented code, without proper indentation, it eventually becomes nearly impossible to see what was forgotten. So remember, always indent your code… the tab key is your friend! It also makes things easier on future developers when they're trying to hack through your code to modify it.

    MySQL Errors
    Another annoying error is the most common MySQL error, which often leaves PHP newbies scratching their heads:

    Warning: Supplied argument is not a valid MySQL result resource in...

    The line it reports will often look something like this:

    while($row = mysql_fetch_array($result)) {

    The argument, $result, is not a valid resource. In English, that means that since your query failed, it cannot process mysql_fetch_array. Either the query had invalid syntax (you should copy-paste your query into the MySQL console window to test it), or a connection failed to your database (in which case you should double-check your username, password, etc).

    Preventing Errors From Occuring
    There are a few steps that a diligent PHP coder can take to eliminate some of these parse errors from happening in the first place:
    • After the end of a statement, don't even think about adding a semicolon -- It should be second nature.
    • Always indent your code, which allows you to see if you forgot to place a curly bracket after an if call, or the end of a function, etc.
    • Use an editor (such as HTML-Kit) that highlights your syntax. Within the confides of this editor you will be able to see if you didn't escape a quote, missed a semi-colon, etc.

    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-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT