Thursday, September 12, 2019

Error Types in PHP


Error Types in PHP


Here, we are going to explain about error types in PHP.

When we write any wrong syntax or wrong code, then error comes. It is a type of mistake.


Error Types in PHP
PHP contains basically four types of errors.

1. Syntax Error
2. Fatal Error
3. Warning Error
4. Notice Error

Syntax Error
When we write any wrong syntax, then this types of error occures. For example, missing semicolon, missing dollar before variable, Extra parentheses etc. It is also known as Parsed Error. It is caught by compiler. It stops execution of script.

Example1:
<?php
$var1 = 'Desktop';
$var2 = 'Mobile'
$var3 = 'Tablets';
?>

Error:
Parse error: syntax error, unexpected ‘$var3’ (T_VARIABLE) in error_handling.php on line 5

Example2:

<?php
$var1 = 'Desktop';
var2 = 'Mobile';
$var3 = 'Tablets';
?>

Error:
Parse error: syntax error, unexpected ‘=’ in error_handling.php on line 4

Fatal Error
This type of error comes generally, when we call an undefined function OR when we make object of an non-existing class. It is very critical error an stop execution of script.

Example:

<?php
function addition($a, $b){
return $a + $b;
}

echo substraction(10, 20);
?>

In above program, we have made a function name addition, but we are using function substraction, which is not defined.

Error:
Fatal error: Uncaught Error: Call to undefined function substraction() in error_handling.php:11 Stack trace: #0 {main} thrown in error_handling.php on line 11

Warning Error
This type of error comes generally, when we missed any file to include OR passed wrong number of parameters to a function. It will not stop the script execution. It is a non-critical error.

Example1:

<?php
$revenue=10;
$impression= 0;
$ctr = $revenue / $impression;
echo "The value of ctr is:". $ctr;
?>

Error:
Warning: Division by zero in error_handling.php on line 18
The value of ctr is:INF

Example2:

<?php
include('connection.php');
echo "The sum of 10 and 20 is: ". (10+20);
?>

Error:
Warning: include(connection.php): failed to open stream: No such file or directory in error_handling.php on line 21

Warning: include(): Failed opening ‘connection.php’ for inclusion (include_path=’php\PEAR’) in error_handling.php on line 21
The sum of 10 and 20 is: 30

Notice Error
This error is same as warning error. It does not stop script execution. It is also non-critical error. It comes generally, when we used any variable without define them.

Example:

<?php
$sum = $sum + 2;
echo "The value of sum is: ". $sum;
?>

Error:

Notice: Undefined variable: sum in error_handling.php on line 24
The value of sum is: 2

No comments:

Post a Comment