search
HomeBackend DevelopmentPHP TutorialIntroduction 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



##File loading

File loading statements

1) 4 file loading statements:

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 file

Path

 1)

Relative path

: 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)

Absolute path

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 process

step 1: 从include|require 语句处退出PHP脚本模式(进入html代码模式)
step 2:载入include|require 语句所设定的文件中的代码,并执行
step 3:退出html模式重新进入php脚本模式,继续之后的代码

The difference between include, include_once, require, require_once

1) The difference between include and require, or the difference between include_once and 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

# 1) include_once, require_once loading statement, if the loading is successful, it returns 1, if the loading fails, it returns false

  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 classification

 1) Syntax error

  If 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 = &#39;&#39;;
 18     $str .= "<p><b><font color=&#39;red&#39;>错误:</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中文网!

相关推荐:

PHP基本语法的介绍

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP语言开发中如何处理请求头错误?PHP语言开发中如何处理请求头错误?Jun 10, 2023 pm 05:24 PM

在PHP语言开发中,请求头错误通常是由于HTTP请求中的一些问题导致的。这些问题可能包括无效的请求头、缺失的请求体以及无法识别的编码格式等。而正确处理这些请求头错误是保证应用程序稳定性和安全性的关键。在本文中,我们将讨论一些处理PHP请求头错误的最佳实践,帮助您构建更加可靠和安全的应用程序。检查请求方法HTTP协议规定了一组可用的请求方法(例如GET、POS

PHP中json_encode()函数错误的原因及解决方式PHP中json_encode()函数错误的原因及解决方式May 11, 2023 am 09:03 AM

随着Web应用程序的不断发展,数据交互成为了一个非常重要的环节。其中,JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,广泛用于前后端数据交互。在PHP中,json_encode()函数可以将PHP数组或对象转换为JSON格式字符串,json_decode()函数可以将JSON格式字符串转换为PHP数组或对象。然而,

PHP命令行错误:你可能不知道的事情PHP命令行错误:你可能不知道的事情May 11, 2023 pm 08:21 PM

本文将介绍关于PHP命令行错误的一些你可能不知道的事情。PHP作为一门流行的服务器端语言,一般运行在Web服务器上,但它也可以在命令行上直接运行,比如在Linux或者MacOS系统下,我们可以在终端中输入“php”命令来直接运行PHP脚本。不过,就像在Web服务器中一样,当我们在命令行中运行PHP脚本时,也会遇到一些错误。以下是一些你可能不知道的有关PHP命

PHP语言开发中如何处理日期格式化错误?PHP语言开发中如何处理日期格式化错误?Jun 09, 2023 pm 06:40 PM

在PHP语言开发中,日期格式化错误是一个常见的问题。正确的日期格式对于程序员来说十分重要,因为它决定着代码的可读性、可维护性和正确性。本文将分享一些处理日期格式化错误的技巧。了解日期格式在处理日期格式化错误之前,我们必须先了解日期格式。日期格式是由各种字母和符号组成的字符串,用于表示特定的日期和时间格式。在PHP中,常见的日期格式包括:Y:四位数年份(如20

PHP中的容错机制PHP中的容错机制May 23, 2023 am 08:16 AM

在编写程序时总会存在各种各样的错误和异常。任何编程语言都需要有良好的容错机制,PHP也不例外。PHP有许多内置的错误和异常处理机制,可以让开发者更好地管理其代码,并正确地处理各种问题。下面就让我们一起来了解一下PHP中的容错机制。错误级别PHP中有四个错误级别:致命错误、严重错误、警告和通知。每个错误级别都有一个不同的符号表示,以帮助识别和处理错误:E_ER

PHP语言开发中如何处理开发环境与生产环境的数据不一致错误?PHP语言开发中如何处理开发环境与生产环境的数据不一致错误?Jun 10, 2023 am 10:31 AM

随着互联网的快速发展,开发人员的任务也随之多样化和复杂化。特别是对于PHP语言开发人员而言,在开发过程中面临的最常见问题之一就是在开发环境和生产环境中,数据不一致的错误问题。因此,在开发PHP应用程序时,如何处理这些错误是开发人员必须面对的一个重要问题。开发环境和生产环境的区别首先需要明确的是,开发环境和生产环境是不同的,它们有着不同的设置和配置。在开发环境

PHP语言开发中解析JSON时常见错误及处理方法PHP语言开发中解析JSON时常见错误及处理方法Jun 10, 2023 pm 12:00 PM

在PHP语言开发中,常常需要解析JSON数据,以便进行后续的数据处理和操作。然而,在解析JSON时,很容易遇到各种错误和问题。本文将介绍常见的错误和处理方法,帮助PHP开发者更好地处理JSON数据。一、JSON格式错误最常见的错误是JSON格式不正确。JSON数据必须符合JSON规范,即数据必须是键值对的集合,并使用大括号({})和中括号([])来包含数据。

国外程序员分享的PHP错误处理与调试技巧国外程序员分享的PHP错误处理与调试技巧May 11, 2023 pm 12:12 PM

PHP(HypertextPreprocessor)是一种广泛用于Web开发的脚本语言。在开发PHP应用程序时,错误处理和调试被认为是非常重要的一块。国外程序员在经验中积累了许多PHP错误处理和调试技巧,下面介绍一些比较常见和实用的技巧。错误报告级别修改在PHP中,通过修改错误报告级别可以显示或禁止显示特定类型的PHP错误。通过设置错误报告级别为“E_AL

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)