PHP Error Handling

Here you will learn about handling error in PHP with example.

You can handle error in PHP with your own to make PHP code more efficient and professional.

Mostly, error can be handled in PHP by using if-else statement of PHP.

PHP Error Handling Example

Before going to the error handling example code, let's first create a textual file named codescracker.txt inside the directory C:\xampp\htdocs\ and now follow the example given below to understand about error handling in PHP.

Here is an example shows how you can easily handle errors in your PHP program/code:

   $filename = "codescracker.txt";
   <title>Error Handling in PHP with Example - codescracker</title>
   // check whether file exists or not
      // print the message when file doesn't exist
      die("The given file doesn't exists inside the current directory");
      // further proceed, if the file exists
      $ftr = fopen($filename, "r");
      echo "The given file exists inside the current directory";

Save the above code inside the same directory where the textual file is created, that is, C:\xampp\htdocs\, with name codescracker.php.

Now open your browser and type localhost/codescracker.php, you will see the following sample output:

php error handling

Now, delete the file named codescracker.txt (textual file) and re-run or refresh the browser, then you will see the following sample output this time:

error handling in php example

PHP Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube