PHP 简明教程

PHP 类型转换

1. PHP 类型转换 (Type Casting)

PHP 类型转换(Type Casting)是指将一个值从一种数据类型显式转换为另一种数据类型的过程,例如将浮点数转换为整数。

这使得开发者能够直接控制变量的数据类型。
要对变量进行类型转换,您需要在想要转换的变量或值之前加上括号,并在括号内指定目标类型。

PHP 中的类型转换运算符包括:

  • (string) - 转换为字符串数据类型 (String)
  • (int) - 转换为整型数据类型 (Integer)
  • (float) - 转换为浮点型数据类型 (Float)
  • (bool) - 转换为布尔数据类型 (Boolean)
  • (array) - 转换为数组数据类型 (Array)
  • (object) - 转换为对象数据类型 (Object)
  • (unset) - 已弃用。转换为 NULL 数据类型

2. 转换为字符串 (String)

要转换为字符串数据类型:在需要转换的变量或值之前使用 (string)

示例

$a = 5;       // Integer (整型)
$b = 5.34;    // Float (浮点型)
$c = "hello"; // String (字符串)
$d = true;    // Boolean (布尔型)
$e = NULL;    // NULL

$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;

// 使用 var_dump() 验证数据类型
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);

3. 转换为整型 (Integer)

要转换为整型数据类型:在需要转换的变量或值之前使用 (int)

示例

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "25 km"; // String
$d = "km 25"; // String
$e = "hello"; // String
$f = true;    // Boolean
$g = NULL;    // NULL

$a = (int) $a;
$b = (int) $b;
$c = (int) $c;
$d = (int) $d;
$e = (int) $e;
$f = (int) $f;
$g = (int) $g;

4. 转换为浮点型 (Float)

要转换为浮点数据类型:在需要转换的变量或值之前使用 (float)

示例

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "25 km"; // String
$d = "km 25"; // String
$e = "hello"; // String
$f = true;    // Boolean
$g = NULL;    // NULL

$a = (float) $a;
$b = (float) $b;
$c = (float) $c;
$d = (float) $d;
$e = (float) $e;
$f = (float) $f;
$g = (float) $g;

5. 转换为布尔型 (Boolean)

要转换为布尔数据类型:在需要转换的变量或值之前使用 (bool)

示例

$a = 5;       // Integer
$b = 5.34;    // Float
$c = 0;       // Integer
$d = -1;      // Integer
$e = 0.1;     // Float
$f = "hello"; // String
$g = "";      // String
$h = true;    // Boolean
$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;

如果一个值为 0NULLfalse 或为空(empty),(bool) 会将其转换为 false,否则转换为 true。即使是 -1 也会被转换为 true

6. 转换为数组 (Array)

要转换为数组数据类型:在需要转换的变量或值之前使用 (array)

示例

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

$a = (array) $a;
$b = (array) $b;
$c = (array) $c;
$d = (array) $d;
$e = (array) $e;

转换为数组时,大多数数据类型都会转换为只有一个元素的索引数组。

NULL 值会转换为一个空的数组对象。

对象(Object)会转换为一个关联数组,其中对象的属性名将成为数组的键,属性值将成为数组的值:

示例

将对象转换为数组:

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);

7. 转换为对象 (Object)

要转换为对象数据类型:在需要转换的变量或值之前使用 (object)

示例

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

$a = (object) $a;
$b = (object) $b;
$c = (object) $c;
$d = (object) $d;
$e = (object) $e;

转换为对象时,大多数数据类型会转换为只有一个属性(名为 "scalar")的对象,该属性的值就是被转换的值。

NULL 值会转换为一个空对象。

索引数组会转换为对象,其中索引号将成为属性名,对应的值将成为属性值。

关联数组会转换为对象,其中数组的键将成为属性名,对应的值将成为属性值。

示例

将数组转换为对象:

$a = array("Volvo", "BMW", "Toyota"); // 索引数组
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); // 关联数组

$a = (object) $a;
$b = (object) $b;

8. 转换为 NULL

注意: (unset) 语句在 PHP 7.2.0 中已被弃用,并在 PHP 8.0.0 中被移除。

要转换为 NULL,只需直接将变量赋值为 NULL 即可:

示例

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "hello"; // String
$d = true;    // Boolean
$e = NULL;    // NULL

$a = NULL;
$b = NULL;
$c = NULL;
$d = NULL;
$e = NULL;