PHP 是一种广泛使用的服务器端脚本语言,用于开发动态网页和 Web 应用程序。PHP 有许多关键字(也称为保留字),这些关键字在 PHP 中有特殊的含义,不能用作变量名、函数名、类名等标识符。以下是 PHP 中的关键字列表:
基本关键字
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
finally
fn
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
yield
yield 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()
类型声明
bool
int
float
string
array
object
iterable
void
mixed
static
self
parent
其他关键字
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 代码非常重要。避免使用这些关键字作为标识符,可以防止代码中的冲突和错误。