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;
自己動手試一試 »