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

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

    You might consider changing your approach and using a variable variable name?

    $var_name = "FooBar";
    $$var_name = "a string";
    

    then you could just

    print($var_name);
    

    to get

    FooBar
    

    Here's the link to the PHP manual on Variable variables

提交回复
热议问题