在PHP中将字符串转换为long int的正确方法 [英] Proper way of converting string to long int in PHP
问题描述
我尝试了(int)4209531264
和 intval(4209531264)
所有我得到的是 2147483647
(我意识到这是因为32位架构或一些php依赖或某些东西)。
I tried (int) "4209531264"
and intval("4209531264")
but sadly all I get is 2147483647
(I realize this is because of 32 bit architecture or some php dependencies or something).
我想出了4209531264+ 0
它返回正确的结果,但它是令人惊讶的看到它工作,因为它超出了maxint。
I came up with "4209531264" + 0
which returns the correct result but it is surprising to see it working since it is beyond maxint.
但真正的问题:这是将字符串转换为long的正确方法?
but the real question: is this the "right way" of converting string to long?
编辑:
(float)
即是。
推荐答案
只要你不是特别关注什么样的价值, $ c>number+ 0 可能是转换输入的最佳方式,因为它转换为自然数值数据类型。
As long as you are not very particular about what exactly kind of value you end up with, "number" + 0
is probably the best way of converting your input because it converts to the "natural" numeric data type.
如果输入没有小数部分和拟合(见 PHP_INT_MAX
),结果将是一个整数,如果没有小数部分,结果将是一个整数。
The result will be an integer if the input has no decimal part and fits (see PHP_INT_MAX
) or a float if it does not.
这篇关于在PHP中将字符串转换为long int的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!