PHP Interview Question Answers : How to debug in PHP

We will discuss best practices to troubleshoot and debug PHP script. One of the very common interview questions we have seen is – How to debug in PHP or How would you troubleshoot your PHP code. 

Debugging PHP can often be tricky. Various flavors of PHP e.g. PHP 5.3 vs. PHP 5.4 can throw different warnings or run-time errors depends upon the type of code written. In addition, programmers are not able to change or update PHP code on the fly using tool such as chrome debugger; which makes PHP debugging very tricky and time consuming.

We will discuss best practices as to how you can troubleshoot PHP code.

(1) Write meaningful PHP Code

Rule of thumb is to write proper code and build meaningful logic. Many times programmers make code extremely tough to debug by write not so meaningful code.

If you write logical code with aligned variables, you are making code programmer friendly, easy to read, and of course easy to debug. So keep reminding yourself while you write code, to write quality code, instead of just to make things work.

(2) Turn display_error ON

One common issue that I have heard from PHP programmers who are new is – “I’m getting no errors”.

This is normally caused by a syntax errors left in the code by programmers. To investigate this properly, you should turn display_errors to ON. This can be done either in your php.ini file or at the head of your code in your PHP file using code below.

Sample Code

<?php
ini_set('display_errors', 'On');
?>

Next, you will need to set an error reporting level. PHP 4 and PHP 5 do not show PHP notices by default that are very important in debugging your code. Notices are generated by PHP whether they are displayed or not, so deploying code with may impact as an overhead.

Make sure notices are displayed properly, set your error reporting level either in php.ini or in your PHP file.

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL); ?>

(3) Use var_dump() to excel your debugging

var_dump() is a function which is used to display structured information, along with type and value, for one or more variables within your PHP code.

Basically var_dump() –dumps the detailed information and run time value assigned to the variables. That is why it’s very useful to retrieve value for variable at each stage.

Let’s take a look at the sample.

<?php

$val = 10.5;
$flag_bool = true;
var_dump($val, $flag_bool);

?>

For the sample code above the output will be :
float(10.5)
bool(true)

As you can see it returns the type of variables float, boolean and followed by their values. In PHP debugging var_dump() function is a really powerful way to troubleshoot.

(4) Use print_r() or echo() to make your debugging easy at runtime

print_r() will Print information about a variable in a human-readable way. As an example array values will be presented in a format which will show keys and elements. Also similar notation is used for objects.

echo() Outputs one or more strings separated by commas. It does not return any value where as print_r() returns 1.

These are some very common debugging techniques in PHP. It is not as straightforward as front-end technologies such as HTML or JavaScript or CSS.

Comments are closed.