How to debug PHP

Introduction

When a coder writes a program, he/she can sometimes miss a syntax or something else can go wrong. In this situation the easiest way to fix the program is to check the execution line by line. This process is called debugging a code. PHP provides an extension called XDebug which can be used in various editors to enable the debugging mode. In this article we will see what other ways we can use to achieve debugging and fix our code easily.

How to debug PHP

To fix a PHP issue, you need to do two things:

  1. Get the issue based upon your analysis
  2. Perform changes in the code and retry the execution

Well this can be easily done in Chrome directly, and this is useful in small use-case. But in an actual project, you might need to attach a debugger which will stop you at a required breaking point and help you to go through the code step by step.

How to debug PHP in Chrome

To debug PHP in Chrome, you need to use client side programing to call the PHP e.g. via AJAX and catch both success and error responses. In sources, add a breakpoint as shown below:

debug PHP in Chrome

In the response you will get the error, like in above scenario. You can just take the response out via “alert”, as shown below:

Error in PHP

Also, you can check the response tab within Network for your php file:

common errors in PHP

Once you get the error then you need to check it, google it and fix it. There are many types of common errors like:

  • Parse error or Syntax Error
  • Fatal Error
  • Warning Errors
  • Notice Error

The solution of these errors are discussed here.

Debugging PHP with XDebug and Editors

All the editors utilize XDebug to integrate the debugging for PHP, the implementation of the same is linked below:

Leave a comment

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