PHP Keywords
Keywords are predefined, reserved words used in programming languages that cannot be used as identifiers for variables, functions, classes, or other elements in
What Are PHP Keywords?
Keywords are reserved words that PHP gives a built-in meaning. They define the language's structure — conditionals, loops, classes, error handling, and more — so PHP treats them differently from names you invent yourself. This page lists every PHP keyword, explains the rules around them, and shows how the most common ones are used.
Because they are reserved, you cannot use a keyword as an ordinary identifier:
<?php
// These all cause a parse error — the words are reserved:
$list = []; // OK: "list" is fine as a variable name in modern PHP...
function for() {} // Parse error: "for" cannot be a function name
class If {} // Parse error: "if" cannot be a class nameTwo practical rules follow from this:
- Keywords are case-insensitive.
if,IF, andIfall mean the same thing, soECHO "hi";works. (Variable names and the names you define, by contrast, are case-sensitive.) - Constants like
true,false, andnullare also reserved. They behave like keywords even though they look like ordinary values.
List of PHP Keywords
The table below groups the keywords by what they do, which makes them far easier to remember than one flat list.
| Category | Keywords |
|---|---|
| Conditionals | if, else, elseif, endif, switch, case, default, endswitch, match |
| Loops | for, endfor, foreach, endforeach, while, endwhile, do, break, continue |
| Functions & generators | function, fn, return, yield, use, static, global |
| Classes & OOP | class, interface, trait, enum, extends, implements, new, clone, abstract, final, public, protected, private, const, var, instanceof, insteadof |
| Namespaces & autoloading | namespace, use, include, include_once, require, require_once |
| Error handling | try, catch, finally, throw |
| Language constructs | echo, print, array, list, isset, unset, empty, die, exit, eval |
| Logical operators | and, or, xor |
| Type & values | true, false, null, callable, int, float, string, bool |
| Other | as, declare, enddeclare, goto, __halt_compiler |
The exact set grows slowly between versions — match arrived in PHP 8.0, enum in PHP 8.1, and fn (arrow functions) in PHP 7.4 — so a few entries above are unavailable on older runtimes. You can always check a word at runtime instead of memorising the list:
<?php
var_dump(function_exists('array')); // bool(false) — "array" is a language construct, not a function
echo PHP_VERSION, "\n"; // tells you which keywords your runtime supportsUsing PHP Keywords
Keywords combine into the statements that make up every program. The example below uses a conditional, a loop, and a function definition together:
<?php
$x = 7;
// Define a conditional statement
if ($x == 5) {
echo "x is equal to 5.";
}
// Define a loop
for ($i = 0; $i < 10; $i++) {
echo $i;
}
// Define a function
function add($a, $b)
{
return $a + $b;
}
?>Here the if keyword starts a conditional that checks whether $x equals 5, for builds a loop that runs from 0 to 9, and function declares a reusable routine that returns the sum of its two arguments.
Common Gotchas
- Reserved words as method or constant names. Since PHP 7, most keywords are allowed as method, property, and class-constant names (e.g.
$object->list()orFoo::CONSTwon't parse, butFoo::PRINTdoes), but using them hurts readability. Prefer a clearer name. echoandprintare not functions.echo "a", "b";works (multiple arguments, no parentheses needed); they are language constructs, so you can't pass them as callbacks.exitanddieare identical. Both stop the script immediately;dieis just an alias.- Don't confuse
and/orwith&&/||. The word forms have lower precedence, so$ok = true and false;assignstrue, notfalse. Stick to&&and||in expressions. See PHP Operators for the precedence table.
Related Topics
Each keyword group has its own chapter that goes deeper:
- PHP Variables and PHP Data Types — how
var,array, and the type keywords work. - PHP If...Else...Elseif and PHP Loops — the control-flow keywords.
- PHP Functions —
function,return,use, and scope. - PHP Classes and Objects — the OOP keywords.
- PHP Constants and PHP Namespaces.
Conclusion
PHP keywords are the fixed vocabulary of the language: reserved, case-insensitive words that build conditionals, loops, classes, and error handling. Knowing which words are reserved keeps you from naming things illegally, and knowing what each one does lets you read and write PHP fluently. When in doubt, group them by purpose — as in the table above — rather than memorising them one by one.