Types of PHP Errors
PHP Errors
In PHP, there are various possible error occurrences that may happen as you code or write your programs. These errors are categorized based on the time of occurrences and based on whether it is recoverable or not.
And then, this classification is made with respect to how it is triggered to send an error message to the browser. It might be triggered automatically while executing an improper line of code or triggered by the user by using trigger_error() function.
PHP errors will occur with some improper attempts with PHP scripts like, an invalid line of code execution, an infinite loop that causes default execution time elapse (30 seconds), and etc. Let us start with the major classification of PHP errors as follows.
- Fatal error
- Parse error
- Warning
- Notices
Fatal Error
This type of errors is uncaught exceptions that can not be recovered. When this error occurred, then it will stop the execution. Based on the time of occurrence, fatal errors are classified as,
- Startup fatal error – This will occur when the code cannot be executed with the PHP environment due to the fault that occurred at the time of installation.
- Compile time fatal error – This kind of error will occur when we attempt to use nonexistent data like file, class, function and etc.
- Run time fatal error – This will occur during execution. It is similar to compile time fatal error, except Compile time fatal error is generated by the Zend engine based on the time of occurrence.
Example: PHP Fatal Error
Let us call a nonexistent function fnSwap() in the following PHP program.
<?php
app_send_email();
echo "Sent Successfully!";
?>
This program will raise the following fatal error at the time of execution which will stop executing a further line that is the echo statement.
Fatal error: Call to undefined function app_send_email() in ... on line 1
Parse Error
Parse errors are generated only at compile time which is also called as a syntax error. If anything wrong with PHP syntax, for example, missing semi-colon for the end of the line, will trigger this type of errors to be displayed to the browser.
<?php
echo "content to be displayed to the browser!"
echo "<br/>embedding line break";
?>
This program sends parse error to the browser as follows due to the lack of semicolon(;) at the end of the line.
Parse error: syntax error, unexpected 'echo' (T_echo), expecting ',' or ';' in ... on line 3
Warning
Like fatal errors, PHP warning messages also created based on the three types of warning, that is, Startup warning, Compile time warning and Runtime warning. PHP will create warning message for sending them to the user without halting the execution. An example scenario for warning messages to be created is the divide by zero problem that is as shown in the following PHP program.
<?php
$count = 0;
$total = 200;
$result = $total / $count;
echo "RESULT: " . $result;
?>
In the above program, since $count has the value 0 and any number divided by zero is undefined, the line on which the division is made will create the following warning notices followed by the string returned by the echo statement with an empty value for $result variable. Meaning that, even after the occurrence of the warning error, the echo statement is executed.
Warning: Division by zero in ... on line 4
RESULT:
Notice
Like other PHP error messages, notice message can be created automatically or by the user by using the PHP trigger_error() function.It is used to send messages to the browser to make the user know about the problem of the code is any, which might cause an error.
For example, the following program starts with incrementing an uninitialized variable $result to print incremented value to the browser. Since $result is not initialized, it will automatically trigger the notice error on executing this script.
<?php
$result += 1;
echo "RESULT: " . $result;
?>
And the notice is,
Notice: Undefined variable: result in ... on line 2
RESULT: 1
But program execution will not be terminated because of this PHP notice. Rather, the notice message will be sent to the browser and the echo statement will print the incremented $result value subsequently.
Note:
- These are set of predefined error constants in PHP, like, E_ERROR, E_WARNING, E_NOTICE, E_PARSE and etc. Each of them is defined with integer value appropriately. For example, the integer value that is defined for E_ERROR is 1.
- These error constants are required to be specified with PHP configuration file (php.ini) to display various type of PHP errors while execution.
- On the other hand, we can override error reporting settings at run time by using the PHP error_reporting() function.
- Another alternative way of overriding error related directive setting on configuration file is, by enabling PHP flags with the .htaccess file. For example,
php_flag display_errors on
Screen Shots
Click on a photo below to scroll through the screen shots of the application!
Submit your Job or Project Today!
We can help you turn your idea into reality, take over your existing project, or extend your current development team.
Submit your idea job or project below and we will follow up with you shortly.
OUR OBJECTIVE
Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.
OUR MISSION
We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.