Displays the name of a variable and its value for a variable in the global area
Yes, there is, but you will need to specify a name instead:
function debug($var_name) { printf('%s value is %s', $var_name, var_export($GLOBALS[$var_name], true)); }
or, if you only want a value without formatting with parsing:
function debug($var_name) { printf('%s value is %s', $var_name, $GLOBALS[$var_name]); }
Display the variable name and its value for the variable in the local area
Note: this only works for variables in the global scope. To do the same for the local area, you probably need a solution using get_defined_vars() , for example:
printf('%s value is %s', $var_name, get_defined_vars()[$var_name]);
It cannot be simply wrapped in a debug() function. This is because get_defined_vars() returns an array representing the variables in the area where get_defined_vars() is get_defined_vars() , and we donβt need the area where debug() defined, right?
Single solution
A unified solution can use the global scope by default, but also accept some array representing the local scope, so the definition could be:
function debug($var_name, $scope_vars=null) { if ($scope_vars === null) { $scope_vars = $GLOBALS; }; printf('%s value is %s', $var_name, var_export($scope_vars[$var_name], true)); }
and then you can call it globally:
debug('myvar');
or similarly in a local area, passing in a local array of areas:
debug('myvar', get_defined_vars());
Working example
For a working example, see this demo: http://ideone.com/NOtn6
Does it help?