Jump to content


Photo

getting a var name as output ??


  • Please log in to reply
4 replies to this topic

#1 benjam

benjam
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts
  • LocationSalt Lake City, UT

Posted 14 April 2006 - 08:05 PM

I have a simple (yet extremely useful) little debug tool that outputs a var or line of asterisks whenever it is called and was wondering if there is a way to get the var name in the output as well.

Here is the current function:
function call($var = 'NULLNULL')
{
    if ('NULLNULL' === $var)
    {
        echo '<div style="font-size:large;font-weight:bold;color:red;background:white;">*****</div>';
    }
    else
    {
        echo '<pre style="font-size:large;color:black;background:white;">';

        if (is_bool($var) || is_null($var))
        {
            var_dump($var);
        }
        else
        {
            print_r($var);
        }

        echo '</pre>';
    }
}

What I would like it to do, is when I call it with the following:
$my_var = 'This is My Variable';
call($my_var);

this is what it would output now:
This is My Variable

I would like it to output somthing like:
$my_var = This is My Variable

Is this possible? How?
Windows 7
MySQL: 5.1.36-community
PHP: 5.3.2 VC9
Apache 2.2 (apachelounge VC9)

#2 benjam

benjam
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts
  • LocationSalt Lake City, UT

Posted 17 April 2006 - 07:59 PM

If this is not possible, please let me know.
Windows 7
MySQL: 5.1.36-community
PHP: 5.3.2 VC9
Apache 2.2 (apachelounge VC9)

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 April 2006 - 08:31 PM

You can loop throught the $GLOBALS array looking for the value that was passed. If you have more than one variable defined with the same value, you will get the first variable name.

Here's an example:
<?php
$test1 = 'this is a test';
$test2 = 'this is another test';
$test3 = $test1;

call($test1);
call($test2);
call($test3);

function call($var='NULLNULL')
{
    if ($var != 'NULLNULL')
        foreach($GLOBALS as $k => $v)
            if ($v == $var) {echo $k . '=' . $var . '<br>'; return; }
}
?>

Ken

#4 benjam

benjam
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts
  • LocationSalt Lake City, UT

Posted 20 April 2006 - 08:14 PM

I actually thought of that, and when developing, I use the call() function quite a lot, and I would rather not run though the GLOBALS array everytime I make a call to call().

I also thought of the fact that it might not return the right variable name. Which is also not the desired effect.

But if this is the only way... I may just have to use it.

I was hoping there would be another way that I had not thought of.
Windows 7
MySQL: 5.1.36-community
PHP: 5.3.2 VC9
Apache 2.2 (apachelounge VC9)

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 April 2006 - 09:29 PM

Take a look at the [a href=\"http://www.php.net/array_search\" target=\"_blank\"]array_search()[/a] and [a href=\"http://www.php.net/array_keys\" target=\"_blank\"]array_keys()[/a] functions. These may help you.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users