PHP Keywords & Identifiers Tutorial
In this tutorial we learn about key words in PHP that are reserved for special uses, as well as lvalues and rvalues (operands).
We also learn about identifiers and the rules for naming and casing them.
What is an identifier?
An identifier is the name by which we identify variables, constants, arrays, functions, interfaces and classes.
It’s a name, like John would be the name of a person, or Ford would be the brand name of a car.
Rules for naming identifiers
PHP has certain rules that we must keep in mind when naming our variables, constants, arrays, functions, interfaces and classes.
If there are specific rules and/or conventions other than the ones below, we will explain each in their respective tutorial lessons.
1. A variable must start with a $ symbol. If we don’t include the $ symbol the interpreter will assume a constant. This rule does not apply to constants, functions, interfaces or classes.
<?php // Variable $message = "Hello World"; // Not a variable message = "Hello World"; ?>
2. A name cannot start with a numerical value, but may contain a number inside of it.
<?php // Not allowed $21_Jump_Street; // Allowed $mambo_number_5; ?>
3. A name may start with uppercase or lowercase alphabetical letters A - Z or a - z , and underscores _ .
<?php // Allowed $Name; $name; $_name; ?>
4. A name may not contain special characters such as $ , @ , % etc.
<?php // Not allowed $^_^; $n@me; $name&surname; ?>
5. Most names are case sensitive, which means that a name with lowercase letters is not the same as a name with uppercase letters.
<?php // Not the same $learning; $LearNing; $LEARNING; ?>
Even though the same word is used in the example above, they are treated as different because of their casing.
6. A name should not contain a PHP specific keyword. Keywords are words that are reserved by PHP for special uses.
<?php // Bad practice $if; $echo; $while; ?>
In PHP the words if , else , and while are keywords. It’s not strictly illegal to name a variable with these words, however, it’s conventionally considered as bad practice.
Identifier casing conventions
Most languages have strict conventions on the casing of data containers such as variables.
In the case of Python for example, variable and function names use snake_case with underscores separating each word, while classes use PascalCase where words are capitalized.
The Python conventions are specified in its PEP-8 document, and it’s heavily frowned upon to break from those conventions.
In PHP though, the conventions are less strict. We can see, especially in older code, that functions are often named with snake_case, but in newer code camelCase or PascalCase is used.
We also have to consider popular PHP frameworks and applications. The Zend and Symfony frameworks, for example, do not like underscores but CodeIgniter promotes using snake_case. Wordpress also encourages the use of snake_case instead of camelCase.
Let’s take a look at the different casings.
When using snake case, all words of the name is lowercase. If a name has multiple words, the words are separated by underscores.
<?php // Snake casing $name; $first_name; ?>
Studies have shown that snake casing is the easiest to read, however, most coders don’t like snake casing.
When using camel case, the first word of a name is lowercase. If a name has multiple words, each new word is directly connected to the previous word but the first letter is uppercase.
<?php // Camel casing $name; $firstName; ?>
Most coders like camel casing for variable and function names.
When using pascal case, the first letter of a word is uppercase. If the name has multiple words, each new word is directly connected to the previous word but the first letter is also uppercase.
<?php // Pascal casing $Name; $FirstName; ?>
Most developers like pascal casing for class names.
Which casing to use
As mentioned before, different frameworks will have different conventions. Most workplaces will also have their own conventions, which you will have to follow.
As it’s not as strict in PHP as it is in Python, it doesn’t really matter which you use, as long as you stay consistent.
In this tutorial series we will be using camelCase for variables/attributes and functions/methods, and PascalCase for classes.
For constants we will use full uppercase with underscore word separation: EXAMPLE_CONSTANT.
What is a keyword
In PHP there are certain words that’s reserved for a special use. We cannot use these words when naming our variables, constants, arrays, functions, interfaces and classes.
These keywords have special meaning and is only to be used in special contexts. While there is nothing strictly stopping us from prefixing a keyword with a $ sign, it is conventionally considered bad practice, and it will make code difficult and slow to read.
List of keywords
The following table shows the list of keywords in PHP that are reserved for special use.
|break||callable (PHP 5.4)||case||catch||class|
|final||finally (PHP 5.5)||for||foreach||function|
|global||goto (PHP 5.3)||if||implements||include|
|include_once||insteadof (PHP 5.4)||instanceof||interface||isset()|
|list()||namespace (PHP 5.3)||new||or|
|return||static||switch||throw||trait (PHP 5.4)|
|xor||yield (PHP 5.5)||yield from (PHP 7.0)|
Operands. Lvalue & Rvalue
There are two kinds of expressions in PHP, Lvalues and Rvalues. The operand on the left of the assignment = is the Lvalue and the operand on the right side of the assignment is the Rvalue.
An lvalue can have a value assigned to it so it’s allowed to appear on either the left or right side of an assignment.
<?php // lvalue (var1) may be on the left $var1 = 10 // or on the right side of the = $var2 = $var1 ?>
An rvalue is an expression that cannot be assigned a value so it may only appear on the right side of an assignment.
<?php // rvalue (10) may only be on the right var1 = 10 // and cannot be on the left 10 = 10 ?>
Summary: Points to remember
- A variable name in PHP is always prefixed with the $ symbol.
- A name cannot start with a numerical value.
- A name may start with uppercase or lowercase alphabetical letters and underscores.
- A name may not contain special characters.
- A name may not contain special characters.
- Names are case sensitive.
- Using reserved keywords for names is considered bad practice.
- Be consistent in the casing that you use. In this series we will use camelCase for variables, attributes, functions and methods, and PascalCase for classes.
- Reserved keywords have special meaning and should only be used in special context.
- An Lvalue is the operand on the left or right of the =.
- An Rvalue is the operand on the right of the =.