Link to home
Start Free TrialLog in
Avatar of Calvin Close
Calvin CloseFlag for United States of America

asked on

PHP Function to Output Variable Information to Browser or File

I want to write a PHP function that outputs variable information to the browser or a string.

The function must handle single variables and arrays.  Currently, I use the following:

echo "\$variable is:<br>";
print_r($variable);
echo "<br>";

I want a function that you can pass the variable to it, and it will do the same.

For example, if $a=23, $b="A string", $c=array(2,3,4), then the function must be able to handle all of these.

OutputVar($var,$return=FALSE)
{

}

if $return is TRUE, the output is returned as a string.  Otherwise, it must output to the browser.  The string can then be outputted to a debug file if needed.

Thanks,

Calvin
SOLUTION
Avatar of micacca
micacca

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of richdiesal
richdiesal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahhh... micacca's print_r will work better if you have nested arrays.
Avatar of Calvin Close

ASKER

Actually, the above solutions didn't quite answer my question.

if $a = 1, then calling the function OutputVar($a) would output:

$a is:
1

I want the variable name shown, not some general thing like $variable.  micacca's solution works except for this point.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial