PHP

Debugging in PHP

In this tutorial, we are going to cover the concept of debugging in PHP and we will also talk about the tools that are particularly used to debug a PHP program let’s start the tutorial with a basic introduction first.

Introduction to Debugging

Debugging is just a way of finding and correcting errors or bugs in a PHP program. As we all know as a developer we face many complications while building an application and generating errors are one of them. 

Errors are the worst part of programming it also affects the working of an application, there are several types of errors that occur in a program like syntax errors, fetal errors, logical errors, etc. 

It often happens when we work on complex PHP projects and we have to write a bundle of lines of code, in this case, the chance of generating errors increases.

So the procedure of analyzing and solving the errors generated in a program is called debugging. 

While writing codes the most common mistakes we do is forget adding semi-colons at the end of each line of code, using an assignment operator(=) instead of using equal operators (==) in conditions, and a misspelling or using undefined variable names. 

Furthermore using single or double quotations in queries at the wrong place or using the isset function with an unclear concept brings big problems sometimes.

But these obvious errors can be solved effortlessly, the browser or console displays where, why, and which type of error is generating.

In PHP we have some built-in methods as well that we can use for debugging. The methods are explained below.

Debugging methods in PHP

var_dump() method

The var dump is a method that dumps the data of a variable. it shows the output value in a structural format.   

print_r() method

The print_r() function performs the same task the only difference is it shows the output value in a human-readable format.

get_defined_vars() method

This method is used to get all declared variables even though they are local, global or built-in. It returns all variables in the form of an array.

debug_zval_dump() method

This debug method is used in the case when we have a bunch of paths to update a single reference, technically it is used to dump a variable by its reference count.

debug_print_backtrace() method

This method is used to print a backtrace to see the current function calls.

debug_backtrace() method

The debug_bactrace() method is used to create a backtrace.

These methods are very useful when you want to print the data or output values of a variable. 

The above methods help a developer determine the mistake behind the occurrence of a particular error by outputting the data.

How to do Debugging in PHP with VS Code?


PHP provides the facility to perform debugging, There are various debugging tools available on the internet that can be used to debug a program and get rid of all the errors. These amazing extensions have the profiling capabilities that inform the developers about each generating error and also provide guidance on how to solve it, The most popular debugging extensions are listed below:

  1. Xdebug
  2. ZendDebugger
  3. phptrace
  4. phpdbg
  5. ltrace
  6. Inotify

You can choose your favorite one from the above list but I would like to recommend you the Xdebug extension.

Xdebug Extension

The debug extension is a PHP debugging tool that comes up with a bunch of amazing features that give an error-free application. the xdebug extension features include remote debugging, call logging functions, tracing, code coverage analysis, and profiling.

The xdebug extension requires an IDE that can understand different programming languages, we are going to use the VS (visual studio) code with it.

Let’s see the setup for the visual studio with xdebug extension.

Installing Xdebug extension with VS Code

Follow the steps explained below:

Step no: 1) install VS code in your system.

Step no: 2) access VS code and search for the PHP extensions.

Step no: 3) install the PHP debug Extension

Step no: 4) then click on the reload option to reload the VS code.

Step no: 5) install the Xdebug extension for VS code, make sure you are using the current version of PHP.

Step no: 6) put your installed version in the PHP/ext directory.

Step no:7) now if you want to use the extension configure PHP and then allow the remote debugging.

Add the following configuration in the php.ini file.

; set the extension path
zend_extension="C:/Program Files 
(x86)/PHP/v7.0/ext/php_xdebug-2.6.1-7.0-vc14-nts.dll"
; allow remote debugging
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

the above configuration will create a setup for the local Host PHP Server so you can effortlessly access the Xdebug extension.

You can connect the debugger to the port in the extension.

After finishing the VS code setup establish a connection with the Xdebug extension, you just have to perform some basic steps and then your attachment will be ready. You can use any IDE we are here taking the example of VS code, the configuration steps for every IDE will remain the same. 

Now let’s see the configuration of VS code following the steps explained below.

Configuration VS Code

After installing the Xdebug extension Make sure you have configured the VS code to connect with the debugger.

Then the remaining work will be done by itself you just have to perform some basic steps.

Step:1) convert the environment into debug mode

Step:2) from the appeared menu select the gear option.

Step:3) then select PHP, the VS code will produce the configuration automatically.

Step:4) now follow this step carefully, handling reloading of the PHP server is a quite difficult task. To deal with it I would suggest you install another extension from a different platform such as “PHP Server” this will help out to control the server.

Setp:5) the DBGP protocol is used by the Xdebug extension via port 9000. This server will connect the VS code with the Xdebug. Then we will use the TCP port feature of debugging server that will help contact the debugger.

After following all the above steps successfully next you have to do is connect a debugger.

Connecting Debugger

You can check a launch.json file in the projects root.vs code folder that was created by the PHP debug plugin for visual studio code.

The code in the file will look like this.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
 
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

The above JSON file will produce 2 configurations you can access them in the debug view.

Now we have two options, the first is either we connect with the current server and the second option is we can produce a new server with a new script. You can choose anyone, in our case a php.info file is already executing so will continue with it by selecting the listen for xdebug that will help to connect with the server. 

After building the connection a debug toolbar will appear. You don’t need to be confused almost every debugger contains the same functions such as start, stop, step, and restart the debugger.

Let’s move forward and see how to switch an error reporting level.

Switching Error Reporting Level

PHP has some methods to configure error reporting, you can access them in the php.ini file.

Conclusion

In this tutorial we talked about the debugging methods in PHP, first of all, we discussed what is debugging and how to perform it then we saw different debugging methods in PHP.

We also saw the list of top six debugging tools, we briefly discussed the Xdebug extension with the installation process as well. 

We saw the configuration procedure of VS code and learned how to connect a debugger. in the end, we saw the error reporting levels in PHP.

That’s all for today hope you like the tutorial if you have any queries regarding this tutorial so feel free to contact us below. Thanks for reading.

Suggested Article:

Leave a Comment