Skip to content

Instantly share code, notes, and snippets.

@muratpurc
Created September 22, 2011 10:23
Show Gist options
  • Save muratpurc/1234489 to your computer and use it in GitHub Desktop.
Save muratpurc/1234489 to your computer and use it in GitHub Desktop.
PHP: Debug content of passed variable
/**
* Dumps the content of passed variable and returns the result.
*
* @param mixed $var The variable
* @param string $label Additional text to describe the variable
* @param bool $formatted Flag to return formated information
* return string
*/
function mp_dumpVar($var, $label = '', $formatted = true) {
$dump = ($label !== '') ? $label . ":\n" : '';
if (is_object($var) || is_array($var)) {
$dump .= print_r($var, true);
} elseif (is_bool($var)) {
$dump .= ($var == true) ? '(bool) true' : '(bool) false';
} elseif (is_null($var)) {
$dump .= '(is_null)';
} elseif (is_string($var) && empty($var)) {
$dump .= '(is_empty)';
} else {
$dump .= $var;
}
if ($formatted) {
return '<pre>' . $dump . '</pre>';
} else {
return $dump;
}
}
################################################################################
## Example
$foobar = null;
echo mp_dumpVar($foobar, '$foobar');
echo mp_dumpVar($_SERVER, '$_SERVER');
class Person {
protected $_firstname;
protected $_lastname;
public function __construct($firstname, $lastname) {
$this->_firstname = $firstname;
$this->_lastname = $lastname;
}
}
$johnDoe = new Person('John', 'Doe');
echo mp_dumpVar($johnDoe, '$johnDoe');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment