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.

Example: variable prefix
<?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.

Example: names may not start with a number
<?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 _ .

Example: alphabetical letters and underscores are allowed
<?php

// Allowed
$Name;
$name;
$_name;

?>

4. A name may not contain special characters such as $ , @ , % etc.

Example: special characters are not allowed
<?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.

Example: names are case sensitive
<?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.

Example:
<?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.

Snake case

When using snake case, all words of the name is lowercase. If a name has multiple words, the words are separated by underscores.

Example: snake casing
<?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.

Camel case

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.

Example: camel casing
<?php

// Camel casing
$name;
$firstName;

?>

Most coders like camel casing for variable and function names.

Pascal case

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.

Example: camel casing
<?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.

__halt_compiler()abstractandarray()as
breakcallable (PHP 5.4)casecatchclass
cloneconstcontinuedeclaredefault
die()doechoelseelseif
empty()enddeclareendforendforeachendif
endswitchendwhileeval()exit()extends
finalfinally (PHP 5.5)forforeachfunction
globalgoto (PHP 5.3)ifimplementsinclude
include_onceinsteadof (PHP 5.4)instanceofinterfaceisset()
list()namespace (PHP 5.3)neworprint
privateprotectedpublicrequirerequire_once
returnstaticswitchthrowtrait (PHP 5.4)
tryunset()usevarwhile
xoryield (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.

Lvalue

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.

Output: Lvalue, the left operand
<?php

// lvalue (var1) may be on the left
$var1 = 10

// or on the right side of the =
$var2 = $var1

?>

Rvalue

An rvalue is an expression that cannot be assigned a value so it may only appear on the right side of an assignment.

Output: Lvalue, the left operand
<?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 =.