Forum

php Downloads Documentation Get Involved Help Midwest PHP Call For Papers Open Predefined Classes » « List of Reserved Words PHP Manual Appendices List of Reserved Words Change language: Edit Report a Bug List of Keywords ¶ These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion. As of PHP 7.0.0 these keywords are allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name. PHP Keywords __halt_compiler() abstract and array() as break callable (as of PHP 5.4) case catch class clone const continue declare default die() do echo else elseif empty() enddeclare endfor end

php

Downloads
Documentation
Get Involved
Help

Midwest PHP Call For Papers Open
Predefined Classes »
« List of Reserved Words

PHP Manual Appendices List of Reserved Words

Change language:
Edit Report a Bug
List of Keywords ¶

These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion.

As of PHP 7.0.0 these keywords are allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.
PHP Keywords __halt_compiler() abstract and array() as
break callable (as of PHP 5.4) 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 (as of PHP 5.5) for foreach function
global goto (as of PHP 5.3) if implements include
include_once instanceof insteadof (as of PHP 5.4) interface isset()
list() namespace (as of PHP 5.3) new or print
private protected public require require_once
return static switch throw trait (as of PHP 5.4)
try unset() use var while
xor yield (as of PHP 5.5) yield from (as of PHP 7.0)
Compile-time constants __CLASS__ __DIR__ (as of PHP 5.3) __FILE__ __FUNCTION__ __LINE__ __METHOD__
__NAMESPACE__ (as of PHP 5.3) __TRAIT__ (as of PHP 5.4)
add a note add a note
User Contributed Notes 4 notes
up
down
37
martindilling at gmail dot com ¶
6 years ago
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|mplements|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|break|list|(x)?or|var|while)
)\b
up
down
15
Thomas Hansen ¶
3 years ago
Please note that reserved words are still not allowed to be used as namespace or as part of it:

namespace MyNameSpace\List;

class Test
{
}
?>

This will fail with a Parse error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING)
up
down
18
Chris ¶
7 years ago
Here they are as arrays:

$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', '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', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', '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_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
?>

Along with get_defined_functions() and get_defined_constants(), this can be useful for checking eval() statements.
up
down
-20
bla at taxistop dot be ¶
1 year ago
This list doesn't include 'self' and 'parent', which I take are indeed keywords.
add a note add a note

List of Reserved Words
List of Keywords
Predefined Classes
Predefined Constants
List of other reserved words

Copyright © 2001-2019 The PHP Group My PHP.net Contact Other PHP.net sites Privacy policy

Messages In This Thread