开发者

Get the name of a variable passed into a PHP function?

I just threw this together to help in debugging some PHP scripts. As you can see, it is sloppy but I am going to improve it some more.

My debug function has 2 variables passed in, a variable name and a variable value.

Is it possible to just pass in the variable and somehow get the name of the variable without manually doing it like I have it set now?

The Function

<?php
function debug($varname, $var)
{
    echo '<br>' . $varname;

    // $var is a STRING
    if (is_string($var)) {
        echo ' (string) = ' . $var . '<br>';

    // $var is an ARRAY
    } elseif (is_array($var)) {
        echo ' (array) = <pre>';
        print_r($var);
        echo '</pre><br>';

    // $var is an INT
    } elseif (is_int($var)) {
        echo ' (int) = ' . $var . '<br>';

    // $var is an OBJECT
    } elseif (is_object($var)) {
        echo ' (object) = <pre>';
        var_dump($var);
        echo '</pre><br>';
    }
}

The Test

$testString = 'just a test!';
$testArray = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
    );
$testInt = 1234567890;

$testObject = new stdClass;
$testObject->someVar1 = 'testing123';
$testObject->someVar2 = '321gnitset';

debug('$testString', $testString);
debug('$testArray', $testArray);
debug('$testInt', $testInt);
debug('$testObject', $testObject);
?>

The Result...

$testString (string) = just a test!

$testArray (array) = 
Array
(
    [key1] => value1
    [key2] => value2
    [key3] => value3
)



$testInt (int) = 1234567890

$testObject (object) = 
object(stdClass)#1 (2) {
  ["someVar1"]=>
  string(10) "testing123"
  ["someVar2"]=>
  string(1开发者_运维知识库0) "321gnitset"
}


If you want to know the name, why not just pass a string constant of the name of the variable, and use global array to access it (for procedural programs)

function foo($var) {
    echo $var; //name of the variable
    echo $GLOBALS[$$var]; //value of the variale
}

$bar = 'a string';
foo('bar');


Check out this php function func_get_arg()

Actually... it looks like you can't really do it... check this other question How to get a variable name as a string in PHP?


debug_print_backtrace() is your friend! Add it to your debug() function.

If you still want only the calling function name you can use debug_backtrace() and search in the returned array for the name of the previous function, identified as "function" in the associative array result!


I put together these functions for my own debugging just copy and save it in a separate file, include it and use the function d() to call the fancy debug print. it suppose to color code the results based on the type of variable passed.

if(!defined('m_debug')) {define('m_debug', 1);}
function d($var) {
    if (m_debug) {

        $bt = debug_backtrace();
        $src = file($bt[0]["file"]);
        $line = $src[ $bt[0]['line'] - 1 ];

        //striping the inspect() from the sting
        $strip = explode('d(', $line);
        $matches = preg_match('#\(#', $strip[0]);
        $strip = explode(')', $strip[1]);
        for ($i=0;$i<count($matches-1);$i++) {
            array_pop($strip);
        }
        $label = implode(')', $strip);

           d_format($var, $label);
    }

}

function l() {
    global $super_dump_log;

    if (func_num_args() > 0) {
        $array = func_get_args();
        array_merge($super_dump_log, $array);
    } else {
        foreach($super_dump_log as $log){
            //
        }
    }
}

function d_format($var, $label) {

    $colorVar = 'Blue';
    $type = get_type($var);
    $colorType = get_type_color($type);

    echo "<div class='m_inspect' style='background-color:#FFF; overflow:visible;'><pre><span style='color:$colorVar'>";
    echo $label;
    echo "</span> = <span class='subDump' style='color:$colorType'>";
    if ($type == 'string') {
        print_r(htmlspecialchars($var));
    } else {
        print_r($var);
    }
    echo "</span></pre></div>";
}

function get_type($var) {

    if (is_bool($var)) {
        $type = 'bool';
    } elseif (is_string($var)) {
        $type = 'string';
    } elseif (is_array($var)) { 
        $type = 'array';        
    } elseif (is_object($var)) {    
        $type = 'object';       
    } elseif (is_numeric($var)) {
        $type = 'numeric';
    } else {
        $type = 'unknown';
    }

    return $type;
}


function get_type_color($type) {

    if ('bool' == $type) {
        $colorType = 'Green';
    } elseif ('string' == $type) {
        $colorType = 'DimGrey';
    } elseif ('array' == $type) {
        $colorType = 'DarkOrchid';
    } elseif ('object' == $type) {
        $colorType = 'BlueViolet';
    } elseif ('numeric' == $type) {
        $colorType = 'Red'; 
    } else {    
        $colorType = 'Tomato';  
    }

    return $colorType;
}


You can do this from the other end. Pass the variable name in a string variable and then call it with $$ to pass the actual variable. I made my_var_dump function:

function my_var_dump($varName, $var, $line = false, $func = false)
{
    if ($func) $func = ' in function ' . $func;
    if ($line) $line = ' at line ' . $line;
    echo '<pre>DEBUG $' . $varName . $line . $func . PHP_EOL;
    var_dump($var);
    echo '</pre>' . PHP_EOL;
}

Call this function like this:

my_var_dump($varName = "some_var_name", $$varName, __LINE__, __FUNCTION__);


SOLUTION :

$argv — Array of arguments passed to script

EXAMPLE :

<?php
var_dump($argv);
?>

REFERENCE : PHP's $argv documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