W3docs

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 name

Two practical rules follow from this:

  • Keywords are case-insensitive. if, IF, and If all mean the same thing, so ECHO "hi"; works. (Variable names and the names you define, by contrast, are case-sensitive.)
  • Constants like true, false, and null are 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.

CategoryKeywords
Conditionalsif, else, elseif, endif, switch, case, default, endswitch, match
Loopsfor, endfor, foreach, endforeach, while, endwhile, do, break, continue
Functions & generatorsfunction, fn, return, yield, use, static, global
Classes & OOPclass, interface, trait, enum, extends, implements, new, clone, abstract, final, public, protected, private, const, var, instanceof, insteadof
Namespaces & autoloadingnamespace, use, include, include_once, require, require_once
Error handlingtry, catch, finally, throw
Language constructsecho, print, array, list, isset, unset, empty, die, exit, eval
Logical operatorsand, or, xor
Type & valuestrue, false, null, callable, int, float, string, bool
Otheras, 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 supports

Using 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() or Foo::CONST won't parse, but Foo::PRINT does), but using them hurts readability. Prefer a clearer name.
  • echo and print are not functions. echo "a", "b"; works (multiple arguments, no parentheses needed); they are language constructs, so you can't pass them as callbacks.
  • exit and die are identical. Both stop the script immediately; die is just an alias.
  • Don't confuse and/or with &&/||. The word forms have lower precedence, so $ok = true and false; assigns true, not false. Stick to && and || in expressions. See PHP Operators for the precedence table.

Each keyword group has its own chapter that goes deeper:

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.

Practice

Practice
Which of the following are reserved keywords in PHP?
Which of the following are reserved keywords in PHP?
Was this page helpful?