错误是程序中的故障或错误。它可以有多种类型。由于错误的语法或错误的逻辑,可能会发生错误。这是一种错误或对代码的了解不正确的情况。
PHP 中存在多种类型的错误,但基本上包含四种主要错误类型。
<?php
$x = "geeks";
y = "Computer science";
echo $x;
echo $y;
?>
PHP 解析错误:语法错误,意外的“=”
在/home/18cb2875ac563160a6120819bab084c8.php第3行
解释:在上面的程序中,第 3 行缺少 $ 符号,因此会给出错误消息。
这是 PHP 编译器理解 PHP 代码但识别未声明函数的错误类型。这意味着在没有函数定义的情况下调用函数。
<?php
function add($x, $y)
{
$sum = $x + $y;
echo "sum = " . $sum;
}
$x = 0;
$y = 20;
add($x, $y);
diff($x, $y);
?>
PHP Fatal error: Uncaught Error:
Call to undefined function diff()
in /home/36db1ad4634ff7deb7f7347a4ac14d3a.php:12
Stack trace:
#0 {main}
thrown in /home/36db1ad4634ff7deb7f7347a4ac14d3a.php on line 12
解释:在第 12 行中,调用了函数,但函数的定义不可用。所以它给出了错误。
警告错误的主要原因是包含丢失的文件。这意味着 PHP 函数调用了丢失的文件。
<?php
$x = "thisis demo";
include ("gfg.php");
echo $x . "Computer science portal";
?>
PHP Warning: include(gfg.php): failed to
open stream: No such file or directory in
/home/aed0ed3b35fece41022f332aba5c9b45.php on line 5
PHP Warning: include(): Failed opening 'gfg.php'
for inclusion (include_path='.:/usr/share/php') in
/home/aed0ed3b35fece41022f332aba5c9b45.php on line 5
说明:该程序调用了一个未定义的文件 gfg.php,该文件不可用。所以它会产生错误。
与警告错误类似。这意味着程序包含错误,但允许执行脚本。
<?php
$x = "this for demo";
echo $x;
echo $geeks;
?>
PHP 警告:未定义的变量:geeks 在
/home/84c47fe936e1068b69fb834508d59689.php 第 5 行
说明:该程序使用未声明的变量$geeks,因此会给出错误消息。
PHP 用于 Web 开发。PHP 中的错误处理几乎与所有编程语言中的错误处理类似。PHP 中的默认错误处理将给出文件名行号和错误类型。
处理PHP错误的方法:
基本错误处理:使用 die() 函数die() 函数打印一条消息并从当前脚本退出。 句法:
<?php
// Php code showing default error handling
$file = fopen("geeks.txt", "w");
?>
注意:运行上面的代码并且geeks.txt文件不存在,那么它将显示运行时错误消息。
PHP 警告:fopen(geeks.txt):无法打开流:权限被拒绝
在 /home/dac923dff0a2558b37ba742613273073.php 第 2 行
为了防止此错误,请使用 die() 函数。下面是 die() 函数的实现:
<?php
// PHP code to check errors
// If file is not present
// then exit from script
if( !file_exists("demo.txt") ) {
die("File is not present");
}
// If file is present
// then continue
else {
$file = fopen("demo.txt", "w");
}
?>
注意:如果demo.txt文件不存在,则会显示输出。
输出 :
File is not present
在 PHP 中创建自定义错误处理程序非常简单。创建一个在 PHP 发生错误时可以调用的函数。
error_function( $error_level, $error_message, $error_file, $error_line, $error_context)
参数:该函数接受上面提到的五个参数,如下所述:
error_level:下面列出了可能的错误级别:
set_error_handler() 函数:创建 myerror() 函数后需要设置自定义错误处理程序,因为 PHP 以正常方式处理它,但如果用户执行自定义错误处理,则用户必须将其设置为参数并将 myerror 函数作为字符串传递。
例子:
<?php
// Creates my error function which prints message
//to user
function myerror($error_no, $error_msg) {
echo "Error: [$error_no] $error_msg ";
echo "\n Now Script will end";
// When error occurred script has to be stopped
die();
}
// Setting set_error_handler
set_error_handler("myerror");
$a = 10;
$b = 0;
// This will generate error
echo($a / $b);;
?>
总是尝试使用自定义错误处理来进行错误处理,因为它会根据用户显示更多对用户有帮助的指定消息。如果未使用自定义错误处理来处理错误,则发生错误,则默认情况下脚本将停止,但如果使用自定义错误处理来处理错误,则可以在显示错误消息后继续执行脚本。
PHP 中有四种显示错误的方法,如下所示:
示例:要显示 PHP 中的错误,最快、最简单的方法是将以下行添加到代码中。
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
这两个指令不显示解析错误。
方案一:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include("gfg.php");
?>
要显示错误(包括解析错误),必须在 php.ini 中进行以下更改并重新启动 php-fpm、apche2
显示错误=开
方案2:
<?php
for($i = 0; $i <= 5 $i++)
{
echo $i;
}
?>
输出:
上述指令将显示在浏览器上加载网站时遇到的任何 PHP 错误。当站点处于活动状态时,应禁用显示错误,以防止不在开发环境中时出现任何安全 问题。