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

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.

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

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

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

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

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

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

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

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

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

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
