Types of Errors in PHP and How to Handle Them

Posted on

In this article, I will discuss the types of errors in PHP and how to deal with them.

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

  • Syntax errors or compilation errors
  • Runtime errors or fatal errors
  • Warnings
  • Notice of errors

Syntax errors (compilation errors)

Syntax errors are errors detected by the compiler. These errors result from misspelling, missing semicolon, incorrectly ending string, or any other typing error. When the compiler encounters such an error, it reports these errors as syntax errors. The following code shows the syntax errors in lin2 and line 6.

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

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

?>

Production

A parse error due to missing apostrophe and semicolon
A parse error due to missing apostrophe and semicolon

When we make a correction to line 2 of the above program like $b=’world!’ we get another error. This is caused by a misspelling in the echo function in the last line.

A parsing error due to a spelling mistake
A parsing error due to a spelling mistake

Once we fixed these two errors as follows, we get the following output.

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

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

?>

Production

The output after removing syntax errors
The output after removing syntax errors

Notice of errors

In fact, a notification error may or may not be an error. These are minor errors. Also, notification errors do not stop the script and produce the output. The most common source notification errors are the use of an undefined variable. 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 notification error occurs.

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

Production

Notice the errors due to the use of non-existent variables
Notice the errors due to the use of non-existent variables

Runtime errors (fatal errors)

These errors cause the script to crash. When a fatal error occurs, the script stops running. These types of errors come from several sources. For example, call a non-existent function or call a function from an object that does not exist.

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

The following code shows that calling a non-existent mysqli function results in a fatal error.

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

Production

Fatal error due to the use of an undefined function
Fatal error due to the use of an undefined function

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

<?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();
?>

Production

Calling an undefined function results in a fatal error
Calling an undefined function results in a fatal error

Fix the above error

In order to fix the above error, simply call the show() function as an 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();
?>

Production

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()
?>

Production

Fatal error due to unavailability of database table
Fatal error due to unavailability of database table

When a recursive function fails to complete, a runtime error occurs. The following code demonstrates this.

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

Production

Fatal error due to uncompleted recursive function call
Fatal error due to uncompleted recursive function call

Warnings

Warnings indicate the presence of certain problems. However, they do not stop script execution. The following code displays a warning generated when the database user cannot access a table due to invalid credentials.

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

Production

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 due to an invalid database name.

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

Production

Warning generated due to use of unknown database
Warning generated due to use of unknown database

Further reading

Introducing the Medium Stack

Building Single Page Applications with Angular

Angular 10 Binding data in different ways

Create Angular Components

Examples of array functions in PHP

Basic programs in PHP

principles.com

Leave a Reply

Your email address will not be published.