默认的情况下,安装配置好PHP和Apache服务器之后,在浏览PHP页面的情况下,如果出现程序错误或异常,是不会自动报错的的,也不能显示错误或异常信息。而且,显示的只是“HTTP 500 - 内部服务器错误 ”,没有任何其它的信息输出,这样不便于PHP程序的调试。
其实,是出于安全考虑,PHP默认情况下关闭了对出现异常信息的显示,只需要在PHP的初始化文件php.ini中进行设置即可。
(注意:
我的PHP版本和Apache的具体配置信息为:
PHP Version 5.2.6
apache2handler
)
配置PHP的php.ini文件在目录C:\WINDOWS下面,打开php.ini进行设置。
主要设置的就是开启报错功能,同时可以设置报错级别,找到:
display_errors = Off
error_reporting = E_ALL
他们就是对应于报错开关、报错级别的设置,上面这两个都是默认的设置。
开启设置,只需要经Off改为On即可:
display_errors = On
关于设置报错级别,可以参考注释部分的提示,如下所示:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; error_reporting is a bit-field. Or each number up to get desired error
; reporting level
; E_ALL - All errors and warnings (doesn't include E_STRICT)
; E_ERROR - fatal run-time errors
; E_RECOVERABLE_ERROR - almost fatal run-time errors
; E_WARNING - run-time warnings (non-fatal errors)
; E_PARSE - compile-time parse errors
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
; E_STRICT - run-time notices, enable to have PHP suggest changes
; to your code which will ensure the best interoperability
; and forward compatibility of your code
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup
; E_COMPILE_ERROR - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR - user-generated error message
; E_USER_WARNING - user-generated warning message
; E_USER_NOTICE - user-generated notice message
;
; Examples:
;
; - Show all errors, except for notices and coding standards warnings
;
;error_reporting = E_ALL & ~E_NOTICE
;
; - Show all errors, except for notices
;
;error_reporting = E_ALL & ~E_NOTICE | E_STRICT
;
; - Show only errors
;
;error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
;
; - Show all errors, except coding standards warnings
设置完成以后,测试PHP程序,需要重新启动Apache服务器。测试一下:
上面的错误其实是变量的定义语法错误,我写成了:
var sno;
var school;
var grade;
var score;
显然,变量应该定义为:
var $sno;
var $school;
var $grade;
var $score;