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

前端 未结 24 1425
南旧
南旧 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:05

    You could use compact() to achieve this.

    $FooBar = "a string";
    
    $newArray = compact('FooBar');
    

    This would create an associative array with the variable name as the key. You could then loop through the array using the key name where you needed it.

    foreach($newarray as $key => $value) {
        echo $key;
    }
    

提交回复
热议问题