乙酰氨基酚是什么药| 甲状腺看什么门诊| 吃饭流汗是什么原因| 654-2是什么药| 桃皮绒是什么面料| 什么晚霜比较好用| 心肌酶能查出什么病| 女性尿里带血是什么原因| 包饺子剩下的面团能做什么| 云南古代叫什么| 胺试验阳性是什么意思| 为什么一直下雨| 眉茶属于什么茶| 有什么游戏| 家乡是什么意思| 屈光检查是什么| 肝右叶钙化灶什么意思| 平板支撑是什么| 为什么会得肿瘤| 对虾是什么虾| 放行是什么意思| 猪肝能钓什么鱼| 尿道流脓吃什么药| 皮肤癣用什么药| 本科生是什么意思| 飞马是什么意思| 头昏脑胀吃什么药| 老年人腿疼是什么原因引起的| 眼皮重是什么原因| 4月1日是什么星座| 中年危机是什么意思| 什么叫职业年金| 肠炎吃什么药| 什么可以解酒| 为什么总是做噩梦| 麻蛇是什么蛇| 斯字五行属什么| 头痛呕吐什么原因| 什么叫cp| 做活检意味着什么| 做梦遗精是什么原因| 颠勺是什么意思| 干戈是什么意思| 接触隔离什么意思| 人到无求品自高什么意思| ccc是什么意思| tpc是什么意思| 放疗期间吃什么食物最好| dr胸部正位片是检查什么的| 高血压什么不能吃| 什么食物降尿酸效果好| 风土人情是什么意思| 高温丝假发是什么材质| 舌下腺囊肿挂什么科| 月经期间应该吃什么食物比较好| 重庆的市花是什么| 补睾丸吃什么药最好| leep是什么手术| 群聊名字什么最好听| 喝咖啡要注意什么| 心脏彩超能检查出什么| 虚恋是什么意思| 什么是煞气| 间是什么结构| 公安局属于什么机关| 男人下面出汗是什么原因| 上飞机不能带什么| 食物中毒挂什么科| 胃糜烂要吃什么药| 红细胞偏高是什么意思| 老人助听器什么牌子好| 炖牛肉放什么佐料| 高压低压是什么意思| 氨味是什么味道| 唇釉是什么| 鉴黄师是什么职业| 刚柔并济是什么意思| 长期吃面条对身体有什么影响| 刚满月的小狗吃什么| 8月1日是什么星座| 小便白细胞高是什么原因| 骨龄挂什么科| 无机盐是什么| 吹泡泡什么意思| 亚甲减是什么意思| 风声鹤唳的意思是什么| 什么的拳头| 乙肝两对半和乙肝五项有什么区别| 一什么蛇| 司法警察是做什么的| 来月经前胸胀痛什么原因| 九眼天珠是什么做的| 吃什么可以让卵泡长得快| 尿胆红素高是什么原因| 因什么制宜| 卡介苗是什么| 什么是腺样体面容| 孕妇为什么不能吃桃子| 头发白是什么原因引起的| 太平公主叫什么名字| 梦到丧事场面什么意思| 靖五行属性是什么| 黄金茶属于什么茶| fujixerox是什么牌子| 氯吡格雷治什么病| 婷婷玉立什么意思| 为什么不能抖腿| 夷是什么意思| 孑然一身是什么意思| 叼是什么意思| 虫草花是什么| 桥本甲状腺炎挂什么科| 普拉提和瑜伽有什么区别| 生吃大蒜有什么好处和坏处| 肝火是什么原因引起的| 小学什么时候放假| 心软是什么意思| 母亲节送母亲什么礼物| 龙抬头是什么意思| 太阳黑子是什么东西| 手术后吃什么水果| 孕期腰疼是什么原因| 维生素c高是什么原因| 脚背上长痣代表什么| 植物纤维是什么面料| 国民党为什么会失败| 参加白事回来注意什么| 男人跑马是什么原因| 提手旁的字有什么| 什么的竹笋| 发质硬适合什么发型| 乳腺导管扩张吃什么药| 北京友谊医院擅长什么| 属兔五行属什么| 慢性荨麻疹是什么症状| 胰腺的作用和功能是什么| 32周孕检检查什么项目| 胎盘下缘覆盖宫颈内口是什么意思| 莆田系是什么意思啊| 监督是什么意思| 什么是割包皮| 日本全称是什么| 副厅级是什么级别| 农历正月初一是什么节日| 感冒看什么科| 月经过后腰酸疼是什么原因| 抗体阳性什么意思| 什么的飞机| 吃什么白头发变黑| cpm是什么意思| 血脂高吃什么食物最好| 酸菜鱼一般加什么配菜| 游离三碘甲状腺原氨酸是什么意思| 每个月月经都提前是什么原因| 什么是抽动症| 专长是什么意思| 七情六欲指什么| 水是什么生肖| 止血敏又叫什么| 糖尿病筛查做什么检查| 天天都需要你爱是什么歌| 芙蓉是什么意思| 胎儿左心室灶状强回声是什么意思| tp是什么| 什么牌子的氨糖最好| smart什么牌子| 女人吃藕有什么好处| hankook是什么轮胎| 阴道没水什么原因| 顺丰为什么这么快| 什么的什么的词语| 检查hpv需要注意什么提前注意什么| 蜗牛的天敌是什么| 凤凰指什么生肖| 43岁属什么生肖| 早上起来头晕是什么原因| 夏天喝盐水有什么好处| 额头老出汗是什么原因| 高甘油三酯血症是什么意思| 钾离子低的原因是什么| 甲状腺球蛋白抗体低说明什么| 214是什么意思| 秋黄瓜什么时候种| 女生左手食指戴戒指什么意思| 蚊子会传染什么病| 瓜田李下是什么意思| 打马虎眼是什么意思| 无创是什么| 皮肤一块块白是什么病| 戍是什么意思| 吃黄体酮有什么副作用| 提心吊胆是什么生肖| 什么是天眼| 旅拍什么意思| 纯阴八字为什么要保密| 乳腺结节吃什么| 壁咚是什么意思| 发烧吃什么药退烧快| 眼睛发黄是什么原因引起的| 手心为什么老出汗| 手心热是什么原因| 什么人不能献血| 什么是扁平疣图片| 老年人适合吃什么水果| 宫颈糜烂用什么药好得快| 万加一笔是什么字| nda是什么| 三维b片主治什么病| 周易和易经有什么区别| 吃榴莲不能和什么一起吃| 喉咙发甜是什么原因| 以什么为准| 腘窝囊肿是什么原因引起的| 腰膝酸软是什么症状| 梦见鸡死了是什么预兆| 懵懂是什么意思| 豆豉是什么东西| 迷走神经是什么| 月经推迟什么原因| 大便很臭什么原因| 胀气吃什么药| 金字旁和什么有关| lsil什么意思| td是什么意思| 腹部增强ct能检查出什么| 什么是固态法白酒| 尿潴留吃什么药| 糜烂性胃炎吃什么药| 反社会人格有什么表现| 终亡其酒的亡是什么意思| 毕业送什么礼物给老师| 门客是什么意思| 菩提子是什么树的种子| 什么是质子| hpv42阳性是什么意思| 军绿色裤子配什么上衣| 豆浆什么时候喝最好| 什么的蘑菇| 陕西有什么烟| 女人腰疼是什么原因引起的| 北京为什么叫四九城| 双侧肾盂分离是什么意思| 静的部首是什么| 心什么神往| 日干是什么| vodka是什么酒| 鱼不能和什么一起吃| 10月30日是什么星座| 高压正常低压低是什么原因| 喉咙干痒咳嗽吃什么药| 小暑是什么| 靶向治疗是什么意思| 阴道炎挂什么科| 苏铁属于什么植物| 拔罐颜色紫黑代表什么| 什么对雨| 与会是什么意思| mio是什么意思| 什么是砭石| 为什么小孩子有白头发| 闲鱼卖出的东西钱什么时候到账| 黑热病是什么病| 女性长期缺维d会带来什么病| 金匮肾气丸有什么功效| 百度
PHP 8.5.0 Alpha 4 available for testing

