Jump to content

getting a var name as output ??


benjam

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.