Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Function to Output Variable Information to Browser or File

Posted on 2007-03-19
5
Medium Priority
?
235 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:Calvin Close
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Assisted Solution

by:micacca
micacca earned 450 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 450 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:Calvin Close
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 600 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

721 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