做(zuò)自(zì)由與創造的先行(xíng)者

類型轉換

PHP中文手冊

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

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

Numeric

String

Logical

Integral and string

Comparative

Function

注意: 當值需要(yào)解釋為(wèi)不同類型時(shí),值本身不會改變類型。

強制将變量當做(zuò)某種變量來求值,參見(jiàn)類型轉換一(yī)節。要(yào)更改變量的類型,請(qǐng)參閱 settype() 函數(shù)。

Numeric contexts

This is the context when using an arithmetical operator.

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

String contexts

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

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

Logical contexts

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

在這(zhè)種情況下(xià),值将會解釋為(wèi) bool。

Integral and string contexts

This is the context when using a bitwise operators.

在這(zhè)種情況下(xià),如(rú)果所有的運算對象都(dōu)是 string,則結果也将是 string。否則運算對象将解釋為(wèi) int,結果也将是 int。如(rú)果其中一(yī)個運算對象無法解釋,則會抛出 TypeError。

Comparative contexts

This is the context when using a comparison operator.

在此上(shàng)下(xià)文中發生的類型轉換在比較多種類型表中進行(xíng)了(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à)文中,值必須是類型值。但(dàn)存在兩個例外(wài),第一(yī)個是如(rú)果值為(wèi) int,但(dàn)聲明(míng)的類型是 float,然後整數(shù)會轉換為(wèi)浮點數(shù)。第二個是如(rú)果聲明(míng)的類型是 scalar 類型,值可(kě)轉換為(wèi)标量類型,并且強制類型模式處于活動狀态(默認),值會轉換為(wèi)可(kě)接受的标量值。參見(jiàn)下(xià)文查看(kàn)有關此行(xíng)為(wèi)的描述。

警告

內(nèi)部函數(shù)自(zì)動将 null 轉換為(wèi)标量類型,此行(xíng)為(wèi)自(zì) PHP 8.1.0 起棄用。

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

bool 類型聲明(míng):值将解釋為(wèi) bool。

int 類型聲明(míng):如(rú)果明(míng)确定義轉換,則值将解釋為(wèi) int。例如(rú),字符串是數(shù)字。

float 類型聲明(míng):如(rú)果明(míng)确定義轉換,則值将解釋為(wèi) float。例如(rú),字符串是數(shù)字。

string 類型聲明(míng):值将解釋為(wèi) string。

使用聯合類型的強制類型 ¶

When strict_types is not enabled, scalar type declarations are subject to limited implicit type coercions. 如(rú)果值的精确類型不是聯合的一(yī)部分,然後會按照以下(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.

警告

一(yī)個例外(wài),如(rú)果值是字符串,并且 int 和(hé) float 都(dōu)是聯合類型的一(yī)部分,首選類型則通(tōng)過現有的數(shù)字字符串語義決定。例如(rú) "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

?>

類型轉換 ¶

類型轉換通(tōng)過在值前面的括号中寫入類型來将值轉換指定的類型。

<?php

$foo = 10; // $foo 是 int

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

?>

允許的轉換是:

(int) ——轉換為(wèi) int

(bool) ——轉換為(wèi) bool

(float) ——轉換為(wèi) float

(string) ——轉換為(wèi) string

(array) ——轉換為(wèi) array

(object) ——轉換為(wèi) object

(unset) ——轉換為(wèi) NULL

注意:

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

警告

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

警告

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

警告

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

注意:

在轉換的括号內(nèi)忽略空格。因此,以下(xià)兩個轉換是等價的:

<?php

$foo = (int) $bar;

$foo = ( int ) $bar;

?>

将文字 string 和(hé)變量轉換為(wèi)二進制 string:

<?php

$binary = (binary) $string;

$binary = b"binary string";

?>

注意: 除了(le)将變量轉換為(wèi) string 之外(wài),還可(kě)以将變量放在雙引号內(nèi)。

<?php

$foo = 10; // $foo 是 int

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

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

// 打印出 "they are the same"

if ($fst === $str) {

echo "they are the same";

}

?>

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

轉換為(wèi) bool

轉換為(wèi) int

轉換為(wèi) float

轉換為(wèi) string

轉換為(wèi) array

轉換為(wèi) object

轉換為(wèi) resource

轉換為(wèi) NULL

類型比較表

注意: 因為(wèi) PHP 的 string 支持使用與 array 索引相同的語法,通(tōng)過偏移量進行(xíng)索引,所以以下(xià)示例适用于所有 PHP 版本:

<?php

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

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

echo $a; // bar

?>

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

網站建設開(kāi)發|APP設計開(kāi)發|小程序建設開(kāi)發
上(shàng)一(yī)篇:類型聲明(míng)