fb-pixel
Logo Supporthost

PHP errors: a complete guide

July 28, 2022 / Published in:  from Ivan Messina
No comments

PHP errors are more or less serious code errors that can start from a simple syntax error and end up with fatal errors that cause code execution to stop.

In this article, PHP errors: a complete guide, let’s look at what types of errors there are, and for each type of error let’s examine the specific examples.

After that, we will see several methods that allow us to display PHP errors.

Php Errors Guide

What are PHP errors?

When there is a syntax error or an undeclared variable or function it is called PHP code and an error occurs. Thus, we talk about PHP errors when there are problems within the code.

As we shall see in this article, PHP errors: a complete guide, there are several types of errors. Some may interrupt script execution, while others prevent us from getting the result we expect.

When we create a script in PHP, we can run into four types of errors. Let’s see what they are.

Types of PHP errors

Code errors in PHP can be divided into four categories: notice, warning, parse error, and fatal error.

Notice

When such an error occurs in the script code, script execution is not interrupted. An error notice is not a serious error, but this is used to indicate that an error may be present.

The most common case is when an attempt is made to call a variable that has not been defined.

When such an error occurs it will be indicated as “Notice error” or as “PHP Notice”.

Warning

PHP errors of the type “warning” are part of nonfatal errors. Again, as we have just seen for notice errors, the execution of the script is not interrupted.

This type of error can occur, for example, when incorrect parameters are passed to a function or when an attempt is made to call a file that is not present.

Such an error is identified as a “Warning error” or “PHP warning.”

Parse error

In PHP, parse errors are syntax errors, so it is an error in the code such as a typo, the absence of a semicolon or quotation marks.

PHP errors that belong to this category are referred to as “Parse errors” or as “PHP Parse errors.”

Fatal error

Fatal errors are critical errors that interrupt script execution. They can occur during initialization, compilation, or during code execution itself.

These errors are referred to as “Fatal error” or “PHP fatal error.”

PHP error examples

Now that we have take a look at the four main categories of PHP errors, let’s look at examples to help us identify the type of errors.

Php Error Example

Parse error

As we have seen, these errors can be generated during code writing. In most cases they can be due to a typo or an oversight.

Here is an example of a parse error:

<?php
//Simulate a "Parse error"
echo "Hello everyone \n"
echo "Welcome";
?>

The code above returns this error:

Parse error: syntax error, unexpected token "echo", expecting "," or ";" in [...] on line 4

In this case, we get this error message because in line 3, at the end of the echo “Hello everyone,” we forgot to insert the semicolon.

Warning error

Non-critical errors belongs to this category. Thus, in these cases, code execution is completed even if the error is encountered.

Let’s look at an example of a warning error:

<?php
//Simulate a "Warning error"
function sum($a,$b) {
return $a+$b;
}
include ("math_functions.php");
echo sum(7,3);
?>

This code returns the following warning:

Warning:  include(funzioni_matematiche.php): Failed to open stream: No such file or directory in [...] on line 6

Warning:  include(): Failed opening 'funzioni_matematiche.php' for inclusion (include_path='.:') in [...] on line 6
10

In this case, the program continues with the code execution, and as we see, the sum function is executed and the expected result (10) is returned to us.

Before returning the result, a Warning error warns us that the file math_functions.php is not present on the server.

Notice error

Like warning errors, notice errors are not fatal and do not interrupt code execution.

Here is an example of code that generates a notice error:

<?php
//Simulate a "Notice error"
function name_surname($nome,$cognome) {
    return "Nome: ".$name." Surname: ".$surname;
}
$n = "John";
$c = "Smith";
echo name_surname($n,$v);
?>

The result will be as follows:

Notice:  Undefined variable: v in [...] on line 8
Name: John Smith: 

In this case we called in the function name_surname the variable $v which had not been declared. We are shown the presence of the error (undefined variable) and the output is returned because the execution of the code continues.

As we can see, however, in this case the result we get is not as expected because only the “name” field is returned to us.

The type of errors, and therefore the type of warnings we get, may vary depending on the PHP version we are using.

In this case, in fact, in PHP 7 we get a Notice error. In PHP 8, however, this error has been reclassified as a Warning error.

