search
HomeBackend DevelopmentPHP ProblemWhat are the common error types in php

What are the common error types in php

Apr 24, 2023 pm 04:10 PM
php error type

Common error types in php are: 1. Parse error type, indicating parsing error or syntax error; 2. Fatalerror type, indicating fatal error; 3. Warning type, indicating warning error; 4. Notice type, Indicates attention to errors; 5. Deprecated type, indicating the lowest level error.

What are the common error types in php

Operating system for this tutorial: Windows 10 system, PHP version 8.1, Dell G3 computer.

1. PHP error type

PHP error level

Parse error > Fatal Error > Waning > Notice > Deprecated

1. Parse error or syntax error (Parse error)

Syntax error is the most common error in programming and the easiest to solve, such as: missing a point An error message will be displayed when the number is reached. This error stops program execution and displays an error message. We can correct the program based on the error message and re-execute it.

[Example] The following uses simple code to demonstrate common syntax errors and related error messages.

<?php
    $a = 1;
    $b = 2;
    $c = $a + $b
    echo ;
?>

; is omitted at the end of line 4 in the above code, so running the above code will display the following error message:

Parse error: syntax error, unexpected &#39;echo&#39; (T_ECHO) in D:\WWW\index.php on line 5

As can be seen from the above example and running results, syntax errors will Prevents the program from continuing downward execution. Only after these errors are resolved can the program execute smoothly.

2. Fatal error:

This is the type of error where the PHP compiler understands the PHP code but it recognizes an undeclared function. This means calling a function without a function definition.

The program reports an error directly and the code needs to be modified! ! ! To interrupt program execution, you can use the register_shutdown_function() function to trigger a function before the program terminates, such as

<?php 
function add($x, $y) { 
    $sum = $x + $y; 
    echo "sum = " . $sum; 
}
$x = 0; 
$y = 20;
add($x, $y); 
diff($x, $y); 
?>

What are the common error types in php

Description: In line 10, the function diff() is called, But the function diff() is not defined in the declaration, so it gives an error.

3. Warning:

There is no syntax error in the program, but during execution, PHP will find some unreasonable aspects of the program, thus A warning message will appear, but the program will continue to execute.

Example: Using 0 as the divisor will cause the program to run incorrectly and output an error message.

<?php
    $a = 1;
    $b = 0;
    $c = $a / $b;
    echo "$a / $b = $c";
?>

Error:

What are the common error types in php

4. Note:

It is similar to a warning error, This means that the program contains an error, but it allows script execution. This occurs when some undefined variables, constants, or array keys are used without quotes, and the program continues execution

<?php  
header("content-type:text/html;charset=utf-8");
$x = "PHP中文网"; 
echo $x; 
echo $y; 
?>

Description: This program uses the undeclared variable $y, so it gives an error message.

5. The lowest level error (Deprecated, not recommended, not recommended)

Will occur when using some expired functions, and the program will continue to execute.

2. PHP error configuration

In addition to setting it in the script, you can also configure it in the php.ini configuration file

error_reporting = E_ALL&~E_NOTICE; //设置错误报告级别
display_errors = 1; //开发环境开启,生产环境关闭

3 , PHP exception

PHP exception is a new feature of PHP5. Different from JAVA/C# exception, PHP exception needs to be thrown manually by throw new Exception instead of automatically thrown by the system.

The difference between PHP errors and exceptions. They are two different concepts, but they have something in common:

If the exception is not caught and handled, the program will terminate and a Fatal Error will be reported. , when you see this, everyone will wonder if the anomaly is a wrong kind. This is an illusion, but it can be understood this way. However, the program can continue to execute after the exception is caught, but the program must terminate after the real Fatal Error occurs.

Exceptions can be captured using try{}catch(){}. After capture, subsequent code can continue to execute; while errors cannot be captured using try{}catch(){ }Captured.

If an exception is thrown, it must be caught, otherwise the program will terminate execution.

4. PHP exception and error throwing

Exception throwing: throw new Exception('Some Error Message');

Error thrown: trigger_error();

trigger_error()The triggered error will not be caught by the try-catch exception capture statement

5. PHP error handling

set_error_handler()

can only handle three levels of errors: Deprecated, Notice, and Waning, and after processing, the script will continue to execute the next line where the error occurred.

register_shutdown_function()

This method is the last callback function before the end of the script, so whether it is die()/error (exception)/or the script ends normally, it will be called

6. PHP exception Handling

set_exception_handler()

Set the default exception handler. If there is a try/catch capture, this function will not be executed. Otherwise, it will be executed. And if it is executed, the script will not continue to execute after the exception occurs. One line of code, the program will terminate immediately

set_exception_handler()Notes

set_exception_handler(“myException”) 不仅可以接受函数名,还可以接受类的方法(公开的静态方法 及 公开的非静态方法 都可以),但需要以 数组形式 传递,数组的第一值为“类名”,第二个参数为“方法名”,如下代码所示:

<?php
class App{
    function myException($exception) {
        echo "<b>Exception:</b> " , $exception->getMessage();
    }
}
set_exception_handler(array(&#39;App&#39;,&#39;myException&#39;));
 
throw new Exception(&#39;Uncaught Exception occurred&#39;);
?>

七、PHP7 异常处理的大变化

在PHP7之前,Deprecated、Notice、Waning这类错误是可以捕获的(使用set_error_handler()函数),而Fatel Error无法捕获的。

在PHP7之后,出现了一个异常与错误通用的接口Throwable,Exception类与Error类都实现了该接口,导致Error类或Error类的派生类的错误对象(大部分Fatel Error,而之前三类错误不变)也可以像Exception一样被捕获(2种捕获方法:1、try/catch 2、set_exception_handler())。

The above is the detailed content of What are the common error types in php. 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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools