Home Backend Development PHP Problem What 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 ;
?>
Copy after login

; 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
Copy after login

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); 
?>
Copy after login

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";
?>
Copy after login

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; 
?>
Copy after login

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; //开发环境开启,生产环境关闭
Copy after login

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()
Copy after login

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()
Copy after login

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()
Copy after login

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;);
?>
Copy after login

七、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 of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

This article explores efficient PHP array deduplication. It compares built-in functions like array_unique() with custom hashmap approaches, highlighting performance trade-offs based on array size and data type. The optimal method depends on profili

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

This article explores PHP array deduplication using key uniqueness. While not a direct duplicate removal method, leveraging key uniqueness allows for creating a new array with unique values by mapping values to keys, overwriting duplicates. This ap

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

This article analyzes PHP array deduplication, highlighting performance bottlenecks of naive approaches (O(n²)). It explores efficient alternatives using array_unique() with custom functions, SplObjectStorage, and HashSet implementations, achieving

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

This article explores optimizing PHP array deduplication for large datasets. It examines techniques like array_unique(), array_flip(), SplObjectStorage, and pre-sorting, comparing their efficiency. For massive datasets, it suggests chunking, datab

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea

See all articles