Home arrow HTML arrow Page 3 - Combating Coding Errors
HTML

Combating Coding Errors


Coding errors can get even the best of us down, but they don't have to be seen as the "be all, end all" of coding, as Steve shows us in this article.

Author Info:
By: Steve Adcock
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
November 28, 2002
TABLE OF CONTENTS:
  1. · Combating Coding Errors
  2. · The 4 types of Programming Errors
  3. · PHP Specific Error Messages and Reporting
  4. · Conclusion

print this article
SEARCH DEVARTICLES

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
blog comments powered by Disqus
HTML ARTICLES

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

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