Type Juggling

PHP does not require explicit type definition in variable declaration. In this case, the type of a variable is determined by the value it stores. That is to say, if a string is assigned to variable $var, then $var is of type string. If afterwards an int value is assigned to $var, it will be of type int.

PHP may attempt to convert the type of a value to another automatically in certain contexts. The different contexts which exist are:

  • Numeric
  • String
  • Logical
  • Integral and string
  • Comparative
  • Function

Note: When a value needs to be interpreted as a different type, the value itself does not change types.

To force a variable to be evaluated as a certain type, see the section on Type casting. To change the type of a variable, see the settype() function.

Numeric contexts

This is the context when using an arithmetical operator.

In this context if either operand is a float (or not interpretable as an int), both operands are interpreted as floats, and the result will be a float. Otherwise, the operands will be interpreted as ints, and the result will also be an int. As of PHP 8.0.0, if one of the operands cannot be interpreted a TypeError is thrown.

String contexts

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

In this context the value will be interpreted as string. If the value cannot be interpreted a TypeError is thrown. Prior to PHP 7.4.0, an E_RECOVERABLE_ERROR was raised.

Logical contexts

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

In this context the value will be interpreted as bool.

Integral and string contexts

This is the context when using bitwise operators.

In this context if all operands are of type string the result will also be a string. Otherwise, the operands will be interpreted as ints, and the result will also be an int. As of PHP 8.0.0, if one of the operands cannot be interpreted a TypeError is thrown.

