PHP 是一种广泛使用的服务器端脚本语言,用于开发动态网页和 Web 应用程序。PHP 有许多关键字(也称为保留字),这些关键字在 PHP 中有特殊的含义,不能用作变量名、函数名、类名等标识符。以下是 PHP 中的关键字列表:
基本关键字
abstractandarrayasbreakcallablecasecatchclasscloneconstcontinuedeclaredefaultdiedoechoelseelseifemptyenddeclareendforendforeachendifendswitchendwhileevalexitextendsfinalfinallyfnforforeachfunctionglobalgotoifimplementsincludeinclude_onceinstanceofinsteadofinterfaceissetlistnamespaceneworprintprivateprotectedpublicrequirerequire_oncereturnstaticswitchthrowtraittryunsetusevarwhilexoryieldyield from预定义常量
__CLASS____DIR____FILE____FUNCTION____LINE____METHOD____NAMESPACE____TRAIT__魔术方法
__construct()__destruct()__call()__callStatic()__get()__set()__isset()__unset()__sleep()__wakeup()__toString()__invoke()__set_state()__clone()__debugInfo()类型声明
boolintfloatstringarrayobjectiterablevoidmixedstaticselfparent其他关键字
enum (PHP 8.1 引入)match (PHP 8.0 引入)readonly (PHP 8.1 引入)never (PHP 8.1 引入)self 和 parent 在类定义中使用。<?php
class MyClass {
public function __construct() {
echo "Constructor called!";
}
public function myMethod() {
echo "Method called!";
}
}
$obj = new MyClass();
$obj->myMethod();
?>
在这个示例中,class、public、function、new 等都是 PHP 的关键字。
了解 PHP 的关键字对于编写符合规范的 PHP 代码非常重要。避免使用这些关键字作为标识符,可以防止代码中的冲突和错误。