var_dump
in PHP, by default, prints the results of var_dump to the output in the same way that print
would. This prevents us from doing things like echo "Variable 'x' is: " . var_dump($x) . ".";
.
To prevent this, we can use the output buffer instead.
Usage
In place of var_dump(...)
, you simply use var_data(...)
.
$x = 2;
print "Using var_dump on 'x': " . var_dump($x) . ".<br/>";
print "Using var_data on 'x': " . var_data($x) . ".<br/>";
This will produce:
int(2) Using var_dump on 'x': .
Using var_data on 'x': int(2).
Code
function var_data($var) {
ob_start();
var_dump($var);
return ob_get_clean();
}