For more details on the reclassification of errors from version 7 to 8 you can refer to the RFC.

Fatal error

Fatal errors are critical errors that can occur in several cases, for example, if we try to call a function that does not exist.

There is an example of a fatal error in this code:

<?php
//Simulate a "Fatal error"
function sum($a,$b) {
    return $a+$b;
}
echo multiply(7,3);
?>

Execution returns this error:

Fatal error:  Uncaught Error: Call to undefined function moltiplica() in [...]:7
Stack trace:
#0 {main}
  thrown in [...] on line 7

In this case we are attempting to call the multiply() function which has not been declared. Keep in mind that when such an error occurs, code execution is aborted.

How to show PHP errors

During development it is very easy to run into errors, even simply syntax errors. To be able to identify and fix PHP errors, it is necessary to understand what kind of errors they are and where they are in the code.

Debug Php Errors

Remember that error display should be activated only for debugging or development purposes. Remember, therefore, to turn it off once you are finished.

There are several ways to display PHP errors:

  • add code to the PHP file
  • add directives to the php.ini file
  • enable error display with .htaccess file
  • enable error display from cPanel
  • Consult the error log.

Let’s see how to show errors by following these methods.

Edit PHP script

One of the quickest ways to show PHP errors, is to add code directly into the script.

In this case we simply add these lines to the beginning of the PHP file:

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

The display_errors directive allows us to enable or disable the display of errors.

By enabling display_startup_errors, we can also display errors encountered during initialization.

With the “error_reporting” function we can specify which errors to display. The parameter “E_ALL” allows us to display all errors.

If we wanted to exclude Notice errors instead, we would simply use this parameter:

E_ALL & ~E_NOTICE

Be careful, because with this system errors that cause script execution to stop, such as parse and fatal error, are not shown. In this case you have to consult the log files to see the errors.

Edit the php.ini file

To show PHP errors we just need to edit the php.ini file.

Keep in mind that some providers do not allow you to edit the php.ini file.

With all our plans from shared hosting to dedicated services such as VPS cloud hosting and dedicated servers, you can change the PHP version and edit the file php.ini.

If you want to put the service to the test, take advantage of our free hosting for 14 days and do all the testing you want with a trial plan.

As we will see in the next section of this article, PHP errors: a complete guide, you can also enable the display of errors directly from cPanel, enabling the settings without having to edit the code at all.

In this case, let’s see what directives you need to add to the php.ini file to enable error display:

display_errors = on
error_reporting = E_ALL

With these directives all errors will be shown on the screen.

Edit the .htaccess file

We can also enable error display through the .htaccess file. In this case we just need to add the following lines:

php_flag display_errors on
php_flag display_startup_errors on

Enable errors from cPanel

From cPanel, you can enable the display of code errors on the screen or consult the error log.

Keep in mind that for an online site, it is preferable to consult the error log.

If, however, the site or application is in development, you may need to enable screen display.

Let’s see how to do it.

Consult the error log

On our plans, the error log is active by default. If you want to check for code errors, you can look at the error_log file located in the directory where the error was encountered.

This file is automatically generated by the system when a code error occurs. You can simply use the file manager of cPanel to locate the file and view it.

In this example, you can see the error_log file that stored fatal and notice PHP errors from the examples I showed you earlier.

Php Error Logs

View PHP errors on screen

On our hosting plans, by default option the management of the PHP version is entrusted to Cloudlinux. In this case, therefore, you can use the options of the Select PHP Version tool to enable the display of errors on the screen.

Cpanel Select Php Version Settings

From the Options tab you can turn on the diplay_errors option as you see in this screenshot.

Cpanel Diplay Errors Php Selector
In this screen shot the display_errors option is disabled

Remember to turn off the option before publishing the site or after you have fixed the errors.

Conclusion

We have seen that there are different types of PHP errors and that in some cases they can break the execution of scripts.

In addition to being able to consult error logs, there are also several ways to enable the display of code errors on the screen. This system helps us for debugging purposes and allows us to identify errors during development.

Did you know about PHP error classification? Have you ever enabled error display to solve a problem? Let me know in he comments below.

Related posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.