Home arrow PHP arrow Page 3 - Making Sense Of PHP Errors
PHP

Making Sense Of PHP Errors


PHP's interpreter has been known to split confusing error messages. In this article we will learn what the most common ones mean and also how to combat them.

Author Info:
By: Elan Bechor
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
September 26, 2002
TABLE OF CONTENTS:
  1. · Making Sense Of PHP Errors
  2. · The Way of The Interpreter
  3. · Other Common Errors
  4. · Conclusion

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials