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
Solved

PHP Function to Output Variable Information to Browser or File

Posted on 2007-03-19
5
227 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
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 …

766 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