


PHP common function blocks_error and exception handling - php (32)
1. Error and exception handling
1.1 Error types and basic debugging methods
??Errors in PHP programs generally fall into the following three areas:
??Syntax errors:
??Syntax errors are the most common and easy to fix. For example: a semicolon is missing in the code. Such errors prevent the script from executing.
??Runtime error:
??This kind of error generally does not prevent the execution of the PHP script, but it will prevent what is currently being done. An error is output, but the php script continues to execute
?? Logic error:
?? This kind of error is the most troublesome, as it neither prevents the script from executing nor outputs an error message.
??An exception is an exception or an event that occurs during the execution of a program, which interrupts the execution of normal instructions and jumps to other program modules to continue execution.
PHP’s error reporting level
??E_ALL //All message values: 6143
??E_ERROR //Fatal runtime error value: 1
??E_RECOVERABLE_ERROR //Near-fatal run Time error, if not caught, it will be treated as E_ERROR Value: 4096
??E_WARNING //Runtime warning (non-fatal error) Value: 2
??E_PARSE //Compile time parsing error value: 4
??E_NOTICE //Runtime reminder (often a bug, may also be intentional) Value: 8
??E_STRICT //Coding standardization warning (recommended how to modify for forward compatibility) Value: 2048
??E_CORE_ERROR //Fatal error value during PHP startup initialization process: 16
??E_CORE_WARNING //Warning (non-fatal error) value during PHP startup initialization process: 32
??E_COMPILE_ERROR // Compile-time fatal error value: 64
??E_COMPILE_WARNING //Compile-time warning (non-fatal error) value: 128
??E_USER_ERROR //User-defined fatal error value: 256
?? E_USER_WARNING //User-defined warning (non-fatal error) Value: 512
??E_USER_NOTICE //User-defined reminder (often bug) Value: 1024
php.ini configuration file
display_errors: Whether to enable the function of PHP output error report
?? Values are: On (default output error report), Off (mask all error messages)
?? The ini_set() function can be called in the PHP script, Dynamically set the php.ini configuration file.
??For example: ini_set("display_errors","On"); //Display all error information
??error_reporting: Set different error reporting levels.
??error_reporting= E_ALL & ~E_NOTICE
--can throw any non-noticeable error, default value
??error_reporting= E_ERROR | E_PARSE | E_CORE_ERROR
--only fatal runtimes are considered errors, new parsing errors, and core errors.
??error_reporting= E_ALL & ~(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE)
--Report all errors except user-caused errors.
??The error reporting level can be dynamically set through the error_reporting() function in PHP scripts. Such as: error_reporting(E_ALL);]
Set error level example: error.php
<span><h2>测试错误报告</h2> <?<span>php </span><span>/*</span><span>开启php.ini中的display_errors指令,只有该指令开启如有错误报告才能输出</span><span>*/</span><span>ini_set(</span><span>'</span><span>display_errors</span><span>'</span>,<span>1</span><span>); </span><span>/*</span><span>通过error_reporting()函数设置在本脚本中,输出所有级别的错误报告</span><span>*/</span><span>error_reporting(E_ALL); </span><span>/*</span><span>“注意(notice)”的报告,不会阻止脚本的执行,并且不一定是一个问题</span><span>*/</span><span>getType($</span><span>var</span>);<span>//</span><span>调用函数时提供的参数变量没有在之前声明</span><span>/*</span><span>“警告(warning)”的报告,指示一个问题,但是不会阻止脚本的执行</span><span>*/</span><span>getType();</span><span>//</span><span>调用函数时没有提供必要的参数</span><span>/*</span><span>“错误(error)”的报告,它会终止程序,脚本不会再向下执行</span><span>*/</span><span>get_Type();</span><span>//</span><span>调用一个没有被定义的函数</span>?></span>
Configuration instructions for PHP error reporting behavior
display_startup_errors= Off
??Whether to display the PHP engine Error encountered during initialization.
??log_errors= On
??Determines the location of log statement recording.
??error_log (default null)
??Specify the file where errors are written or record the error log in the system log syslog.
??Log_errors_max_len=1024
??The maximum length of each log entry, in bytes. 0 means maximum.
1.2 Error log
Two ways to record error logs:
??Use the specified file to record the error report log
??The error log is recorded in the operating system log
Use The specified file records the error report log
1. First configure php.ini:
??error_reporting= E_ALL//Every error will be sent to PHP
??display_errors=Off//Do not display error reports
??log_errors=On//Determine the location of log statement recording.
??log_errors_max_log=1024//The maximum length of each log item
??error_log=G:/myerror.log//Specify the file where errors are written
??2. Use function: in php Use error_log() in the file to record the log, and you can write the information to the myerror.log file
??For example: error_log("Login failed!");
2. Use four functions to Logging:
??define_syslog_variables();//Initialize configuration for system log
??openlog();//Open a log link
??syslog();//Send a log
Example
<span><?<span>php </span><span>if</span>(!<span>Ora_Logon($username, $password)){ error_log(</span><span>"</span><span>Oracle数据库不可用!</span><span>"</span>, <span>0</span><span>); </span><span>//</span><span>将错误消息写入到操作系统日志中</span><span>} </span><span>if</span>(!($foo=<span>allocate_new_foo()){ error_log(</span><span>"</span><span>出现大麻烦了!</span><span>"</span>, <span>1</span>, <span>"</span><span>webmaster@www.mydomain.com</span><span>"</span>); <span>//</span><span>发送到管理员邮箱中</span><span>} error_log(</span><span>"</span><span>搞砸了!</span><span>"</span>,<span>2</span>, <span>"</span><span>localhost:5000</span><span>"</span><span>); </span><span>//</span><span>发送到本机对应5000端口的服务器中</span>error_log(<span>"</span><span>搞砸了!</span><span>"</span>, <span>3</span>, <span>"</span><span>/usr/local/errors.log</span><span>"</span><span>); </span><span>//</span><span>发送到指定的文件中</span>?></span>
<span><?<span>php define_syslog_variables(); openlog(</span><span>"</span><span>PHP5</span><span>"</span><span>, LOG_PID , LOG_USER); syslog(LOG_WARNING, </span><span>"</span><span>警告报告向syslog中发送的演示,警告时间:</span><span>"</span>.date(<span>"</span><span>Y/m/dH:i:s</span><span>"</span><span>)); closelog(); </span>?></span>
View the log: For example, in Windows system, you can see the log by right-clicking "My Computer"-> Select Management Options->Select Event Viewer in the System Tools Menu->In the Application Options
1.3 Exception Handling
Exception (Exception) handling is used to change the normal flow of the script when a specified error occurs. It is an important new feature in PHP5. Exception handling is an extensible, easy-to-maintain error handling unified mechanism, and provides a new object-oriented error handling method.
??Exception handling format:
try{
Use try to include code where exceptions may occur.
Once an exception occurs, try to capture the exception and hand it over to catch for processing.
Throw exception statement: throw exception object.
}catch (exception object parameter) {
Exception handling is done here.
}[catch(.,,){
.. .. ..
}]
The above introduces the common function blocks of PHP_Error and Exception Handling - php (32), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
