Which special characters are allowed in PHP variable names?
Only the underscore (_) is allowed as a special character in a PHP variable name. Characters like -, @, # and a space are not allowed.
Here are the full rules:
- A variable name starts with the dollar sign
$. - After the dollar sign, the first character must be a letter or an underscore, not a digit.
- The rest of the name can have letters, digits and underscores.
- Variable names are case-sensitive.
$nameand$Nameare two different variables.
| Variable name | Valid? |
|---|---|
| $name | Yes |
| $_name | Yes |
| $user_name2 | Yes |
| $2name | No, starts with a digit |
| $first-name | No, hyphen is not allowed |
| $first name | No, space is not allowed |
PHP also accepts some non-English letters, but it is better to use only a to z in names. Read more in PHP Variables.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Variables |