Solved

PHP Function to Output Variable Information to Browser or File

Posted on 2007-03-19
5
226 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
ID: 18751509
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
ID: 18751529
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
ID: 18751542
Ahhh... micacca's print_r will work better if you have nested arrays.
0
 

Author Comment

by:calvinclose
ID: 18751865
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
ID: 18754672
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exporting image to excel from php document 10 28
Why do people dis php? 5 50
php convert date format 3 22
I wonder how people fake their ip address? 3 40
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

810 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