Proper way of converting string to long int in PHP

对着背影说爱祢 提交于 2019-12-01 13:51:25

问题


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).

I came up with "4209531264" + 0 which returns the correct result but it is surprising to see it working since it is beyond maxint.

but the real question: is this the "right way" of converting string to long?

edit:

(float) that is.

thanks for the comments! eye opening!


回答1:


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.

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.



来源:https://stackoverflow.com/questions/24002152/proper-way-of-converting-string-to-long-int-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!