PHP 支持多种数据类型,主要分为以下几类:
42
。3.14
。"Hello, World!"
。true
或 false
。array(1, 2, 3)
或 [1, 2, 3]
。new MyClass()
。$var = null;
。'myFunction'
或 [$object, 'method']
。Traversable
接口的对象。<?php
// 标量类型
$integer = 42;
$float = 3.14;
$string = "Hello, World!";
$boolean = true;
// 复合类型
$array = [1, 2, 3];
$object = new stdClass();
// 特殊类型
$null = null;
$resource = fopen('file.txt', 'r');
// 伪类型
$callable = function() {
return "I am callable!";
};
// 类
class MyClass {
public $property = "I am a property!";
}
// 接口
interface MyInterface {
public function myMethod();
}
?>
PHP 提供了丰富的数据类型,涵盖了从简单的标量类型到复杂的复合类型和特殊类型。理解这些数据类型对于编写高效、可维护的 PHP 代码至关重要。