Php Validate Variable Name

[Solved] Php Validate Variable Name | Php - Code Explorer | yomemimo.com
Question : PHP validate variable name

Answered by : tiago-frana

## Regex:
## ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$
// PHP validate variable name
$regex = '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/i';
// Valid
preg_match($regex, '_aas', $matches);
[ "_aas",
]
// Valid
preg_match($regex, '_validVariable', $matches);
[ "_validVariable",
]
// Valid
preg_match($regex, 'oth3r_valid_variable', $matches);
[ "oth3r_valid_variable",
]
//Invalid
preg_match($regex, 'invalid Variable', $matches);
[]
//Invalid
preg_match($regex, '0th3r_invalid_vriable', $matches);
[]
//Invalid
preg_match($regex, '0th3r_invalid_variable', $matches);
[]

Source : https://www.php.net/manual/en/language.variables.basics.php | Last Update : Wed, 28 Sep 22

Answers related to php validate variable name

Code Explorer Popular Question For Php