手册
目录
在 PHP 中进行类型转换可以使用以下语句:
(string) - 转换为字符串类型(int) - 转换为整数类型(float) - 转换为浮点类型(bool) - 转换为布尔类型(array) - 转换为数组类型(object) - 转换为对象类型(unset) - 转换为 NULL 类型要转换为字符串,请使用 (string) 语句:
$a = 5; // 整数 $b = 5.34; // 浮点数 $c = "hello"; // 字符串 $d = true; // 布尔值 $e = NULL; // NULL $a = (string) $a; $b = (string) $b; $c = (string) $c; $d = (string) $d; $e = (string) $e; // 要验证 PHP 中任何对象的类型,请使用 var_dump() 函数: var_dump($a); var_dump($b); var_dump($c); var_dump($d); var_dump($e);运行实例 »
点击 "运行实例" 按钮查看在线实例
要转换为整数,请使用 (int) 语句:
$a = 5; // 整数 $b = 5.34; // 浮点数 $c = "25 kilometers"; // 字符串 $d = "kilometers 25"; // 字符串 $e = "hello"; // 字符串 $f = true; // 布尔值 $g = NULL; // NULL $a = (int) $a; $b = (int) $b; $c = (int) $c; $d = (int) $d; $e = (int) $e; $f = (int) $f; $g = (int) $g;运行实例 »
点击 "运行实例" 按钮查看在线实例
要转换为浮点数,请使用 (float) 语句:
$a = 5; // 整数 $b = 5.34; // 浮点数 $c = "25 kilometers"; // 字符串 $d = "kilometers 25"; // 字符串 $e = "hello"; // 字符串 $f = true; // 布尔值 $g = NULL; // NULL $a = (float) $a; $b = (float) $b; $c = (float) $c; $d = (float) $d; $e = (float) $e; $f = (float) $f; $g = (float) $g;运行实例 »
点击 "运行实例" 按钮查看在线实例
要转换为布尔值,请使用 (bool) 语句:
$a = 5; // 整数 $b = 5.34; // 浮点数 $c = 0; // 整数 $d = -1; // 整数 $e = 0.1; // 浮点数 $f = "hello"; // 字符串 $g = ""; // 字符串 $h = true; // 布尔值 $i = NULL; // NULL $a = (bool) $a; $b = (bool) $b; $c = (bool) $c; $d = (bool) $d; $e = (bool) $e; $f = (bool) $f; $g = (bool) $g; $h = (bool) $h; $i = (bool) $i;运行实例 »
点击 "运行实例" 按钮查看在线实例
如果值为 0、NULL、false 或为空,则 (bool) 会将其转换为 false,否则为 true。
甚至 -1 也会转换为 true。
要转换为数组,请使用 (array) 语句:
$a = 5; // 整数 $b = 5.34; // 浮点数 $c = "hello"; // 字符串 $d = true; // 布尔值 $e = NULL; // NULL $a = (array) $a; $b = (array) $b; $c = (array) $c; $d = (array) $d; $e = (array) $e;运行实例 »
点击 "运行实例" 按钮查看在线实例
转换为数组时,大多数数据类型都会转换为一个仅包含一个元素的索引数组。
NULL 值会转换为一个空的数组对象。
对象会转换为关联数组,其中属性名称成为键,属性值成为值:
将对象转换为数组:
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("red", "Volvo");
$myCar = (array) $myCar;
var_dump($myCar);
运行实例 »点击 "运行实例" 按钮查看在线实例
要转换为对象,请使用 (object) 语句:
$a = 5; // 整数 $b = 5.34; // 浮点数 $c = "hello"; // 字符串 $d = true; // 布尔值 $e = NULL; // NULL $a = (object) $a; $b = (object) $b; $c = (object) $c; $d = (object) $d; $e = (object) $e;运行实例 »
点击 "运行实例" 按钮查看在线实例
转换为对象时,大多数数据类型都会转换为一个仅包含一个名为 "scalar" 的属性和相应值的对象。
NULL 值会转换为一个空对象。
索引数组会转换为对象,其中索引号作为属性名称,值作为属性值。
关联数组会转换为对象,其中键作为属性名称,值作为属性值。
将数组转换为对象:
$a = array("Volvo", "BMW", "Audi"); // 索引数组
$b = array("Bill"=>"35", "Steve"=>"37", "Elon"=>"43"); // 关联数组
$a = (object) $a;
$b = (object) $b;
运行实例 »点击 "运行实例" 按钮查看在线实例
要转换为 NULL,请使用 (unset) 语句:
$a = 5; // 整数 $b = 5.34; // 浮点数 $c = "hello"; // 字符串 $d = true; // 布尔值 $e = NULL; // NULL $a = (unset) $a; $b = (unset) $b; $c = (unset) $c; $d = (unset) $d; $e = (unset) $e;运行实例 »
点击 "运行实例" 按钮查看在线实例
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
70.9万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习