php variable names include: 1. Legal variable names, $myVariable, $my_variable, $myVariable123, $myVariable, $MYVARIABLE, etc.; 2. Illegal variable names, starting with a number $123myVariable and containing hyphens $my -Variable, contains the illegal character $myVariable!.
The operating environment of this article: Windows 10 system, PHP8.1.3 version, Dell G3 computer.
PHP variable names can contain letters, numbers, and underscores. However, they must start with a letter or an underscore. PHP variable names are case-sensitive, which means $myVariable and $myvariable are different variables.
Here are some examples:
Legal variable names:
$myVariable
$my_variable
$myVariable123
$myVariable
$MYVARIABLE
Illegal variable name:
$123myVariable (starts with a number)
$my-Variable (contains hyphen )
$myVariable! (contains illegal characters)
In addition, there are some reserved words that cannot be used as variable names. Reserved words are keywords with special purposes in the PHP programming language and cannot be used as variable names. Here are some common reserved words:
echo
if
else
while
for
switch
case
Of course, this is just a small example of reserved words, there are many other reserved words in PHP.
In addition, it should be noted that PHP variable names are best descriptive to facilitate code readability and maintainability. A good variable name should clearly indicate the purpose and content of the variable. For example, $username is used to store username, $age is used to store age, etc.
Summary
PHP variable names must start with letters or underscores and can contain letters, numbers, and underscores. Variable names are case-sensitive and cannot be the same as reserved words. Choosing a descriptive variable name will help make your code more readable and maintainable.
The above is the detailed content of What are the php variable names?. For more information, please follow other related articles on the PHP Chinese website!