在 PHP 中将字符串转换为 long int 的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24002152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way of converting string to long int in PHP
提问by Maciej Jankowski
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).
我尝试过(int) "4209531264"
,intval("4209531264")
但遗憾的是我得到的只是2147483647
(我意识到这是因为 32 位架构或某些 php 依赖项或其他原因)。
I came up with "4209531264" + 0
which returns the correct result but it is surprising to see it working since it is beyond maxint.
我想出了"4209531264" + 0
哪个返回正确的结果,但看到它工作令人惊讶,因为它超出了 maxint。
but the real question: is this the "right way" of converting string to long?
但真正的问题是:这是将字符串转换为 long 的“正确方法”吗?
edit:
编辑:
(float)
that is.
(float)
那是。
thanks for the comments! eye opening!
感谢您的评论!大开眼界!
采纳答案by Jon
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.
只要您对最终得到的值的类型不是很特别,"number" + 0
这可能是转换输入的最佳方式,因为它会转换为“自然”数字数据类型。
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_INT_MAX
),则结果将是一个整数,如果不适合,则结果将是一个浮点数。