How to make custom debugging print function in PHP

by  
Share:
How to make custom debugging print function in PHP

How many times we developers get stuck at a certain section debugging code, praying to GOD that “this time my code compiles without error“. Yet most of the times the error is quite silly and at the end we either end up either laughing or cursing our self for the mistake. I have been in the situation many times debugging the code, changing my printing function from echo to print to print_r, to var_dump based on the output. So I decided to write a custom debugging print function myself that will help me save my time and energy from unnecessary changing of codes.

View Demo

What does this custom debugging print function do?

My primary goal was to write a custom debugging print function which print/outputs any variable irrespective of its type. Which helps me to see every variable type and value. So this save me from banging my head when I get stuck at some section just because some variable has null value or any other similar problems.

Take an example how I have to debug earlier.

<?php
$var1 = 10 +20;
echo $var1;

// To check the output of an array
$arr = array(10,20,25,36,78);
print_r($arr);

// if boolean value I use echo but false value is not displayed to screen
$status = true;
echo $status;

// For testing boolean value I use var_dump
$status = false;
var_dump($status);

?>

How I debug now with my custom debugging print function debugResults().
<?php
$var1 = 10 +20;
debugResults($var1);
debugResults($var1, true);

$arr = array(10,20,25,36,78);
debugResults($arr, true);

$status = true;
debugResults($status);

$status = false;
debugResults($status, true);

?>

Lets look how I have coded this function  debugResults().
<?php
function debugResults($var, $strict = false) {
	if ($var != NULL) {
		if ($strict == false) {

			if( is_array($var) ||  is_object($var) ) {
				echo "<pre>";print_r($var);echo "</pre>";
			} else {
				echo $var;
			}

		} else {

			if( is_array($var) ||  is_object($var) ) {
				echo "<pre>";var_dump($var);echo "</pre>";
			} else {
				var_dump($var) ;
			}

		}

	} else {
		var_dump($var) ;
	}
}
?>

Keep these points in mind while using the function debugResults().
  • The function accepts two parameters, first its the variable to debug/output the results, seconds is boolean(true, false) type to output variable (with or without its variable type).
  • The second argument is optional, if you want to print the variable along with its type then you can send “true”. By default the value is false.
  • Don’t use any printing function with this function. Like echo debugResults(“test”).

Let’s test some of the output with this function.

<?php
debugResults("");echo "<br>";
debugResults("David");echo "<br>";
debugResults("David", true);echo "<br>";
debugResults(10);echo "<br>";
debugResults(10, true);echo "<br>";
debugResults(false);echo "<br>";
debugResults(true, true);echo "<br>";
debugResults(array("a"=>10, "b"=>20, "c"=>30));echo "<br>";
debugResults(array("a"=>10, "b"=>20, "c"=>30), true);echo "<br>";
?>

View Demo
Download
I hope this function will help you too.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Leave a Reply

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