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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-20.png)
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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-21.png)
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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-22.png)
Notice Errors
In fact, a notice error may be or may not be an errors. These are minor errors. Furthermore, notice errors don’t stop script and produces the output. The most common source f notice errors are the use of 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 notice error occurs.
<?php
echo $a.' '.$b.' '.$c;
?>
Output
![Notice Errors Due to the Use of Non-Existing variables](https://www.programmingempire.com/wp-content/uploads/2022/06/image-13.png)
Runtime Errors (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 doesn’t 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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-16.png)
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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-17.png)
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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-23.png)
The following fatal error results from the fact that the database table ’emp’ doesn’t 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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-18.png)
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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-19-1024x185.png)
Warnings
Warnings indicate the presence of some issues. However, they don’t 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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-14-1024x182.png)
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](https://www.programmingempire.com/wp-content/uploads/2022/06/image-15-1024x261.png)
Further Reading
Creating Single Page Applications with Angular
Angular 10 Data Binding in Different Ways
Creating Some Angular Components
Examples of Array Functions in PHP
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML