How to Display All PHP Errors: For Basic and Advanced Use

[ad_1]

In this tutorial, we will learn how to display all errors in PHP. And as well as learn, how to report all errors in PHP.

First of all, we need to know that in PHP, some error prevents the execution of the script. And some errors only display error messages with warnings. Then we need to show or hide all errors reporting in PHP.

There are three ways to display/show and hide errors in PHP. But first of all, we need to know about php errors.

There are four types of errors in PHP; as follows:

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

Notice Error

When trying to access the undefined variable in PHP script at that time notice error occurs. Notice error does not stop the execution of PHP script. It only notice you that there is a problem.

The following example represents the notice error:

<?php
$a="A variable";
echo $a;
echo $b;
?>

In this example, define only $a variable and print it. As well as print $b variable without define. When executing this script, a notice error occurs and display a message, the $b variable is not defined.

Warning Error

The main reason to occurs warning errors is to include a missing file, calling a function with incorrect parameters. It is similar to notice an error, it also does not stop the execution of PHP script. It only reminds you that there is a problem in the script.

The following example represents the warning error:

<?php
echo "Warning error"';
include ("external_file.php");
?>

In this example, include any PHP file that does not exist in your application. At that time, it displays a message that failed to include it.

Fatal Error

When call to undefined functions and classes in PHP script, at that time fatal error occurs. The Fatal error does stop the execution of the PHP script and it display error message.

Note that, if you call the undefined function or class in the script is the main reason to occurs fatal error.

The following example represents the fatal error:

<?php

function addition(){
 $a = 10;
 $b = 50;
 $c = $a + $b;
 echo "Addition of a + b = ".$c;
}
addition();
sub();

?>

In this example, A function is defined, which name is addition(). This function adds 2 variables and prints its result.

When the addition() function is called. After that, sub() function is also called which is not defined. At this time, the fatal error comes and stops the execution of the script with an error message.

Parse Error

The parse error happens, when a syntax mistyped in the script. Parse error does also stops the execution of the PHP script and it display error message.

The following main reason for syntax/parse errors are:

  • Unclosed quotes
  • Missing or Extra parentheses
  • Unclosed braces
  • Missing semicolon

The following example represents the parse error:

<?php
echo "1";
echo "2"
echo "3";
?>

In this example, print a 1, 2, 3. After print “1”, you missed a semicolon. At that time it occurs.

How to Display All PHP Errors: For Basic and Advanced Use

Use the following method to display all types of errors in PHP:

  • Suddenly Show All PHP Errors
  • Configure PHP.ini to Display All Errors
  • Configuration .htaccess To Display All PHP Errors

Suddenly Show All PHP Errors

To display all php errors and warnings is to add these lines to your PHP code file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Configure PHP.ini to Display All Errors

If adding some of the PHP code errors doesn’t show in the browser during testing, then the PHP ini configuration has some additional directives to handle this.

display_errors = on

Configuration .htaccess To Display All PHP Errors

Developers usually have access to the directory files. The directive for showing PHP errors can also be enabled or disabled using the .htaccess file located in the root or public directory of the project.

php_flag display_startup_errors on
php_flag display_errors on

Recommended PHP Tutorials

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00