PHP Reference ยท Concepts and Reference
PHP reserved keywords
The list of PHP reserved keywords, which words you cannot use as names, which are allowed as method or constant names, and how to list tokens yourself.
A reserved keyword is a word with a fixed meaning in the PHP grammar, such as if, class or return. You cannot use most of them as the name of a class, function or constant. Since PHP 7 the rules are more relaxed: most keywords are allowed as method names, property names and class constant names, so $list->new() and Status::DEFAULT are fine.
Keywords are case-insensitive (IF works), but write them in lowercase. Names of variables are case-sensitive and are never keywords because they start with $.
<?php class Router { public function list(): array { return []; } // allowed as a method name const DEFAULT = "home"; // allowed as a constant name }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
The keyword list
| Group | Keywords |
|---|---|
| Control flow | if, else, elseif, endif, switch, case, default, endswitch, match, for, endfor, foreach, endforeach, as, while, endwhile, do, break, continue, goto, declare, enddeclare, return, yield |
| Errors | try, catch, finally, throw |
| Functions and values | function, fn, use, static, global, echo, print, exit, die, eval, isset, unset, empty, list, array, callable, clone, new, include, include_once, require, require_once |
| OOP | abstract, class, interface, trait, enum, extends, implements, final, public, protected, private, readonly, const, var, instanceof, insteadof, self, parent |
| Namespaces | namespace |
| Operators | and, or, xor |
| Compile-time constants | __CLASS__, __DIR__, __FILE__, __FUNCTION__, __LINE__, __METHOD__, __NAMESPACE__, __TRAIT__, __halt_compiler |
true, false and null are also reserved as constant names. Type names such as int, float, string, bool, iterable, object, mixed, void, never, null, false and true cannot be used as class names.
Usage notes
- Allowed as method, property and class constant names: almost every keyword (except
classas a constant name, which is reserved forFoo::class). - Not allowed as function, class, interface or constant names: all of the list above.
- Magic constants such as
__CLASS__and the__construct-style magic method names are reserved by convention. Do not invent your own__somethingnames. - A common surprise is
list,printandarray:printandlistare language constructs, not functions, which is why they cannot be passed as callables. - Namespaced code can define a function called
array_maporprint_it, but never a keyword. - Use
PhpToken::tokenize()ortoken_get_all()(tokenizer extension) to inspect how PHP reads code.
Related
- PHP tags and embedding
- Glossary
- Tutorial: PHP syntax, Variables