Publishers of technology books, eBooks, and videos for creative people

Home > Articles > HTML/XHTML

HTML Forms and PHP

This chapter is from the book 

Error Reporting

The second of the three PHP configuration issues you should be aware of is error reporting. There are eight types of errors in PHP plus three user-defined errors (which aren't covered here). Table 3.1 lists the four most important error types, along with a description and example.

You can determine what errors PHP reports on using the error_reporting() function. The function takes either a number or a constant (a nonquoted string with a predetermined meaning) to adjust the levels. For example:

error_reporting (0); // Turn off entirely.
error_reporting (E_ALL); // Report
β†’ everything.
error_reporting (E_ALL & ~E_NOTICE);
β†’ // Don't show notices.

The last example states that you want to see all error messages except notices (the & ~ means and not). You'll apply this setting to the handle_form.php page to get rid of those pesky error messages (Figure 3.12).

Script 3.6. You adjust PHP's level of error reporting to give you more or less feedback.

1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
    1.0 Transitional//EN"
2   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
    transitional.dtd">
3   <html xmlns="http://www.w3.org/1999/xhtml">
4   <head>
5        <meta http-equiv="content-type"
         content="text/html; charset=
         iso-8859-1" />
6        <title>Your Feedback</title>
7   </head>
8   <body>
9   <?php // Script 3.6 - handle_form.php
    (third version after Scripts 3.4 and 3.5)
10
11  ini_set ('display_errors', 1); // Let me
    learn from my mistakes.
12   error_reporting (E_ALL & ~ E_NOTICE); //
    Don't show notices.
13
14  // This page receives the data from
    feedback.html.
15  // It will recieve: title, name, email,
    response, comments, and submit.
16
17  print "Thank you $title $name for your
    comments. <br />";
18  print "You stated that you found this
    example to be $response and added:
    $comments";
19
20  ?>
21  </body>
22  </html>

Table 3.1. The four most common error types in PHP.

PHP Error Types

TYPE

DESCRIPTION

EXAMPLE

Notice

Nonfatal error that may or may not be indicative of a problem

Referring to a variable that has no value

Warning

Nonfatal error that is most likely problematic

Passing the wrong type of parameter to a function

Parse error

Fatal error caused by a syntactical mistake

Omission of a semicolon or an imbalance of quotation marks, braces, or parentheses

Error

A general fatal error

Memory allocation problem

To adjust error reporting:

  1. Open handle_form.php in your text editor (Script 3.5).
  2. After the ini_set() line, enter the following (Script 3.6):
    error_reporting(E_ALL&~E_NOTICE);
  3. Save the file as handle_form.php.
  4. Upload the file to your Web server and test it in your Web browser (Figures 3.13 and 3.14).

    Figure 3.13

    Figure 3.13 Trying the form one more time...

    Figure 3.14

    Figure 3.14 ...and now the notices are gone.

    At this point, you should either see a successful handling of the form (Figure 3.9) or be missing the variable values but not see the error messages (Figure 3.14). The next section of this chapter will address the missing variable values.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.