Comparative contexts

This is the context when using a comparison operator.

The type conversions which occur in this context are explained in the Comparison with Various Types table.

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.

In this context the value must be a value of the type. Two exceptions exist, the first one is: if the value is of type int and the declared type is float, then the integer is converted to a floating point number. The second one is: if the declared type is a scalar type, the value is convertable to a scalar type, and the coercive typing mode is active (the default), the value may be converted to an accepted scalar value. See below for a description of this behaviour.

Warning

Internal functions automatically coerce null to scalar types, this behaviour is DEPRECATED as of PHP 8.1.0.

Coercive typing with simple type declarations

  • bool type declaration: value is interpreted as bool.
  • int type declaration: value is interpreted as int if the conversion is well-defined. For example the string is numeric.
  • float type declaration: value is interpreted as float if the conversion is well-defined. For example the string is numeric.
  • string type declaration: value is interpreted as string.

Coercive typing with union types

When strict_types is not enabled, scalar type declarations are subject to limited implicit type coercions. If the exact type of the value is not part of the union, then the target type is chosen in the following order of preference:

  1. int
  2. float
  3. string
  4. bool
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.

Caution

As an exception, if the value is a string and both int and float are part of the union, the preferred type is determined by the existing numeric string semantics. For example, for "42" int is chosen, while for "42.0" float is chosen.

Note:

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.

Example #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
?>

Type Casting

Type casting converts the value to a chosen type by writing the type within parentheses before the value to convert.

Example #2 Type Casting

<?php
$foo
= 10; // $foo is an integer
$bar = (bool) $foo; // $bar is a boolean

var_dump($bar);
?>

The casts allowed are:

  • (int) - cast to int
  • (bool) - cast to bool
  • (float) - cast to float
  • (string) - cast to string
  • (array) - cast to array
  • (object) - cast to object
  • (unset) - cast to NULL

Note:

(integer) is an alias of the (int) cast. (boolean) is an alias of the (bool) cast. (binary) is an alias of the (string) cast. (double) and (real) are aliases of the (float) cast. These casts do not use the canonical type name and are not recommended.

Warning

The (real) cast alias has been deprecated as of PHP 8.0.0.

Warning

The (unset) cast has been deprecated as of PHP 7.2.0. Note that the (unset) cast is the same as assigning the value NULL to the variable or call. The (unset) cast is removed as of PHP 8.0.0.

Caution

The (binary) cast and b prefix exists for forward support. Currently (binary) and (string) are identical, however this may change and should not be relied upon.

Note:

Whitespaces are ignored within the parentheses of a cast. Therefore, the following two casts are equivalent:

<?php
$foo
= (int) $bar;
$foo = ( int ) $bar;
?>

Casting literal strings and variables to binary strings:

<?php
$binary
= (binary) $string;
$binary = b"binary string";
?>

Instead of casting a variable to a string, it is also possible to enclose the variable in double quotes.

Example #3 Different Casting Mechanisms

<?php
$foo
= 10; // $foo is an integer
$str = "$foo"; // $str is a string
$fst = (string) $foo; // $fst is also a string

// This prints out that "they are the same"
if ($fst === $str) {
echo
"they are the same", PHP_EOL;
}
?>

It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections:

Note: Because PHP supports indexing into strings via offsets using the same syntax as array indexing, the following example holds true for all PHP versions:

Example #4 Using Array Offset with a String

<?php
$a
= 'car'; // $a is a string
$a[0] = 'b'; // $a is still a string
echo $a; // bar
?>
See the section titled String access by character for more information.

add a note

User Contributed Notes 6 notes

up
74
Raja
20 years ago
Uneven division of an integer variable by another integer variable will result in a float by automatic conversion -- you do not have to cast the variables to floats in order to avoid integer truncation (as you would in C, for example):

$dividend = 2;
$divisor = 3;
$quotient = $dividend/$divisor;
print $quotient; // 0.66666666666667
up
17
Anonymous
4 years ago
Cast operators have a very high precedence, for example (int)$a/$b is evaluated as ((int)$a)/$b, not as (int)($a/$b) [which would be like intdiv($a,$b) if both $a and $b are integers].
The only exceptions (as of PHP 8.0) are the exponentiation operator ** [i.e. (int)$a**$b is evaluated as (int)($a**$b) rather than ((int)$a)**$b] and the special access/invocation operators ->, ::, [] and () [i.e. in each of (int)$a->$b, (int)$a::$b, (int)$a[$b] and (int)$a($b), the cast is performed last on the result of the variable expression].
up
2
Anonymous
9 months ago
Type casting in expressions is executed first.
The casting is assigned to the value, not to the expression result.
Examples:

