做自(zì)由與創造的先行者

類型轉換

PHP中文(wén)手冊

PHP 在變量聲明(míng)時(shí)不需要定義類型。在這(zhè)種情況下(xià),變量的類型由存儲的值決定。也(yě)就是說,如果 string 賦值給 $var,然後 $var 的類型就是 string。之後将 int 值賦值給 $var,它将是 int 類型。

PHP 可能(néng)會(huì)嘗試在某些(xiē)上(shàng)下(xià)文(wén)中自(zì)動将值轉換爲另一種類型。不同的上(shàng)下(xià)文(wén)有:

Numeric

String

Logical

Integral and string

Comparative

Function

注意: 當值需要解釋爲不同類型時(shí),值本身不會(huì)改變類型。

強制将變量當做某種變量來(lái)求值,參見類型轉換一節。要更改變量的類型,請(qǐng)參閱 settype() 函數。

Numeric contexts

This is the context when using an arithmetical operator.

在這(zhè)種情況下(xià),如果任一運算(suàn)對(duì)象是 float(或者不能(néng)解釋爲 int),則兩個運算(suàn)對(duì)象都将解釋爲 float,結果也(yě)将是 float。否則,運算(suàn)對(duì)象将解釋爲 int,結果也(yě)将是 int。自(zì) PHP 8.0.0 起,如果無法解釋其中一個運算(suàn)對(duì)象,則會(huì)抛出 TypeError。

String contexts

This is the context when using echo, print, string interpolation, or the string concatenation operator.

這(zhè)種情況下(xià),值将會(huì)解釋爲 string。如果值無法解釋,那麽會(huì)抛出 TypeError。在 PHP 7.4.0 之前,會(huì)引發 E_RECOVERABLE_ERROR。

Logical contexts

This is the context when using conditional statements, the ternary operator, or a logical operator.

在這(zhè)種情況下(xià),值将會(huì)解釋爲 bool。

Integral and string contexts

This is the context when using a bitwise operators.

在這(zhè)種情況下(xià),如果所有的運算(suàn)對(duì)象都是 string,則結果也(yě)将是 string。否則運算(suàn)對(duì)象将解釋爲 int,結果也(yě)将是 int。如果其中一個運算(suàn)對(duì)象無法解釋,則會(huì)抛出 TypeError。

Comparative contexts

This is the context when using a comparison operator.

在此上(shàng)下(xià)文(wén)中發生的類型轉換在比較多種類型表中進行了(le)說明(míng)。

Function contexts ¶

This is the context when a value is passed to a typed parameter, property, or returned from a function which declares a return type.

在此上(shàng)下(xià)文(wén)中,值必須是類型值。但(dàn)存在兩個例外(wài),第一個是如果值爲 int,但(dàn)聲明(míng)的類型是 float,然後整數會(huì)轉換爲浮點數。第二個是如果聲明(míng)的類型是 scalar 類型,值可轉換爲标量類型,并且強制類型模式處于活動狀态(默認),值會(huì)轉換爲可接受的标量值。參見下(xià)文(wén)查看(kàn)有關此行爲的描述。

警告

内部函數自(zì)動将 null 轉換爲标量類型,此行爲自(zì) PHP 8.1.0 起棄用(yòng)。

使用(yòng)簡單類型聲明(míng)的強制類型 ¶

bool 類型聲明(míng):值将解釋爲 bool。

int 類型聲明(míng):如果明(míng)确定義轉換,則值将解釋爲 int。例如,字符串是數字。

float 類型聲明(míng):如果明(míng)确定義轉換,則值将解釋爲 float。例如,字符串是數字。

string 類型聲明(míng):值将解釋爲 string。

使用(yòng)聯合類型的強制類型 ¶

When strict_types is not enabled, scalar type declarations are subject to limited implicit type coercions. 如果值的精确類型不是聯合的一部分,然後會(huì)按照以下(xià)優先順序選擇目标類型:

int

float

string

bool

If the type both exists in the union, and the value can be coerced to the type under PHPs existing type checking semantics, then the type is chosen. Otherwise, the next type is tried.

警告

