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

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

    I think you want to know variable name with it's value. You can use an associative array to achieve this.

    use variable names for array keys:

    $vars = array('FooBar' => 'a string');
    

    When you want to get variable names, use array_keys($vars), it will return an array of those variable names that used in your $vars array as it's keys.

提交回复
热议问题