W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵(lì)
PHP 在變量聲明時(shí)不需要定義類型。在這種情況下,變量的類型由存儲的值決定。也就是說,如果 string 賦值給 $var,然后 $var 的類型就是 string。之后將 int 值賦值給 $var,它將是 int 類型。
PHP 可能會嘗試在某些上下文中自動將值轉(zhuǎn)換為另一種類型。不同的上下文有:
注意: 當(dāng)值需要解釋為不同類型時(shí),值本身不會改變類型。
強(qiáng)制將變量當(dāng)做某種變量來求值,參見類型轉(zhuǎn)換一節(jié)。要更改變量的類型,請參閱 settype() 函數(shù)。
This is the context when using an arithmetical operator.
在這種情況下,如果任一運(yùn)算對象是 float(或者不能解釋為 int),則兩個(gè)運(yùn)算對象都將解釋為 float,結(jié)果也將是 float。否則,運(yùn)算對象將解釋為 int,結(jié)果也將是 int。自 PHP 8.0.0 起,如果無法解釋其中一個(gè)運(yùn)算對象,則會拋出 TypeError。
This is the context when using echo, print, string interpolation, or the string concatenation operator.
這種情況下,值將會解釋為 string。如果值無法解釋,那么會拋出 TypeError。在 PHP 7.4.0 之前,會引發(fā) E_RECOVERABLE_ERROR。
This is the context when using conditional statements, the ternary operator, or a logical operator.
在這種情況下,值將會解釋為 bool。
This is the context when using a bitwise operators.
在這種情況下,如果所有的運(yùn)算對象都是 string,則結(jié)果也將是 string。否則運(yùn)算對象將解釋為 int,結(jié)果也將是 int。如果其中一個(gè)運(yùn)算對象無法解釋,則會拋出 TypeError。
This is the context when using a comparison operator.
在此上下文中發(fā)生的類型轉(zhuǎn)換在比較多種類型表中進(jìn)行了說明。
This is the context when a value is passed to a typed parameter, property, or returned from a function which declares a return type.
在此上下文中,值必須是類型值。但存在兩個(gè)例外,第一個(gè)是如果值為 int,但聲明的類型是 float,然后整數(shù)會轉(zhuǎn)換為浮點(diǎn)數(shù)。第二個(gè)是如果聲明的類型是 scalar 類型,值可轉(zhuǎn)換為標(biāo)量類型,并且強(qiáng)制類型模式處于活動狀態(tài)(默認(rèn)),值會轉(zhuǎn)換為可接受的標(biāo)量值。參見下文查看有關(guān)此行為的描述。
警告
內(nèi)部函數(shù)自動將 null 轉(zhuǎn)換為標(biāo)量類型,此行為自 PHP 8.1.0 起棄用。
When strict_types is not enabled, scalar type declarations are subject to limited implicit type coercions. 如果值的精確類型不是聯(lián)合的一部分,然后會按照以下優(yōu)先順序選擇目標(biāo)類型:
If the type exists in the union and the value can be coerced to the type under PHP's existing type-checking semantics, then the type is chosen. Otherwise, the next type is tried.
警告
一個(gè)例外,如果值是字符串,并且 int 和 float 都是聯(lián)合類型的一部分,首選類型則通過現(xiàn)有的數(shù)字字符串語義決定。例如 "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
?>
類型轉(zhuǎn)換通過在值前面的括號中寫入類型來將值轉(zhuǎn)換指定的類型。
<?php
$foo = 10; // $foo 是 int
$bar = (bool) $foo; // $bar 是 bool
?>
允許的轉(zhuǎn)換是:
注意:(integer) 是 (int) 轉(zhuǎn)換的別名。(boolean) 是 (bool) 轉(zhuǎn)換的別名。(binary) 是 (string) 轉(zhuǎn)換的別名。(double) 和 (real) 是 (float) 轉(zhuǎn)換的別名。這些轉(zhuǎn)換不使用標(biāo)準(zhǔn)的類型名稱,不推薦使用。
警告
自 PHP 8.0.0 起棄用 (real) 轉(zhuǎn)換別名。
警告
自 PHP 7.2.0 起棄用 (unset) 轉(zhuǎn)換。注意 (unset) 轉(zhuǎn)換等同于將值 NULL 通過賦值或者調(diào)用給變量。自 PHP 8.0.0 起移除 unset 轉(zhuǎn)換。
警告
向前兼容 (binary) 轉(zhuǎn)換和 b 前綴轉(zhuǎn)換。注意 (binary) 轉(zhuǎn)換和 (string) 相同,但是這可能會改變且不應(yīng)依賴。
注意:在轉(zhuǎn)換的括號內(nèi)忽略空格。因此,以下兩個(gè)轉(zhuǎn)換是等價(jià)的:
將文字 string 和變量轉(zhuǎn)換為二進(jìn)制 string:
<?php
$binary = (binary) $string;
$binary = b"binary string";
?>
注意: 除了將變量轉(zhuǎn)換為 string 之外,還可以將變量放在雙引號內(nèi)。
有時(shí)在類型之間轉(zhuǎn)換時(shí)確切地會發(fā)生什么可能不是很明顯。更多信息見如下不分:
注意: 因?yàn)?PHP 的 string 支持使用與 array 索引相同的語法,通過偏移量進(jìn)行索引,所以以下示例適用于所有 PHP 版本:請查看章節(jié)標(biāo)題為存取和修改字符串中的字符獲取更多信息。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: