A Variables Dump
If you program in PHP, you know it is a fairly common need to peek into variables and see what they contain. It can be especially needful when one is chasing a bug.
If you have programmed with PHP for a long time, you no doubt have developed ways to view the content of variables without affecting the flow of the software's execution.
Here is another way, or perhaps a way you are already using. This method publishes the variable name, its length, and its content on the web page. The dumped information can be viewed on the web page where the PHP is running.
Pop this function somewhere into your PHP code. It is then ready to use.
function vardump($mixed=null)
{
ob_start();
var_dump($mixed);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
To use the function, call it with a variable name. vardump($xyz) is an example for dumping the $xyz variable.
If you wish to have the output formatted, put it between pre tags. Example:
echo '<pre>'.vardump($xyz).'</pre>';
That's the entire how-to.
The rest of this article is mostly examples for various types of variables.
In the examples, values are assigned to variables. A call is made to the vardump() function. The function's output is then placed immediately underneath for easier assimilation.
<?php // A string. $xyz = 'This is an example'; echo '<pre>'.vardump($xyz).'</pre>'; /* string(18) "This is an example" ($xyz contains 18 characters and its value is "This is an example") */ ?>
<?php // An integer. $xyz = 123; echo '<pre>'.vardump($xyz).'</pre>'; /* int(123) ($xyz contains an integer and its value is 123) */ ?>
<?php // A floating point number. $xyz = 456.78; echo '<pre>'.vardump($xyz).'</pre>'; /* float(456.78) ($xyz contains a number with a floating decimal point and its value is 456.78) */ ?>
<?php
// An array.
$xyz = array('a','b',321,'see','',87.654,'hi:-)','there');
echo '<pre>'.vardump($xyz).'</pre>';
/*
array(8) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
int(321)
[3]=>
string(3) "see"
[4]=>
string(0) ""
[5]=>
float(87.654)
[6]=>
string(5) "hi:-)"
[7]=>
string(5) "there"
}
(
$xyz is an array composed of 8 elements:
[0] contains 1 character and its value is 'a'
[1] contains 1 character and its value is 'b'
[2] contains an integer and its value is 321
[3] contains 3 characters and its value is 'see'
[4] contains 0 characters and it has no value
[5] contains number with a floating decimal point and its value is 87.654
[6] contains 5 characters and its value is 'hi:-)'
[7] contains 5 characters and its value is 'there'
)
*/
?>
<?php
// An array within an array.
$xyz = array('a','b',321,array('food'=>'fish','subjects'=>'people'),'hello');
echo '<pre>'.vardump($xyz).'</pre>';
/*
array(5) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
int(321)
[3]=>
array(2) {
["food"]=>
string(4) "fish"
["subjects"]=>
string(6) "people"
}
[4]=>
string(5) "hello"
}
(
$xyz is an array composed of 5 elements with one of the elements being an array composed of 2 elements:
[0] contains 1 character and its value is 'a'
[1] contains 1 character and its value is 'b'
[2] contains an integer and its value is 321
[3] contains an array composed of 2 elements.
[3]['food'] contains 4 characters and its value is 'fish';
[3]['subjects'] contains 6 characters and its value is 'people';
[4] contains 5 characters and its value is 'hello'
)
*/
?>
As you can see, vardump() will dump any variable.
It is a wonderful and handy tool for PHP programmers.
(This content first appeared in Possibilities newsletter.)
Will Bontrager

