Jump to content

Archived

This topic is now archived and is closed to further replies.

benjam

getting a var name as output ??

Recommended Posts

I have a simple (yet [i]extremely[/i] 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:
[code]
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>';
    }
}
[/code]

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?

Share this post


Link to post
Share on other sites
If this is not possible, please let me know.

Share this post


Link to post
Share on other sites
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:
[code]<?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; }
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.