How to get a variable name as a string in PHP?

前端 未结 24 1405
南旧
南旧 2020-11-22 01:35

Say i have this PHP code:

$FooBar = \"a string\";

i then need a function like this:

print_var_name($FooBar);
24条回答
  •  天涯浪人
    2020-11-22 02:14

    I couldn't think of a way to do this efficiently either but I came up with this. It works, for the limited uses below.

    shrug

    
    
    // Returns
    Array
    (
        [0] => $foo
        [1] => foo
    )
    Array
    (
        [0] => $bar
        [1] => bar
    )
    Array
    (
        [0] => $baz
        [1] => baz
    )
    

    It works based on the line that called the function, where it finds the argument you passed in. I suppose it could be expanded to work with multiple arguments but, like others have said, if you could explain the situation better, another solution would probably work better.

提交回复
热议问题