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. $name and $Name are two different variables.
Variable nameValid?
$nameYes
$_nameYes
$user_name2Yes
$2nameNo, starts with a digit
$first-nameNo, hyphen is not allowed
$first nameNo, 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
Exercises & Assignments
Write a program to print “Hello World” using echo
Write a program to print “Hello PHP” using variable
Write a program to print a string using echo+variable.
Write a program to print two variables in single echo
Interview Questions & Answers
How to check if a variable is empty in PHP?
Is PHP a case sensitive language?
What are the different data types in PHP?
What is the difference between $var and $$var in PHP?
Which special characters are allowed in PHP variable names?
Can we print multiple variables in a single echo statement in PHP?
How to check the data type of a variable in PHP?