Menu

Debugging Varia-bug-les


Note from the author on February, 5 2015:

I have updated my class to give a better description of the variable that is being processed and to add more functionality. I have added several ‘if’ statements to reveal the type of input with description. Also, I have included a ‘die’ statement that can be triggered if you just want to see your output before the html is passed to your browser. I hope you all enjoy the upgrade!

When debugging code, sometimes tracking down your variables can be a bit arduous. Sometimes you just want to see what elements are being stored in a variable – especially if it is returned from a database or created from a function. Using the print_r function in PHP is a great tool. It does not, however, always give you the easiest to read results. Welcome a little HTML tag called <pre> that brings us great joy! For those of you unfamiliar with this little gem it is defined as so from w3schools.com:

“Text in a <pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks… Use the <pre> element when displaying text with unusual formatting, or some sort of computer code.”

When used together with the print_r function in PHP we can see an easy to read display of our array elements. If we have an array like this example:

$test_array = array('1' => 'uno', '2' => 'tree', '3' => 'tword');

We can see the contents by writing a little code block in our code as such:

echo '<pre>';
print_r($array);
echo '</pre>';

Which gives us a really nice output of our keys and values:

Array (
  [1] => uno
  [2] => tree
  [3] => tword
)

However, if your array has many and more elements, or it is a multi-dimensional array, the results are a garbled mess. Also, typing the above block of code can get a little tedious, especially if we are trying to track down a hard to find bug in our code, or we have to look at several arrays in order to track where, or when, we loose – or do not achieve – our desired variable result. I wrote this simple little class to help out with the doing less typing thing. I have been improving upon it for some time now. I have recently upgraded to a more complete output for not only arrays, but also strings, objects, booleans, digits, and even an unknown. I hope you find this as handy as I do:

<?php

/**
 * A simple function to print_r variable data while debugging
 * To use in your code simply type:
 * myprintr::vrbl('variable name', $variable);
 *
 * @Author: Dennis M. Barber
 * @Version: 1.0.6
 */

class myprintr
{
 private static
 $sTitle = '',
 $vVar = '';

 /**
 * Input method for class. Returns the type and output of the input.
 *
 * @param string $sTitle A name for your input
 * @param variable $vVar The input you want to check
 * @param bool $bDie A way to kill the output right after learning your input type
 */
 public static function vrbl($sTitle, $vVar, $bDie = false)
 {
 if(!empty($sTitle))
 self::$sTitle = $sTitle;

 if(!empty($vVar))
 self::$vVar = $vVar;

 $sPrint = self::DetermineOutput();

 print $sPrint;

 if($bDie)
 die('-- An exit command has been issued by the myprintr class --');
 }

 /**
 * This method is used to determine the type and contents of the input
 *
 * @return string Returns the type and contents of the input
 */
 private static function DetermineOutput()
 {
 $sReturn = "<strong>".self::$sTitle;
 $sPrint = '';
 if(isset(self::$vVar))
 {
 if(is_string(self::$vVar))
 {
 $sReturn .= " is an string with ".strlen(self::$vVar)." characters:</strong><br>";
 $sPrint = self::$vVar;
 }
 elseif(is_array(self::$vVar))
 {
 $sReturn .= " is an array:</strong><br>";
 $sPrint = print_r(self::$vVar, true);
 }
 elseif(is_object(self::$vVar))
 {
 $sReturn .= " is an object:</strong><br>";
 $sPrint = self::VarDump(self::$vVar);
 }
 elseif(is_bool(self::$vVar))
 {
 $sReturn .= " is a boolean value:</strong><br>";
 $sPrint = self::VarDump(self::$vVar);
 }
 elseif(is_numeric(self::$vVar))
 {
 $sReturn .= " is a number or numeric string value:</strong><br>";
 $sPrint = self::VarDump(self::$vVar);
 }
 else
 {
 $sReturn .= " is unknown.</strong><br>";
 $sPrint = "The variable is empty or could not be determined. No data to display.";
 }
 }
 else
 {
 $sReturn .= " is not set. Please check your input!</strong>";
 }

 $sReturn .= '<pre>'.$sPrint.'</pre>';

 return $sReturn;
 }

 /**
 * Return the var_dump of the input
 *
 * @param input $vVar the input - could be anything: variable, array, object, string, etc.
 */
 private static function VarDump($vVar)
 {
 $sPrint = '';
 ob_start();
 var_dump($vVar);
 $sPrint = ob_get_clean();
 return $sPrint;
 }
}

As the only method in this class is static, we do not even need to instantiate the class, simply use and ‘include_once’ call at the top of your page, or where ever you include your classes. Then to see the output all we have to do is add

myprintr::vrbl("Test Array", $test_array);

to our code and it give us this output:

Test Array is an array:
Array (
  [1] => uno
  [2] => tree
  [3] => tword
)

You can simply copy the above class and paste it into your own PHP file, and use it where ever you would like. For even easier reading you could nest it in a div that is “fixed” to the top of your browser window. Alas, that is a post of another day. Have fun using the code!

Leave a Reply