您的位置:首页 > 编程语言 > PHP开发

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

2017-03-17 14:36 585 查看
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

--------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: