Solved

PHP Function to Output Variable Information to Browser or File

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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