PHP Error Handling

php error handling. Whenever there is an error in your code, PHP shows it like a report. Any errors that PHP performs is of some kind. All these types are represented by constants in PHP and all of these values are integer.

When there is value 1 of constant, then error is the show. And until that type of error does not come, the value of constant is 0.

Types of PHP Errors | php error handling

Constant Description
E_CORE_ERRORThis is the error that comes in the startup of PHP.
E_CORE_WARNINGThese are warnings that show up in PHP’s startup.
E_ErrorThis is a runtime error. This stops the program. Such as memory allocation problem.
E_WARNINGThese run warnings happen, the program does not stop.
E_PARSEThis is compile time parse errors.
E_NOTICEThese run times notice that error may occur.
E_COMPILE_ERRORThese are the errors that are generated when compiling the PHP code.
E_COMPILE_WARNINGThese are warnings that show up when compiling PHP code.
E_USER_ERROR These are the errors that the user generates by itself own code.
E_USER_WARNING These are the warnings generated by the user.
E_USER_NOTICE These are notices generated by the user.
E_STRICT These are the suggestions given by PHP to improve your code.
E_RECOVERABLE_ERROR These errors are easily caught in the catch. There is no harm to your application from them.
E_ALL All these errors are used to represent together.

PHP Error Handling

It is very important to handle any error in your program. If you leave the php error handling then your program stops for most of the time when you encounter an error.

But if you want your program to stop even when there is an error, you can handle those errors manually. Its first method is to use die () function.

Example

<?php
if(!file_exists("welcome.txt")) {
  die("File not found");
} else {
  $file=fopen("welcome.txt","r");
}
?>

Creating a Custom Error Handler

Another way of handling errors in PHP is to create an error handler of your own. To do this, you use error_function (). In this function, you pass 2 parameters, one is the error constants defined above and the other is the error message.

function customError($errno, $errstr) {
  echo "<b>Error:</b> [$errno] $errstr<br>";
  echo "Ending Script";
  die();
}

Once you define the php error handler, you have to set it for such errors. So whenever any error of that type comes, then your function can handle it.

Syntax

error_function(error_level,error_message,
error_file,error_line,error_context)

Example

<?php
//error handler function
function customError($errno, $errstr) {
  echo "<b>Error:</b> [$errno] $errstr";
}

//set error handler
set_error_handler("customError");

//trigger error
echo($test);
?>

PHP Exceptions

PHP also offers exception handling mechanism like other programming languages. In it you handle the exceptions with the help of some keywords. Exceptions are such errors that occur at run time and due to which the execution of the program stops. php error handling

Constant Description
try This is a block. In this you write that code, which can generate error.
catch This is also a block in which you catch the exceptions and they take action.
throw This is a keyword, used to throw exceptions from outside the try block.
finally  After handling Exception, if you want to perform some task, it comes in this block.

Example

<?php
//create function with an exception
function checkNum($number) {
  if($number>1) {
    throw new Exception("Value must be 1 or below");
  }
  return true;
}

//trigger exception in a "try" block
try {
  checkNum(2);
  //If the exception is thrown, this text will not be shown
  echo 'If you see this, the number is 1 or below';
}

//catch exception
catch(Exception $e) {
  echo 'Message: ' .$e->getMessage();
}
?>