<?php

$string
= "777";

var_dump( $string === 777 ); // FALSE
var_dump( (int) $string === 777 ); // TRUE
var_dump( ( (int) $string ) === 777 ); // TRUE
var_dump( (int) ( $string === 777 ) ); // 0
?>
up
26
fardelian
12 years ago
Casting objects to arrays is a pain. Example:

<?php

class MyClass {

private
$priv = 'priv_value';
protected
$prot = 'prot_value';
public
$pub = 'pub_value';
public
$MyClasspriv = 'second_pub_value';

}

$test = new MyClass();
echo
'<pre>';
print_r((array) $test);

/*
Array
(
[MyClasspriv] => priv_value
[*prot] => prot_value
[pub] => pub_value
[MyClasspriv] => second_pub_value
)
*/

?>

Yes, that looks like an array with two keys with the same name and it looks like the protected field was prepended with an asterisk. But that's not true:

<?php

foreach ((array) $test as $key => $value) {
$len = strlen($key);
echo
"{$key} ({$len}) => {$value}<br />";
for (
$i = 0; $i < $len; ++$i) {
echo
ord($key[$i]) . ' ';
}
echo
'<hr />';
}

/*
MyClasspriv (13) => priv_value
0 77 121 67 108 97 115 115 0 112 114 105 118
*prot (7) => prot_value
0 42 0 112 114 111 116
pub (3) => pub_value
112 117 98
MyClasspriv (11) => second_pub_value
77 121 67 108 97 115 115 112 114 105 118
*/

?>

The char codes show that the protected keys are prepended with '\0*\0' and private keys are prepended with '\0'.__CLASS__.'\0' so be careful when playing around with this.
up
14
miracle at 1oo-percent dot de
19 years ago
If you want to convert a string automatically to float or integer (e.g. "0.234" to float and "123" to int), simply add 0 to the string - PHP will do the rest.

e.g.

$val = 0 + "1.234";
(type of $val is float now)

$val = 0 + "123";
(type of $val is integer now)
up
19
Anonymous
22 years ago
Printing or echoing a FALSE boolean value or a NULL value results in an empty string:
(string)TRUE //returns "1"
(string)FALSE //returns ""
echo TRUE; //prints "1"
echo FALSE; //prints nothing!
To Top
7朵玫瑰花代表什么意思 杭州灵隐寺求什么最灵 科学的尽头是什么 鸽子怕什么怎么赶走 眼皮浮肿是什么原因引起的
梦见吃花生是什么意思 心脏扩大吃什么药好 1985年属什么生肖 扁桃体切除对身体有什么影响 走路脚后跟疼是什么原因
女人左手掌有痣代表什么 导盲犬一般是什么品种 玫瑰茄是什么东西 hpv跟tct有什么区别 婆娑是什么意思
圆坟是什么意思 红细胞分布宽度偏低是什么原因 出气不顺畅是什么原因 太阳鱼是什么鱼 言谈举止是什么意思
为什么泡完脚后非常痒hcv9jop7ns9r.cn 怀孕前壁和后壁有什么区别hcv9jop0ns6r.cn 兔子的眼睛为什么是红色的hcv7jop5ns0r.cn 为什么会岔气hcv8jop5ns3r.cn 肾结水是什么原因造成的hcv9jop0ns3r.cn
见性成佛是什么意思hcv9jop6ns3r.cn 脚经常抽筋是什么原因hcv8jop8ns4r.cn 血糖高有什么表现hcv9jop3ns5r.cn 蜻蜓像什么hcv9jop2ns6r.cn 康波是什么意思hcv7jop6ns0r.cn
慈爱是什么意思hcv9jop7ns4r.cn 饸烙面是什么面hcv9jop5ns8r.cn 马牛羊鸡犬豕中的豕指的是什么hcv8jop9ns8r.cn 朗姆酒是什么酒1949doufunao.com 堂妹是什么关系hcv7jop6ns5r.cn
莆田荔枝什么时候成熟hcv9jop2ns8r.cn 感冒流清鼻涕吃什么药gysmod.com 32岁属什么aiwuzhiyu.com 自愈什么意思xianpinbao.com 喉咙痛鼻塞吃什么药hcv8jop2ns0r.cn
百度