When writing PHP code, you encounter words like if, else,
for, or function on a daily basis. These words are called keywords
or reserved words. Since they represent the core grammar of the PHP language and tell the interpreter how to
control the program logic, they have a predefined, strictly system-level meaning. In the interactive simulator below,
you can see some of the most common examples of their use in practice, and then we will look at why you cannot
use these words to name your own variables.
Before we look at the full list, it is important to understand why these words are so special. Think of keywords
as "traffic signs" in the PHP language. When the PHP interpreter reads your code and encounters
a word like if, class, or function, it knows exactly that it needs to run
a specific system logic at that very moment (e.g., check a condition, create an object, or prepare a function).
Because these words have a deep and predefined meaning built-in, PHP does not allow you to use them for your own naming purposes. To put it simply: you must not use keywords as names for your variables, functions, classes, or constants.
$new = "new data"; // ERROR! 'new' is a keyword for creating objects.
$function = 5; // ERROR! 'function' is reserved for declaring functions.
If you try to run such code, execution
will stop immediately with a critical error: Parse error: syntax error, unexpected '$'.
Interestingly, keywords in PHP (unlike variables) are not case-sensitive. This means that
echo, ECHO, and eChO are the exact same thing to PHP, although in practice,
they are always written exclusively in lowercase for readability.
Below is a complete list of reserved words that PHP uses to control its internal logic. They are divided according to their main purpose, which will help you learn their role in programming faster:
| Keyword | Description and Meaning |
|---|---|
abstract |
Declaration of an abstract class or method that has no implementation. |
and |
Logical AND operator (lower priority than &&). |
array |
Construct for creating an array or typing a data structure. |
as |
Assigning an alias in a foreach loop or when using namespaces. |
break |
Immediate termination of a loop (for, while, foreach)
or a switch block. |
case |
Checking a specific value inside a switch structure. |
catch |
A block of code that executes if an exception is thrown in a try block. |
class |
Keyword for defining a class (Object-Oriented Programming). |
const |
Definition of a constant at the class level or globally. |
continue |
Skips the rest of the current iteration and continues to the next one. |
declare |
Sets execution directives for a block of code (e.g., strict_types=1). |
default |
Fallback option in a switch statement if no case value matches. |
do |
Start of a do-while loop, which executes at least once. |
else |
A block of code that executes if the condition in the if statement is false. |
elseif |
An additional conditional statement inside an if structure. |
enddeclare |
Alternative syntax for ending a declare block. |
endfor |
Alternative syntax for ending a for loop. |
endforeach |
Alternative syntax for ending a foreach loop. |
endif |
Alternative syntax for ending an if statement. |
endswitch |
Alternative syntax for ending a switch block. |
endwhile |
Alternative syntax for ending a while loop. |
extends |
Indicates inheritance: a class takes on properties and methods of a parent class. |
final |
Prevents a class from being inherited or a method from being overridden. |
finally |
A block in a try-catch structure that always executes (regardless of whether an error
occurred). |
for |
A classic numeric loop. |
foreach |
A loop specialized for iterating through array elements. |
function |
Declaration of a user-defined function or an anonymous function. |
global |
Allows access to a global variable from within the local scope of a function. |
goto |
Jumps to a specific label in the code (use with caution). |
if |
The basic conditional statement. |
implements |
Implements an interface within a class. |
include |
Includes an external file (emits a Warning if the file is missing). |
include_once |
Includes a file only once during the entire script execution cycle. |
instanceof |
Checks whether a specific object is an instance of a specific class. |
interface |
Definition of an interface, specifying which methods a class must implement. |
namespace |
Organizes code into logical groups (namespaces). |
new |
Creating a new instance (object) of a class. |
or |
Logical OR operator (lower priority than ||). |
private |
Private visibility: accessible only within the same class. |
protected |
Protected visibility: accessible within the class and derived classes. |
public |
Public visibility: accessible from anywhere. |
require |
Mandatory file inclusion (throws a Fatal Error if the file is missing). |
require_once |
Mandatory file inclusion only once. |
return |
Returns a value from a function and terminates its execution. |
static |
Definition of static methods/properties or static variables inside functions. |
switch |
A structure for checking the value of a single variable multiple times. |
throw |
Manually triggers (throws) an exception. |
trait |
A group of methods that can be included in multiple different classes. |
try |
Starts a block for catching potential errors. |
use |
Imports namespaces or includes traits into a class. |
var |
Deprecated syntax for public (used back in PHP 4). |
while |
A loop that runs as long as the condition is met. |
xor |
Logical "exclusive or" (true if exactly one of the conditions is true). |
Beginners often wonder where these words belong. Keywords are not regular functions that you can just call anywhere. They are basic structural building blocks. Depending on where they live in the code and what their role is, they can be divided into 4 main groups:
We write these words directly into the main execution flow of the script when we want to direct the program
logic and check conditions. We usually write parentheses () and curly braces {} around
them.
// Example of using conditions and loops directly in the code:
if ($hour < 12) {
echo 'Good morning!';
}
switch ($color) {
case 'red':
echo 'Stop!';
break;
}
These words are used to create or declare completely new structures in your application. Most often they stand at the very beginning of a line and signal to the interpreter exactly what follows.
// Use at the beginning of a line to define structures:
abstract class BaseDatabase {
// class code
}
class MyDatabase extends BaseDatabase {
// code that inherits properties
}
function add($a, $b) {
return $a + $b;
}
These words are strictly limited and live exclusively inside the curly braces of a class (Object-Oriented Programming). We use them to determine security or from where certain data can be called.
class User {
// We enter these inside the class before variables and methods:
private $password;
public $username;
public static function displayData() {
// static method
}
}
These words act like arithmetic symbols or tools for working with data. We write them directly between variables, to the right of the equals sign, or directly before class names.
// They act as operators between data:
if ($condition_A xor $condition_B) {
// Logical exclusive OR between two conditions
}
$car = new Car(); // 'new' stands between the variable and the class
if ($car instanceof Car) {
// Checking the object type
}
By knowing these four groups, it will be much easier for you to understand the complete table above and immediately know which part of the code each keyword fits into!