Liste der Schlüsselwörter

Die folgenden Begriffe haben in PHP eine spezielle Bedeutung. Einige von ihnen sehen aus wie Funktionen, Konstanten usw., in Wirklichkeit handelt es sich aber um Sprachkonstrukte. Keines der folgenden Wörter kann als Name für eine benutzerdefinierte Konstante, Klasse, Funktion oder Methode genutzt werden. Die Nutzung als Variablenname ist in der Regel möglich, könnte aber Verwirrung stiften.

Von PHP 7.0.0 an sind diese Schlüsselwörter als Eigenschafts-, Konstanten- und Methodennamen von Klassen, Interfaces und Traits erlaubt, außer dass class nicht als Konstantenname verwendet werden darf.

PHP Schlüsselworte
__halt_compiler() abstract and array() as
break callable (von PHP 5.4 an) 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 (von PHP 5.5 an) fn (as of PHP 7.4) for foreach
function global goto (von PHP 5.3 an) if implements
include include_once instanceof insteadof (von PHP 5.4 an) interface
isset() list() namespace (von PHP 5.3 an) new or
print private protected public require
require_once return static switch throw
trait (von PHP 5.4 an) try unset() use var
while xor yield (von PHP 5.5 an) yield from (as of PHP 7.0)
Kompilierzeit-Konstanten
__CLASS__ __DIR__ (von PHP 5.3 an) __FILE__ __FUNCTION__ __LINE__ __METHOD__
__NAMESPACE__ (von PHP 5.3 an) __TRAIT__ (von PHP 5.4 an)
add a note add a note

User Contributed Notes 5 notes

up
33
martindilling at gmail dot com
11 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
18
Thomas Hansen
7 years ago
Please note that reserved words are still not allowed to be used as namespace or as part of it:

<?php
namespace MyNameSpace\List;

class
Test
{
}
?>

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

<?php
$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
-8
Johnny D.
3 years ago
const FORBIDDEN_TYPES = [
    'null',

    'bool',
    'false',
    'true',

    'int',
    'float',

    'string',
];
up
-40
bla at taxistop dot be
5 years ago
This list doesn't include 'self' and 'parent', which I take are indeed keywords.
To Top