Types of Error in PHP

WBOY
Release: 2024-08-29 12:57:05
Original
456 people have browsed it

The event of the occurrence of deviation of the result from the accurate result is termed as an Error. In PHP, error can be generated because of the usage of an incorrect format of coding or implementation of non-feasible functionality. Based on the root cause and level of severity, errors in PHP are categorized in 4 types, such as:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  1. Syntax error (Parse error)
  2. Warning Error
  3. Notice error
  4. Fatal error

Types of Errors in PHP

Lets discuss the Types of Error in PHP.

Types of Error in PHP

1. Syntax Error (Parse Error)

In PHP, the scripting needs to follow standard grammar to develop an executable code. When the written code syntax gets deviated from the standard, syntax error takes place. It is also called as parse error. This error gets checked in the compilation stage itself and execution of the code gets stopped. It does not allow the execution unless the error is not fixed and compilation is completed without any syntax flaw. The error constant that is used to represent compile time parse (syntax) error: E_PARSE

Example:

The below code snippet is developed to assign values to PHP variables and display the stores values on the output window.

<?php
$Correct_Var = "Writing a code to demonstrate Syntax(Parse Error)";
Incorrect_Var = "The '$' symbol is missing for variable y!!!";
echo $Correct_Var;
echo Incorrect_Var;
?>
Copy after login

Output:

PHP compiler understand existence of any variable when a string is associated with $ symbol. In the above code, definition of variable Incorrect_Var does not satisfy the grammar, hence the compiler throws syntax error for the code and execution is interrupted.

Types of Error in PHP

2. Warning Error

This error arises when the PHP script is trying to process any invalid information such as trying to perform a file operation on a file which does not exist or try to call a function with number of input values i.e. different from number of arguments present in the calling function definition. These are serious errors but does not stop the execution of the program and ends in exhibiting unexpected result. The error constant that is used to represent run time warning without terminating script execution: E_WARNING

Example:

The below code snippet is written to call another script file within the current programming.

<?php
echo "Beginning of program execution";
echo "<br>";
echo "<br>";
$Correct_Var = "Writing a code to demonstrate Warning Error";
echo $Correct_Var;
echo "<br>";
echo "<br>";
include ("MissingScript.php"); //Calling the script file which is not available
echo "Ending of program execution";
?>
Copy after login

Output:

According to the programming, compiler successfully compiled to code and starts execution. The execution continues sequentially. For the command include (“MissingScript.php”), it is looking for the script in the default path …/usr/share/php and does not found any script with the given name. Thus it ends in resulting the warning message for that specific command and execution the rest of the code as designed.

Types of Error in PHP

3. Notice Error

This error is encountered in PHP when there is any invalid coding has been developed in the script. This is categorized as non-critical error which does not stop the execution, ends in resulting an error message. The error constant that is used to represent Run time notice message, resulting because of present of invalid code: E_NOTICE

Example:

<?php
echo "Beginning of program execution";
echo "<br>";
echo "<br>";
$Correct_Var = "Writing a code to demonstrate Notice Error";
echo $InCorrect_Var; //Try to display value stored in an undefined variable
echo "<br>";
echo "<br>";
echo "Ending of program execution";
?>
Copy after login

Output:

The compiler does not recognize the variable $InCorrect_Var as it is not defined in the code. Hence it throws the Notice error.

Types of Error in PHP

4. Fatal Error

A compile time error that is encountered due to any invalid command such as missing of function definition for a calling function, is coined as fatal error. Severity level of this type of error is critical and hence it does not let the execution to be proceed and throw fatal error message as output. The error constant that is used to represent the fatal error which triggers script termination: E_ERROR

Example:

The below code snippet is designed to call demonstrate application of function in PHP scripting.

<?php
echo "Beginning of program execution";
echo "<br>";
echo "<br>";
$Correct_Var = "Writing a code to demonstrate Fatal Error";
echo $Correct_Var;
echo "<br>";
echo "<br>";
UndefinedFunction();//Calling a function which is not defined in the script
echo "Ending of program execution";
?>
Copy after login

Output:

As the code is developed following the correct coding grammar, it does not catch any error during compilation. In the execution phase it cannot decode the command to call the function UndefinedFunction(), as it is not defined in the scope of the program. Hence it results in throwing the fatal error message and execution of the program is halted.

Types of Error in PHP

Additional Note

1. Error handling is easy in PHP. If any developer does not have access to the complete code for any application, it is recommended to use error handling functions in possible scenarios.

2. In order to avoid new error in the PHP programming, developer is expected to follow proper coding guidelines and stays alert towards probabilities of various types of errors, warnings and notices.

3. It is recommended not to allow any error or warning or notice to be displayed to the user. Hence the best practice for any safe PHP programming to ensure the required configuration to be available in php.ini file.

The desired value for the below variables are:

error_reporting as ' E_ALL'
display_errors as 'Off'
log_errors as 'On'
Copy after login

The below code can be included in any PHP script to configure the desired values in the php.ini file:

error_reporting(E_ALL);
ini_set('display_errors','0');
ini_set('log_errors','1');
Copy after login

4. PHP incorporates the feature to enable developer to write own customized error handling functions.
This function needs to be designed with some specific guidelines as follows:

Function should be capable of handling minimum of two input parameters: error message and error level and maximum of 5 input parameters by including the optional parameters such as line number, file and error context.

The above is the detailed content of Types of Error in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!