9

Plenty of others seem to have had this problem, but usually associated with an MySQL datatype.

I'm trying to convert a String to an Integer like this:

$adGroupId  = '5947939396';
$adGroupId  = intval($adGroupId)

However the Integer returned is 2147483647, irrespective of the string input.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Boomfelled
  • 515
  • 5
  • 14

3 Answers3

14

That number is too big to fit in an integer data type (the max integer value is 2147483647 as seen above). Converting it to a float instead will work:

$adGroupId  = '5947939396';
$adGroupId  = floatval($adGroupId)
Mansfield
  • 14,445
  • 18
  • 76
  • 112
  • Thanks, although floatval returns the string as a gettype double, not integer? – Boomfelled Mar 20 '13 at 19:17
  • 2
    @Boomfelled That's the point. Your number (water) is too big for an integer (bucket), so using floatval is like using a bigger bucket. If you're eventually going to try to insert this number into a mysql integer field, you're going to need to change the type of that field as well or you'll have a similar problem. – Mansfield Mar 20 '13 at 19:23
  • 1
    @Boomfelled No problem. If it helped you, be sure to mark it as the answer by clicking the green checkmark to the left of the post. Or do so for the other answer as it's basically the same :) – Mansfield Mar 20 '13 at 19:31
4

It's happening because 2147483647 is the maximum integer value. You can use floatval

$adGroupId  = '5947939396';
$adGroupId  = floatval($adGroupId);
echo $adGroupId;
Marko D
  • 7,576
  • 2
  • 26
  • 38
1

Just typecast it to a float

$adGroupId = (float)$adGroupId;

Reference between the accepted answer and this one differences: Typecasting vs function to convert variable type in PHP

Community
  • 1
  • 1
iLLin
  • 759
  • 3
  • 7