13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

564 Chapter 26 Debugging<br />

By default, when a fatal error occurs, <strong>PHP</strong> outputs<br />

<br />

Error Type: error message in path/file.php<br />

on line lineNumber<br />

<strong>and</strong> stops executing the script. For nonfatal errors, the same text is output, but execution<br />

is allowed to continue.<br />

This HTML output makes the error st<strong>and</strong> out but looks poor.The style of the error<br />

message is unlikely to fit the rest of the site’s look. It might also result in some users seeing<br />

no output at all if the page’s content is being displayed within a table <strong>and</strong> their<br />

browser is fussy about valid HTML. HTML that opens but does not close table elements,<br />

such as<br />

<br />

<br />

<br />

Error Type: error message in path/file.php<br />

on line lineNumber<br />

is rendered as a blank screen by some browsers.<br />

You do not have to keep <strong>PHP</strong>’s default error h<strong>and</strong>ling behavior or even use the same<br />

settings for all files.To change the error reporting level for the current script, you can call<br />

the function error_reporting().<br />

Passing an error report constant, or a combination of them, sets the level in the same<br />

way that the similar directive in php.ini does.The function returns the previous error<br />

reporting level. A common way to use the function is like this:<br />

// turn off error reporting<br />

$old_level = error_reporting(0);<br />

// here, put code that will generate warnings<br />

// turn error reporting back on<br />

error_reporting($old_level);<br />

This code snippet turns off error reporting, allowing you to execute some code that is<br />

likely to generate warnings that you do not want to see.<br />

Turning off error reporting permanently is a bad idea because it makes finding your<br />

coding errors <strong>and</strong> fixing them more difficult.<br />

Triggering Your Own Errors<br />

The function trigger_error() can be used to trigger your own errors. Errors created<br />

in this way are h<strong>and</strong>led in the same way as regular <strong>PHP</strong> errors.<br />

The function requires an error message <strong>and</strong> can optionally be given an error type.<br />

The error type needs to be one of E_USER_ERROR, E_USER_WARNING, or E_USER_NOTICE.<br />

If you do not specify a type, the default is E_USER_NOTICE.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!