PHP中的错误处理

Posted on 2012年5月22日 19:58

1.php的错误等级

E_ERROR 致命的运行时错误,导致脚本的停止
E_WARNING 运行时警告
E_PARSE 语法解析消息
E_NOTICE 运行时注意消息
E_CORE_ERROR 类似E_ERROR,但不包括PHP核心错误
E_CORE_WARNING 类似E_WARNING,但不包括PHP核心错误警告
E_COMPILE_ERROR 致命的编译错误
E_COMPILE_WARNING 致命编译警告
E_USER_ERROR  用户导致的错误消息
E_USER_WARNING  用户导致的警告
E_USER_NOTICE 用户导致的注意消息
E_ALL 所有的错误、警告和注意
E_STRICT 关于PHP版本移植的兼容性和互操作性建议

2.常用错误指令

 在php的配置文件php.ini中display_errors指令控制是否显示错误信息,该指令可以由

ini_get ('display_errors')获得,而ini_set ('display_errors',1)动态设置配置文件中的改指令。

在上述的指令启动时可以设置 错误等级过滤,error_reporting指令,或者通过error_reporting函数修改。

例如:error_reporting(0);关闭错误报告

error_reporting (E_ALL);显示所有的错误报告

error_reporting(E_ALL & ~E_NOTICE);

除了上述两个指令还有下面的指令和错误有关:

 

display_startup_errors 是否显示PHP引擎在初始化时的遇到的错误信息 默认值Off
log_errors 确定日志文件的位置 Off
error_log 设置错误可以发送到syslog中 null
log_errors_max_len 设置日志的最大长度,以字节为单位,设置0表示最大长度 1024
ignore_repeated_errors 是否忽略同一文件、同一行发生的重复错误信息 Off
ignore_repeated_source 忽略不同文件中或同一文件中不同行上发生的重复错误 Off
track_errors 启动该指令会使PHP在$php_errormsg中存储最近发生的错误信息 Off

 

3.错误日志

一般发行网站后不显示错误信息,但是错误是在所难免的故可以使用错误日志。

error_reporting = E_ALL

display_errors = Off

log_errors = On

log_errors_max_len = 1024

error_log = /usr/local/error.log

在程序里面可以使用函数error_log函数发送消息,

第一个参数msg必选的,第二个是类型:

0表示发送到系统日志

1使用phpmail函数发送到邮箱,

2表示同过TCP发送到服务器

3表示将信息写到文件

PHP中的include包含路径问题

Posted on 2012年4月24日 15:36

在C语言和其他语言中include某个文件一般是以包含文件的该文件所在目录为当前目录,但是php则不然,它是以顶层的php文件所在的目录为当前目录,即a包含b,b如果包含c,此时b的当前目录为a所在目录,不是b所在目录。

首先我们来看php官方手册中对include的文件搜索原则的描述:

Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries , current working directory is /www/ , you includedinclude/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/ . If filename begins with ./ or ../ , it is looked only in the current working directory.

寻找包含文件的顺序先是在当前工作目录的相对的 include_path 下寻找,然后是当前运行脚本所在目录相对的 include_path 下寻找。例如 include_path 是 . ,当前工作目录是 /www/ ,脚本中要 include 一个 include/a.php 并且在该文件中有一句 include "b.php" ,则寻找 b.php 的顺序先是 /www/ ,然后是 /www/include/ 。如果文件名以 ./ 或者 ../ 开始,则只在当前工作目录相对的 include_path 下寻找。

和C语言一样php可以设置包含路径,set_include_path(), get_include_path();

下面给出一个例子,来说明php的include的使用。

a.php

<?php 

echo "current directory".getcwd()."</br>";
echo get_include_path()."</br>";
include ("include/c.php");
?>

include/c.php

<?php

echo "this is file: Include/c.php</br>";
echo "this file gcwd:".getcwd()."</br>";

include ("d.php");

?>

include/d.php

<?php

echo "this file include/d.php</br>";

echo "current directory:".getcwd()."</br>";


?>

d.php

<?php

echo "this top d.php</br>";

?>

如果d.php 和include/d.php 同时存在,

运行a.php,当前目录为网站根目录,a包含c,根据查找规则,当前目录相对的include_path优先,则查找到c。

c包含d.php文件,同样的当前目录优先,但注意此时的当前目录为a所在的目录,故查找到的d文件,为根目录下的d.php文件,如果根目录下的d文件不存在,才会查找c文件所在的目录的include_path,此时才会包含include/d.php文件。

运行效果如下:

current directory/var/www/test
.:/usr/share/php:/usr/share/pear
this is file: Include/c.php
this file gcwd:/var/www/test
this top d.php

如果根目录下的d文件不存在:

current directory/var/www/test
.:/usr/share/php:/usr/share/pear
this is file: Include/c.php
this file gcwd:/var/www/test
this file include/d.php
current directory:/var/www/test