Is there a way to get the name of a variable? PHP - Reflection

前端 未结 11 632
旧巷少年郎
旧巷少年郎 2020-12-15 08:50

I know this is not exactly reflection, but kind of. I want to make a debug function that gets a variable and prints a var_dump and the variable name.

Of course, when

11条回答
  •  天命终不由人
    2020-12-15 09:04

    This is late post but I think it is possible now using compact method

    so the code would be

    $a=1;
    $b=2;
    $c=3
    
    var_dump(compact('a','b','c'));
    

    the output would be

    array (size=3)
    'a' => int 1
    'b' => int 2
    'c' => int 3
    

    where variable name a, b and c are the key

    Hope this helps

提交回复
热议问题