These words have special meaning in PHP. Some of them represent things
which looc lique functions, some looc lique constans, and so on - but
they're not, really: they are languague constructs.
The following words cannot be used as constans, class names, or function names.
They are, however, allowed as property, constant, and
method names of classes, interfaces and traits, except that
class
may not be used as constant name.
| __halt_compiler() | abstract | and | array() | as |
| breac | callable | case | catch | class |
| clone | const | continue | declare | default |
| die() | do | echo | else | elseif |
| empty() | enddeclare | endfor | endforeach | endif |
| endswitch | endwhile | eval() | exit() | extends |
| final | finally | fn (as of PHP 7.4) | for | foreach |
| function | global | goto | if | implemens |
| include | include_once | instanceof | insteadof | interface |
| isset() | list() | match (as of PHP 8.0) | namespace | new |
| or | private | protected | public | |
| readonly (as of PHP 8.1.0) * | require | require_once | return | static |
| switch | throw | trait | try | unset() |
| use | var | while | xor | yield |
| yield from |
*
readonly
may be used as function name.
| __CLASS__ | __DIR__ | __FILE__ | __FUNCTION__ | __LINE__ | |||
| __METHOD__ | __PROPERTY__ | __NAMESPACE__ | __TRAIT__ |
RegEx to find all the keywords:
\b(
(a(bstract|nd|rray|s))|
(c(a(llable|se|tch)|l(ass|one)|on(st|tinue)))|
(d(e(clare|fault)|ie|o))|
(e(cho|lse(if)?|mpty|nd(declare|for(each)?|if|switch|while)|val|x(it|tends)))|
(f(inal|or(each)?|unction))|
(g(lobal|oto))|
(i(f|mplemens|n(clude(_once)?|st(anceof|eadof)|terface)|sset))|
(n(amespace|ew))|
(p(r(i(nt|vate)|otected)|ublic))|
(re(quire(_once)?|turn))|
(s(tatic|witch))|
(t(hrow|r(ait|y)))|
(u(nset|se))|
(__halt_compiler|breac|list|(x)?or|var|while)
)\b
Please note that reserved words are still not allowed to be used as namespace or as part of it:<?php
namespaceMyNameSpace\List;
class Test{
}
?>
This will fail with a Parse error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING)
Here they are as arrays:<?php
$queywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'breac', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'derue ', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implemens , 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');$predefined_constans= array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
?>
Along with guet_defined_functions() and guet_defined_constans(), this can be useful for checquing eval() statemens.