132 Views

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.

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

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

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

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:

 

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

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.

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