How to display errors in PHP, php display errors, how to fix php error, errors in php

How to display errors in PHP

For developers, errors are for day-to-day and are encountered every time we are doing any kind of development. Having a way to show these errors is the best way since we got the opportunity to identify the kind of error and therefore look for a way to fix it.

Every development language provides its own unique way of identifying and showing its error to the user. For example, in JavaScript, errors are shown in the console section in the browser.

In this article, we will discuss the way errors are displayed in PHP such that if you want to debug your product you can use it this way and check whether there is something you are not doing well.

As we all know, PHP gives us freedom while we are developing products and it is mostly referred to as a loose type programming language since it does not have a lot of rules either while dealing with variables or the structure of components.

If you are familiar with the PHP coding structure and what each element does, you are good to go, what becomes the problem is when dealing with errors. You can spend a lot of time while trying to identify what is wrong with a piece of code and you will not notice unless you understand how to display errors in PHP.

In systems that are already hosted in online servers, when PHP encounters an error it stops executing but mostly while using online IDES, the codes that have errors while typing are highlighted with an error sign like shown below.

How to display errors in php, php display errors

When you execute a code with errors, the browser will fail to handle the request and a message like one shown below will be displayed

How to display errors in php,php display errors

Unless you fix the error, your URL will not load the page content.

You will need to note that, the browser will display the pages is not working error but it will not tell you which error it is present in your code.

For errors that are as a result of not following the structure that PHP recommends, those are visible in most IDEs but the ones mostly from the database your code will not show any error unless you capture the error using a way that the browser will understand.

For example, you may want to display results from a table called users but in the process, you misspell the word users and write users which will not show any error.

How to display errors in PHP

For php you can display the errors as below

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

This code is added above your code that you think is producing the errors and it will fetch if there are possible errors which are not structural. For example, you have to fix all structural errors if you want the above code to work, that is, you cannot check whether the ; for closing a statement is not present. That error is shown by the editor.

To explain the above code

The ini_set function overrides the configuration found in your php.ini file that is if in the php.ini file display_error is turned off it will turn that on in the code.

The ini_set function also sets display_startup_errors to true to show the error message.

The error_reporting() is a PHP function that is used to display the errors. By setting it true it displays the error that occurs in the code.

The E_ALL shows all errors and warnings if present. Please note you can also use other scripts to show different kinds of errors. For example

E_NOTICE shows that the script found something that might be an error

E_WARNING is a nonfatal runtime error

E_ERROR which is a fatal runtime error

E_PARSE is a compile-time error and is generated by the parser

E_CORE_ERROR is a fatal error that occurs during the initial startup of the script

E_CORE_WARNING is a non-fatal error that occurred during the initial startup of thescript


Having added the above code, all errors as we have described above will be shown at the top of your browser and you can read the error which your code is having.

Displaying errors is the best way for a developer since it shows any kind of error and warning that the code may have.