PHP intval() 和 (int) 转换的区别

1、intval & (int) 都不可以转换 Object。

2、转换效率 (int) > intval() > sprintf (intval 是PHP内置的方法,效率相对低)。

3、其他完全相同。

4、输入0123, 0x123 (int)函数也会按八进制和十六进制转换。


测试代码:

$n="19.99";
$example = array(
    19, '19', 19.99, '19.99','', 'abc123','123abc', 0x20, '0x20', array('19.99'), '+41', '-41',
    '10000000000000000000000000000000', (float)1999, null, $n*100, strval($n*100), 'j18ugj9hu0gj5hg',0123

);

foreach ($example as $key => $value){
    echo 'The Transform Value Is: ' . gettype($value) . '  ' . json_encode($value) . "<br>";
    echo "intval is :" , intval($value) . "<br>";
    echo "(int) is :" , (int)($value) . "<br>";
    echo "--------------------------------" . "<br>";
}

运行结果:

The Transform Value Is: integer 19
intval is :19
(int) is :19
--------------------------------
The Transform Value Is: string "19"
intval is :19
(int) is :19
--------------------------------
The Transform Value Is: double 19.99
intval is :19
(int) is :19
--------------------------------
The Transform Value Is: string "19.99"
intval is :19
(int) is :19
--------------------------------
The Transform Value Is: string ""
intval is :0
(int) is :0
--------------------------------
The Transform Value Is: string "abc123"
intval is :0
(int) is :0
--------------------------------
The Transform Value Is: string "123abc"
intval is :123
(int) is :123
--------------------------------
The Transform Value Is: integer 32
intval is :32
(int) is :32
--------------------------------
The Transform Value Is: string "0x20"
intval is :0
(int) is :0
--------------------------------
The Transform Value Is: array ["19.99"]
intval is :1
(int) is :1
--------------------------------
The Transform Value Is: string "+41"
intval is :41
(int) is :41
--------------------------------
The Transform Value Is: string "-41"
intval is :-41
(int) is :-41
--------------------------------
The Transform Value Is: string "10000000000000000000000000000000"
intval is :2147483647
(int) is :2147483647
--------------------------------
The Transform Value Is: double 1999
intval is :1999
(int) is :1999
--------------------------------
The Transform Value Is: NULL null
intval is :0
(int) is :0
--------------------------------
The Transform Value Is: double 1999
intval is :1998
(int) is :1998
--------------------------------
The Transform Value Is: string "1999"
intval is :1999
(int) is :1999
--------------------------------
The Transform Value Is: string "j18ugj9hu0gj5hg"
intval is :0
(int) is :0
--------------------------------
The Transform Value Is: integer 83
intval is :83
(int) is :83
--------------------------------











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值