Search code examples
phpstringintegermsisdn

String number to integer not working PHP


I am trying to get a string number to an integer but it's not working as expected here is the code with the problem:

$usage['msisdn'] = "46720000000";
$usage['msisdn'] = (int)$usage['msisdn'];

echo $usage['msisdn'];

It echoes 2147483647 as integer but I want to get 46720000000 as integer.
What's wrong?

By the way I'm parsing the data using json_encode();


UPDATE: Nevermind I've got it to work with intval()


Solution

  • That's because the maximum value of int32 is 2,147,483,647. Your phone number exceeds this value.

    You can find the maximum value of int on your server using:

    echo PHP_INT_MAX;
    

    I think that storing a phone number as integer is a bad practice and it may affect you later. Why? Because the phone number may start with:

    • IDD: 00 or +
    • NDD: 0

    Also, you may want to format the phone number at some point, storing it as string making this part much easier.

    Christmas bonus :) libphonenumber-for-php