How to check if a string can be used as a variable name in PHP?

前端 未结 7 1581
孤独总比滥情好
孤独总比滥情好 2021-02-15 18:08

In PHP one can use variable variables...

For example...

class obj { }
$fieldName = \"Surname\";
$object = new obj();
$object->Name = \"John\";
$objec         


        
7条回答
  •  执笔经年
    2021-02-15 18:35

    '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' will validate a PHP variable name.

    This regex is directly from the documentation at: http://www.php.net/manual/en/language.variables.basics.php

提交回复
热议问题