Solved

PHP Function to Output Variable Information to Browser or File

Posted on 2007-03-19
5
220 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:calvinclose
5 Comments
 
LVL 1

Assisted Solution

by:micacca
micacca earned 150 total points
Comment Utility
Pretty simple

function OutputVar($var,$return=FALSE)
{
      if (is_array($var)) {
            $out = "\$variable is:<br>".print_r($var,true)."<br>";
            }
      else {
            $out = "\$variable is:<br>".$var."<br>";
            }

      if ($return)
            return $out;
      else
            echo $out;
}


must do the work you need
0
 
LVL 9

Assisted Solution

by:richdiesal
richdiesal earned 150 total points
Comment Utility
This might be what you're looking for:

<?
function OutputVar($var,$return)
{
      if (is_array($var)) {
            $output = "";
            foreach ($var as $value)
                  $output .= $value . ", ";
            $output = substr($output,0,strlen($output)-2);
      } else {
            $output = $var;
      }
      
      if ($return == TRUE)
            return $output;
      else echo $output . "<br>";
}
?>

Here is some test code:

<?
//begin test - function OutputVar must be defined before this code

$a = 1;
$b = array(1,2,3);
$value = OutputVar($a,TRUE);
echo "Variable = " . $value . "<br>";
OutputVar($a,FALSE);
$value = OutputVar($b,TRUE);
echo "Array = " . $value . "<br>";
OutputVar($b,FALSE);

?>

Which produces the following:

Variable = 1
1
Array = 1, 2, 3
1, 2, 3
0
 
LVL 9

Expert Comment

by:richdiesal
Comment Utility
Ahhh... micacca's print_r will work better if you have nested arrays.
0
 

Author Comment

by:calvinclose
Comment Utility
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.

0
 
LVL 48

Accepted Solution

by:
hernst42 earned 200 total points
Comment Utility
Calling OutputVar that way ist not possible as the called function does not know what the name of the variable is where the function was called. You will need to call the function like:
OutputVar('a', $a);
echo OutputVar('b', $b, false);

function OutputVar($varname, $varvalue, $print = true) {
  if (!$print) {
   return '$' . "$varname is:".print_r($varvalue, true);
  }
  echo  '$' . "$varname is:";
  print_r($varvalue);
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now