开发者

How to cast long to string?

开发者 https://www.devze.com 2023-02-12 03:39 出处:网络
I need to cast a tweet id, which is a long, to a string. For instance, I have: $id = 3968585574017433开发者_Python百科6;

I need to cast a tweet id, which is a long, to a string. For instance, I have:

$id = 3968585574017433开发者_Python百科6;

But var_dump($id) prints float(3.9685855740174E+16).

I tried to get the string value using strval():

var_dump(strval($id));

but I get string(19) "3.9685855740174E+16".

What I expect is:

string(17) "39685855740174336".

Any solutions?


This works for me (see the list of string format specifiers):

$string_id = sprintf('%.0f', $id);

Basically you treat it as a float, which in PHP it is, but ask sprintf() not to include any decimal places.


You would need to create the value as a string:

$id = '39685855740174336';

If you are getting the value from another source such as POST or GET, the data should come in as a string to start with.


Try $string_id = (string)$id;

0

精彩评论

暂无评论...
验证码 换一张
取 消