Common Rules for Naming Variables in PHP

Updated on     Kisan Patel

Variable names or identifiers should be very descriptive. I have seen scripts where all the variables were named $var1, $var1, $var2, and so on. It may seem straightforward to name variables like this, but two years from now when you come back to the script, it will take forever to figure out what information is in each variable. PHP won’t care or get confused, but humans trying to follow the script will have a hard time. Make your scripts much easier to understand by using descriptive variable names like $firstName, $directory_name, or $DateOfBirth.

The rules for variable names are as follows:

  1. All variable names start with a dollar sign ($). This tells PHP that it is a variable name.
  2. Variable names can be any length.
  3. Variable names can include letters, numbers, and underscores only.
  4. Variable names must begin with a letter or an underscore. They cannot begin with a number.
  5. Uppercase and lowercase letters are not the same. $favoritecity and $Favoritecity are not the same variable. If you store information in $FavoriteCity, you can’t retrieve that information later in the script by using the variable name $favoriteCity.

Assigning variable names is a matter of personal style. Creating descriptive variable names by connecting words with an underscore or by using uppercase letters to denote the beginning of new words (often called camel caps) are the two most common variable naming styles, as shown here:


Naming your variables by using one of these two common styles makes it easier for other programmers to read your scripts. It’s also common to start the name with a lowercase letter. The most important factor in naming variables, however, is to be consistent. Pick a style and use it throughout the entire script.


Leave a Reply