导航
×
   ❮   
HTML CSS JavaScript PHP Go ECMS

PHP 类型转换


有时需要将变量从一种数据类型转换为另一种数据类型,有时则希望变量具有特定的数据类型。这可以通过类型转换来完成。


更改数据类型

PHP 中的类型转换通过以下语句完成:

  • (string) - 转换为字符串数据类型
  • (int) - 转换为整数数据类型
  • (float) - 转换为浮点数数据类型
  • (bool) - 转换为布尔数据类型
  • (array) - 转换为数组数据类型
  • (object) - 转换为对象数据类型
  • (unset) - 转换为 NULL 数据类型

转换为字符串

要转换为字符串,请使用 (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;

//To verify the type of any object in PHP, use the var_dump() function:
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
亲自试一试 »

转换为整数

要转换为整数,请使用 (int) 语句。

实例

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "25 kilometers"; // String
$d = "kilometers 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;
亲自试一试 »

转换为浮点数

要转换为浮点数,请使用 (float) 语句。

实例

$a = 5;       // Integer
$b = 5.34;    // Float
$c = "25 kilometers"; // String
$d = "kilometers 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;
亲自试一试 »

转换为布尔值

要转换为布尔值,请使用 (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;
亲自试一试 »

如果值为 0、NULL、false 或空,则 (bool) 将其转换为 false,否则转换为 true。

即使是 -1 也会转换为 true。


转换为数组

要转换为数组,请使用 (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 值将转换为一个空数组对象。

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

实例

将对象转换为数组

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;       // 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"); // indexed array
$b = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); // associative array

$a = (object) $a;
$b = (object) $b;
亲自试一试 »

转换为 NULL

要转换为 NULL,请使用 (unset) 语句。

实例

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

$a = (unset) $a;
$b = (unset) $b;
$c = (unset) $c;
$d = (unset) $d;
$e = (unset) $e;
亲自试一试 »

freew3c.com 中文网是独立运营的中文开发者学习平台,与 freew3c.com 无关联。提供的内容仅用于学习和测试,不保证内容的正确性。


Copyright @2020-2026 京ICP备888888号-8