1. 整数(Integer):
整数是没有小数部分的数字。可以是正整数、负整数或零。
$age = 25;
$temperature = -10;
2. 浮点数(Float):
浮点数是带有小数点或指数形式的数字。
$price = 19.99;
$scientificNotation = 5.4e2; // 表示 5.4 * 10^2
3. 字符串(String):
字符串是字符的序列,可以用单引号或双引号括起来。
$name = "John";
$greeting = 'Hello, World!';
4. 布尔值(Boolean):
布尔值表示真或假。在PHP中,true 和 false 是两个预定义的布尔值。
$isStudent = true;
$hasLicense = false;
5. 数组(Array):
数组是一个有序的、映射的数据集合。可以包含不同类型的值。
$colors = array("red", "green", "blue");
$person = array("name" => "John", "age" => 25);
6. 对象(Object):
对象是类的实例,具有属性和方法。
class Car {
public $model;
public $color;
public function honk() {
echo "Honk!";
}
}
$myCar = new Car();
$myCar->model = "Toyota";
$myCar->color = "blue";
$myCar->honk();
7. 空值(Null):
null 表示一个变量没有值。
$variableWithoutValue = null;
8. 资源(Resource):
资源是一种特殊的变量,保存了对外部资源的引用(如数据库连接、文件句柄等)。
$fileHandle = fopen("example.txt", "r");
这些是PHP中的基本数据类型。在变量使用过程中,PHP会根据上下文自动进行类型转换,因此你通常无需担心数据类型的问题。
转载请注明出处:http://www.zyzy.cn/article/detail/13789/PHP