一個例外(wài),如果值是字符串,并且 int 和(hé) float 都是聯合類型的一部分,首選類型則通過現(xiàn)有的數字字符串語義決定。例如 "42" 選擇 int,"42.0" 選擇 float。

注意:

Types that are not part of the above preference list are not eligible targets for implicit coercion. In particular no implicit coercions to the null, false, and true types occur.

示例 #1 Example of types being coerced into a type part of the union

<?php

// int|string

42 --> 42 // exact type

"42" --> "42" // exact type

new ObjectWithToString --> "Result of __toString()"

// object never compatible with int, fall back to string

42.0 --> 42 // float compatible with int

42.1 --> 42 // float compatible with int

1e100 --> "1.0E+100" // float too large for int type, fall back to string

INF --> "INF" // float too large for int type, fall back to string

true --> 1 // bool compatible with int

[] --> TypeError // array not compatible with int or string

// int|float|bool

"45" --> 45 // int numeric string

"45.0" --> 45.0 // float numeric string

"45X" --> true // not numeric string, fall back to bool

"" --> false // not numeric string, fall back to bool

"X" --> true // not numeric string, fall back to bool

[] --> TypeError // array not compatible with int, float or bool

?>

類型轉換 ¶

類型轉換通過在值前面的括号中寫入類型來(lái)将值轉換指定的類型。

<?php

$foo = 10; // $foo 是 int

$bar = (bool) $foo; // $bar 是 bool

?>

允許的轉換是:

(int) ——轉換爲 int

(bool) ——轉換爲 bool

(float) ——轉換爲 float

(string) ——轉換爲 string

(array) ——轉換爲 array

(object) ——轉換爲 object

(unset) ——轉換爲 NULL

注意:

(integer) 是 (int) 轉換的别名。(boolean) 是 (bool) 轉換的别名。(binary) 是 (string) 轉換的别名。(double) 和(hé) (real) 是 (float) 轉換的别名。這(zhè)些(xiē)轉換不使用(yòng)标準的類型名稱,不推薦使用(yòng)。

警告

自(zì) PHP 8.0.0 起棄用(yòng) (real) 轉換别名。

警告

自(zì) PHP 7.2.0 起棄用(yòng) (unset) 轉換。注意 (unset) 轉換等同于将值 NULL 通過賦值或者調用(yòng)給變量。自(zì) PHP 8.0.0 起移除 unset 轉換。

警告

向前兼容 (binary) 轉換和(hé) b 前綴轉換。注意 (binary) 轉換和(hé) (string) 相同,但(dàn)是這(zhè)可能(néng)會(huì)改變且不應依賴。

注意:

在轉換的括号内忽略空(kōng)格。因此,以下(xià)兩個轉換是等價的:

<?php

$foo = (int) $bar;

$foo = ( int ) $bar;

?>

将文(wén)字 string 和(hé)變量轉換爲二進制 string:

<?php

$binary = (binary) $string;

$binary = b"binary string";

?>

注意: 除了(le)将變量轉換爲 string 之外(wài),還可以将變量放(fàng)在雙引号内。

<?php

$foo = 10; // $foo 是 int

$str = "$foo"; // $str 是 string

$fst = (string) $foo; // $fst 也(yě)是 string

// 打印出 "they are the same"

if ($fst === $str) {

echo "they are the same";

}

?>

有時(shí)在類型之間轉換時(shí)确切地會(huì)發生什(shén)麽可能(néng)不是很(hěn)明(míng)顯。更多信息見如下(xià)不分:

轉換爲 bool

轉換爲 int

轉換爲 float

轉換爲 string

轉換爲 array

轉換爲 object

轉換爲 resource

轉換爲 NULL

類型比較表

注意: 因爲 PHP 的 string 支持使用(yòng)與 array 索引相同的語法,通過偏移量進行索引,所以以下(xià)示例适用(yòng)于所有 PHP 版本:

<?php

$a = 'car'; // $a 是 string

$a[0] = 'b'; // $a 依然是 string

echo $a; // bar

?>

請(qǐng)查看(kàn)章節标題爲存取和(hé)修改字符串中的字符獲取更多信息。

網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發
上(shàng)一篇:類型聲明(míng)