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 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
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
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’ 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
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 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
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
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