Debugging Made Easy for PHP Developers

8:56 AM Unknown 0 Comments


For someone, with Java background it is quite irritating to notice that you cant display values to console while using PHP. Debugging in php is usually done using the built in php function error_log. This built in function assumes the following parameters.

error_log(error, type, destination);

The parameters are discussed as follows:

1. error: The error message to log
2. type: Specifies the error log type. The type assumes one of the following values:
    0: error is sent to the server error logging system.
    1: the error is sent via email specified in the destination parameter.
    2: the error is sent through the PHP debugging connections. (only available in PHP3)
    3. the error is sent to the file specified in the destination parameter.

Recently I have been getting many questions about how to log (program output/errors) locally. Hence I have decided to prepare a short code showing how to and add it in this blog.

Basically what I have done is create a small wrapper class that uses the built in php error_log function.
Assume we have the following folder structure:

project-root/lib/PHPDebug.php

N.B. project-root is the folder of your project. For instance, if you are creating a payrolapp then the project-roo will be something like

payrolapp/lib/PHPDebug.php

And if you have a /pages folder where you put your php page file

payrolapp/pages/....

So what we are trying to do here is, we are trying to access the PHPDebug class from one of the pages we have under the /pages folder.

Assume we have just created a ShowAllPayrolDataForEmployees.php page under the pages folder, and we opened that file, the code's content will look something like

<?php
.....
.....
require_once('../lib/PHPDebug.php');//go up one step and look for a file called PHPDebug.php in the //lib folder

$netIncome = $grossPymt - $deductions;
.....
//now to print out the value of $netIncome variable, you could use something like
PHPDebug::printLogText("Net Income : ".$netIncome,"../lib/debug.txt");
//here we are calling the static method printLogText of class PHPDebug. The //printLogText(String,String) takes two parameters.
//parameter 1. Is the error message you want to be printed in the error log file
//parameter 2. Is the location and name of the file
?>

And the content of the PHPDebug.php looks like:


<?php
    class PHPDebug{
        public static function printLogText($text,$fileName){          
            if (file_exists($fileName)){
                error_log($text,3,$fileName);
            }else{
                error_log("Error locating the file [$fileName]",3,$fileName);
            }
        }
    }//end class
?>

Before using the printLogText(string,string) method, make sure you have created the file in the location you want it to be.

In the above example, we assume we have created the file 'debug.txt' under the /lib folder. So when calling the method, we always pass the file where the log is going to be stored.

N.B. You can have multiple files in different folders and by just passing the exact file location and name, you can re-use the same method over and over aging to print out the log values.

This method will make sure if the file exist in the specified location and will open and save some values to it if only it exists. Otherwise, it should not break your code even if the file does not exist.

This is a very simple and local remedy to help you while debugging in php. Hope it will be of some help for some one out there.

0 comments: