Home >Backend Development >PHP Tutorial >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.