How to implement skipping when php encounters an error: 1. Open the corresponding PHP code file; 2. Display the error and suppress it through the @error control operator.
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
php How to skip when encountering an error?
PHP skips errors and continues execution:
@The error control operator can achieve this function. The
@ symbol can ignore errors and has the function of suppressing errors.
For example:
function db_connect()//连接数据库 { @$db =mysql_connect('localhost','root','test'); if(!$db) { throw new Exception('连接数据库失败!请重试!'); } mysql_select_db('book'); return $db; }
If the connection to the database is unsuccessful, the preceding "@" can suppress the error display, that is, the error will not be displayed, and then an exception will be thrown and displayed. It is recommended to use your own defined exception handling sparingly, as this will increase a certain amount of system overhead.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to skip when php encounters an error. For more information, please follow other related articles on the PHP Chinese website!