表A
<?php
// display warnings and errors
error_reporting(E_WARNING | E_ERROR);
// this will generate a notice, which will never be displayed
echo $undefinedVar;
// this will generate a fatal error, which will be displayed
callUndefFunc();
?>
<?php
// turn off error display
// no errors will be displayed
error_reporting(0);
// this will generate a notice
echo $undefinedVar;
// this will generate a fatal error
callUndefFunc();
?>
表C中的代码将所有错误信息甚至简单的注意事项都显示出来:
表C
<?php
// all errors will be displayed
error_reporting(E_ALL);
// this will generate a notice
echo $undefinedVar;
// this will generate a fatal error
callUndefFunc();
?>
表D
<?php
// no errors will be displayed
error_reporting(0);
// start a task
echo "Starting task...";
// call an undefined function
// a fatal error occurs during task processing
callMe();
// end the task
echo "Successfully completed task...";
?>
<?php
// define custom handler
set_error_handler('myHandler');
// custom handler code
function myHandler($code, $msg, $file, $line) {
echo "Just so you know, something went wrong at line $line of your script $file. The system says that the error code was $code, and the reason for the error was: $msg. Sorry about this!";
}
// generate a notice
echo $undefVar;
?>
当运行此脚本的时候,会出现下面的信息:
Just so you know, something went wrong at line 11 of your /dev/error1.php. The system says that the error code was 8, and the reason for the error was: Undefined variable: undefVar. Sorry about this!