Types of Errors in PHP and How to Handle Them

0
32

In this article, I am going to discuss the Types of Errors in PHP and How to Handle Them.

There are four types of errors that a PHP Script can generate.

  • Syntax errors or compile errors
  • Runtime errors or fatal errors
  • Warnings
  • Notice Errors

Syntax Errors (Compile Errors)

Syntax errors are those errors that the compiler catches. These errors result from wrong spelling, missing semicolon, not ending the string properly or any other typing mistake. When compiler encounters any such error, it reports these errors as syntax errors. The following code shows syntax errors in lin2 and line 6.

<?php
$a="hello";
$b='world!

echo $a;
echo '<br>';
ech $b;

?>

Output

A Parse Error Due to Missing apostrophe and semicolon

When we make correction in line 2 of the above program as $ b = ‘world!’, We get another error. This is due to spelling mistake in echo function in the last line.

A Parse Error due to Spelling Mistake
A Parse Error due to Spelling Mistake

Once we correct both of these errors as follows, we get the following output.

<?php
$a="hello";
$b='world!';

echo $a;
echo '<br>';
echo $b;

?>

Output

The Output After Removing Syntax Errors
The Output After Removing Syntax Errors

Notice Errors

In fact, a notice error may or may not be an error. These are minor errors. Furthermore, notice errors do not stop script and produces the output. The most common source f notice errors are the use of undefined variables. As can be seen in the following code, the variables a, b, and c are not defined. So, when we try to print these variables, a notice error occurs.

<?php
 echo $a.' '.$b.' '.$c;
?>

Output

Notice Errors Due to the Use of Non-Existing Variables
Notice Errors Due to the Use of Non-Existing Variables

Fatal Errors

These errors make the script to crash. When a fatal error occurs, the script stops executing. These types of errors result from a number of sources. For instance, call to a non-existing function, or calling a function from an object that does not exist.

Examples on Types of Errors in PHP and How to Handle Them

The following code shows that call to a non-existing function mysqli results in a fatal error.

<?php
 // Try to connect with a MySQL database
 $con1=mysqli("localhost", "root", "", "somedatabase");
?>

Output

Fatal Error Due to the Use of Undefined Function
Fatal Error Due to the Use of Undefined Function

Similarly the following error occurs because of the undefined function show ().

<?php
 class MyClass
 
   protected $a, $b;
 
 class SubClass extends MyClass
    function __construct()
    
       $this->a=1; $this->b=2;
       show();
     
    function show()
    
	echo 'a=", $this->a;
        echo "<br>b = ", $this->b;
    
  
  $ob=new SubClass(20, 30);
  $ob->__construct();
?>

Output

Call to Undefined Function Results in a Fatal Error
Call to Undefined Function Results in a Fatal Error

Correcting the Above Error

In order to correct the above error, just call the function show () as the instance method as shown below.

<?php
 class MyClass
 
   protected $a, $b;
 
 class SubClass extends MyClass
    function __construct()
    
       $this->a=1; $this->b=2;
       $this->show();
     
    function show()
    
	echo "a=", $this->a;
        echo "<br>b = ", $this->b,"<br>';
    
  
  $ob=new SubClass(20, 30);
  $ob->__construct();
?>

Output

An Example to Show the Types of Errors in PHP and How to Handle Them
An Example to Show the Types of Errors in PHP and How to Handle Them

The following fatal error results from the fact that the database table ’emp’ does not exist.

<?php
 // Try to connect with a MySQL database
 $con1=new mysqli("localhost", "root", "", "d1");
 $r1=$con1->query('select * from emp');
 $row = $r1->fetch_assoc()
?>

Output

Fatal Error Due to Non-availability of the Database Table
Fatal Error Due to Non-availability of the Database Table

When a recursive function fails to terminate, a runtime error occurs. The following code demonstrates it.

<?php
 function f1()
 
  $a=$a++;
  echo $a;
  f1();
 
 f1();
?>

Output

Fatal Error Due to a Non-Terminating Recursive Function Call
Fatal Error Due to a Non-Terminating Recursive Function Call

Warnings

Warnings indicate the presence of some issues. However, they do not stop execution of script. The following code shows a warning generated when the database user is not able to access a table due to invalid cresentials.

<?php
 // Try to connect with a MySQL database
 $con1=new mysqli("localhost", "someuser", "123456", "somedatabase");
?>

Output

Warning Generated Due to Access Denied to a User
Warning Generated Due to Access Denied to a User

Similarly, the following code generates a warning because of invalid database name.

<?php
 // Try to connect with a MySQL database
 $con1=new mysqli("localhost", "root", "", "somedatabase");
?>

Output

Warning Generated Due to the Use of Unknown Database
Warning Generated Due to the Use of Unknown Database

Further Reading

Overview of Mean Stack

Creating Single Page Applications with Angular

Angular 10 Data Binding in Different Ways

Creating Some Angular Components

Examples of Array Functions in PHP

Basic Programs in PHP

princites.com

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here