My Favorite Variable Dumping Tool

Everyone has a few functions that are their favorite, or one that they keep handy in their toolbox for different situations; this is mine: see_var().

What does see_var() do? It takes any variable that you throw at it and it gives you a pleasant readout of it’s value. For strings, integers, objects, and Boolean types, it gives you the visible readout of the containing variable, and lets you know the type. If the variable is an array, it gives you a “pretty” read out so you can easily follow the structure. The “<pre>” tags are in place to ensure the readout is pretty and will display wherever you want it to.

The parameters for see_var() are the variable you want to see in, and the name for the variable. The name is optional but is recommended that you use it for clarity if you are using several of these in a row. It first checks to see if the $name variable is NULL. If $name is not NULL, it outputs it to the browser window, or else it skips over it. Following the flow, it then determines whether or not $var is an array, if it is, it prints it out in a more readable format to the screen. All other variables are var_dumped to gain more insight into the variable specifics, such as type, or length.

Here is the function:

<?php

 /**
 * See what value a variable contains.
 *
 * @param mixed $var Variable you want insight to.
 * @param string $name Optional name to display for variable.
 **/
function see_var($var, $name = NULL) {
echo '<pre>';
    if(!is_null($name)) {
        echo $name;
    }
    if(is_array($var)) {
        print_r($var);
    } else {
        var_dump($var);
    }
    echo '</pre>';
}

?>

Here it is in action, notice I did not use the $name parameter in two of the demonstrations.

<?php  
// Class Cat //  
class Cat {
      public $legs = 4;      
      public $head = 1;
      public $tails = 1;
      public $ears = 2;
      public $speak = 'Meow'; 
}  

// Initialize a new object // 
$objectCat = new Cat();  

// create multi-dimensional array //
$ray = array(
          array($objectCat, 'mouse', 'dog', 'fish'),
          array('mom', 'dad', 'brother', 'sister'),
                'house', 'car'        
       ); 

// create a string //  
$string = 'We own a cat.';  

// create an integer // 
$number = 1;  


// see the variable $objectCat, name param given // 
see_var($objectCat, 'Cat');  

// see the variable $ray, no name param given // 
see_var($ray);  

// see the variable $string, no name param given // 
see_var($string);  

// see the variable $number, name param given // 
see_var($number, 'Number of cats');  

?>

And the output is:

cat:
object(Cat)[1]
  public 'legs' => int 4
  public 'head' => int 1
  public 'tails' => int 1
  public 'ears' => int 2
  public 'speak' => string 'Meow' (length=4)
Array
(
    [0] => Array
        (
            [0] => Cat Object
                (
                    [speak] => Meow
                    [legs] => 4
                    [head] => 1
                    [tails] => 1
                    [ears] => 2
                )

            [1] => mouse
            [2] => dog
            [3] => fish
        )

    [1] => Array
        (
            [0] => mom
            [1] => dad
            [2] => brother
            [3] => sister
        )

    [2] => house
    [3] => car
)
string 'We own a cat.' (length=13)
Number of cats:
int 1

If you need more insight into a multidimensional array, you just pass the key with the array; example: $array[0].

As you can see, this function is great for doing quick little tests and should not be used in production code. Feel free to play around with it.

This entry was posted in Development, PHP and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply