Home > Article > Backend Development > How to catch errors and display friendly information in PHP_PHP Tutorial
To catch errors, you cannot use try{...}catch(){}. The try{...}catch in PHP needs to throw an exception yourself to catch it, which is different from other languages.
Secondly, you can use the following method to achieve the same effect:
error_reporting(0);//Set to block system error prompts and put them at the top of the page
//$string = file_get_contents("index.html");//Normal code
echo 5/0;//Change: 5/8 //Normal code
//If there is an error in the normal code, handle it:
$arr=error_get_last();//Get the error information that just occurred and return an array. If there is no error, return null.
if(isset($arr) ) //If it is not null, it means something went wrong
{
echo "An error occurred, error message:";
print_r($arr); //Specific error information can be modified as needed.
exit;
}
-------------------------------------------------- ---
【echo 5/8;】Output:
0.625
【echo 5/0;】Output:
An error occurred, error message:
Array (
[type] => 2
[message] => Division by zero
[file] => D:wampwwwsinaeditornewfile.php
[line] => 13
)