Home > Article > Backend Development > Introduction to PHP file loading and error handling
This article mainly introduces the introduction of PHP file loading and error handling. It has certain reference value. Now I share it with you. Friends in need can refer to it.
Knowledge points:
1-File loading
2-Error handling
include, require, include_once, require_once
2) The usage forms are the same.include "Full path of the file to be loaded"; or include("Full path of the file to be loaded"); For example: include "d:/index.php";
3)
Files that can be loaded: php or html filePath
: Locate the location of a loaded file relative to the location of the current web page file ./ : Represents the current location, that is The location (directory) where the current web page file is located;
../ : Represents the upper level location, that is, the upper level location (directory) of the location where the current web page file is located;
2)
Local absolute path; For example: include "d:/demo/index.php";
Network absolute path; For example: include "http://www.baidu .com/demo/index.php"
3) Do not write the path, directly the file name
The essence is to load the specified file name
in the current directory For example: include " index.php"; //PHP language engine will find the file in the current web page directory
File loading and execution processstep 1: 从include|require 语句处退出PHP脚本模式(进入html代码模式) step 2:载入include|require 语句所设定的文件中的代码,并执行 step 3:退出html模式重新进入php脚本模式,继续之后的代码The difference between include, include_once, require, require_once
Include or include_once contains When the file fails to be imported (that is, the file is not found), "
prompt error" is reported, and thencontinues to execute the subsequent code; When require or require_once fails to load a file, an error will be reported and
terminate execution immediately. Generally, require is used in a program when subsequent code depends on the loaded file.
2) The difference between inlcude and include_once, or require and require_once
The file loaded by include or require
does not determine whether it is repeated, as long as there is an include or require statement, it will Will be loaded once --- which means it may be loaded repeatedly. Files loaded by include_once or require_once will have an internal judgment mechanism to determine whether the "previous code" has been loaded. If it has been loaded, it will no longer be loaded
. Ru: If there are the same advertisements on both sides in the webpage, you use include
2) If there is a return in the loaded file, the content of the file after the return will not be loaded -- Terminate loading
Can be used for: The loaded file returns a data to the loading file
Error handling
Error classificationIf there is an error in the syntax, an error will be reported immediately and the program will not be executed
2) Runtime error
In the program syntax After passing the check, start running the program, and encounter errors during the process
Three common types of errors: prompt errors, warning errors, fatal errors
3) Logic errors
The program itself can be executed normally without any errors. But it's not the desired result.
1) 技术层面的错误分级: PHP语言中,将各种错误进行了不同级别的分类归纳
每一级别的错误,都有一个“代号”,这个代号是系统内部的一个“常量”
2)系统常见错误
E_ERROR: 致命错误
E_WARNING: 警告性错误
E_NOTICE: 提示性错误
3) 用户自定义错误
E_USER_ERROR: 自定义致命错误
E_USER_WARNING: 自定义警告性错误
E_USER_NOTICE: 自定义提示性错误
4) 其他
E_STRICT: 严谨性语法检查错误
E_ALL: 代表所有错误
详细参考手册: 函数参考》影响PHP行为的扩展》错误处理和日志记录》预定义常量
1 <?php 2 function getBinStr($e) { 3 $s = decbin($e); //这是一个二进制数字字符串 4 /* 5 str_pad($str1,长度n,$str2,位置w)函数: 6 将字符串$str1,用字符串$str2填充到指定的长度n, 7 可以指定填充的位置w,左边填充还是右边填充 8 */ 9 $s1 = str_pad($s,16,"0",STR_PAD_LEFT); 10 return $s1; 11 } 12 echo "<pre class="brush:php;toolbar:false">"; 13 echo "E_EEROR = ".E_ERROR . "\t\t其对应二进制值为: " . getBinStr(E_ERROR); //1 14 echo "<br />E_WARNING = ".E_WARNING. "\t\t其对应二进制值为: " . getBinStr(E_WARNING); //2 15 echo "<br />E_NOTICE = ".E_NOTICE. "\t\t其对应二进制值为: " . getBinStr(E_NOTICE); //8 16 echo "<br />E_USER_NOTICE = ".E_USER_NOTICE. "\t\t其对应二进制值为: " . getBinStr(E_USER_NOTICE); //1024 17 echo "<br />E_ALL = ".E_ALL. "\t\t其对应二进制值为: " . getBinStr(E_ALL); //32767 18 echo ""; 19 ?>
查看错误分级对应的二进制数测试
1) 方式1: 系统触发
典型错误3种:
E_NOTICE: 提示性错误: 会输出错误提示,并继续执行后续代码;如:使用不存在的变量或常量
E_WARNING: 警告性错误: 会输出错误提示,并继续执行后续代码; 如: include载入一个不存在的文件:
E_ERROR: 致命错误: 导致程序无法执行后续语句; 如: 一个不存在的函数!!
2) 方式2: 自定义触发
1) 概念: 当处理某些数据时,数据本身没有错误,但根据具体应用(业务)的需要,会要求数据满足某种条件,而该数据并不满足的时候,可以在程序中“主动”去触发(创建)一个错误,以表明该数据的“非法性”。
2) 语法形式: trigger_error(“错误提示信息内容”,3种用户错误代号之一);
如果触发了用户的致命错误(E_USER_ERROR),会终止程序的后续执行
1) 错误报告: 显示在网页上的错误提示内容
2) 是否显示错误报告 ? display_errors
方式1:全局设置
修改配置文件php.ini 的配置项 display_errors = On; //表示显示 如果是Off表示关闭
方式2: 局部设置
在php脚本文件中使用函数ini_set()来对它进行设置; 如 ini_set("display_errors",0); //不显示错误报告
该方式设置要优先于全局设置
3) 显示哪些级别的错误报告? error_reporting
前提: display_errorrs=On;
方式1: 全局设置
修改配置文件php.ini 的配置项error_reporting, 如 : error_reporting = E_NOTICE | E_WARNING | E_ERROR
方式2: 局部设置
在php脚本文件中使用函数ini_set()来对它进行设置 , 如init_set(“error_reporting”,E_NOTICE | E_WARNING | E_ERROR),
1)是否记录错误日志? log_errors
方式1: 全局设置
修改配置文件php.ini 的配置项log_errors, 如 : log_errors= On; //记录错误日志
方式2: 局部设置
在php脚本文件中使用函数ini_set()来对它进行设置 , 如init_set(“log_errors”,1);//记录错误日志
获取php.ini配置项: ini_get("配置项"); //获取php.ini的指定配置项值
2)记录到哪里? error_log
可以指定位置或记录到系统日志中
指定位置文件中:直接使用文件名,系统会自动在文件夹下都建立该文件名,并用其记录该文件夹下的所有网页文件发生的错误信息。
ini_set("error_log", "myError.txt"); //如果有错误,将记录在myError.txt文件中
写入系统日志中: ini_set("error_log", "syslog");//所有错误日志记录到系统 日志文件 中
1)错误处理器: 发生错误,用来处理该错误的一种方法。实质就是一个函数
2) 自定义错误处理器: 将原本有系统处理错误变为开发者自定义对错误显示和记录处理
3) 分2步:
step 1: 设定用于处理错误的函数 set_error_handler("函数名"); 如 set_error_handler('myError');
step 2: 声明定义处理错误的函数。 如 function myError($errCode, $errMsg, $errFile, $errLine) { //错误处理 }
1 <?php 2 //自定义错误处理器 3 //第一步: 设定要作为错误处理的函数名 4 set_error_handler("my_error_handler"); 5 6 //第2步: 定义函数 7 /** 8 * 自定义错误处理函数 9 * 该函数不要在程序中调用,一发生错误会被自动调用,而且会传入该4个实参数据 10 * @param string $errCode 错误代号(级别) 11 * @param string $errMsg 错误信息的内容 12 * @param string $errFile 发生错误的文件名 13 * @param int $errLine 代表发生错误的行号 14 * @return void 15 */ 16 function my_error_handler($errCode,$errMsg,$errFile,$errLine) { 17 $str = ''; 18 $str .= "<p><b><font color='red'>错误:</font></b>"; 19 $str .= "<br />错误代号是:".$errCode; 20 $str .= "<br />错误内容是:".$errMsg; 21 $str .= "<br />错误文件是:".$errFile; 22 $str .= "<br />错误行号是:".$errLine; 23 $str .= "<br />发生的时间:".date("Y-m-d H:i:s"); 24 $str .= "</p>"; 25 echo $str; //输出该“构建”的错误完整处理结果 26 //可以将该内容写入到某个文件去,既记录错误日志 27 } 28 29 //以下是错误代码 30 echo "<br />aaaa"; 31 echo $v1; //使用不存在的变量 32 echo C1; // 使用不存在的常量 33 echo "<br />bbbb"; 34 echo "<hr />";
点击查看自定义错误处理器测试
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of Introduction to PHP file loading and error handling. For more information, please follow other related articles on the PHP Chinese website!