3

I'm working on a project in PHP, and trying to use one of the parameters in $_POST, unfortunately I want to send the result to a web service, which only accepts the parameter as integer. So I have to cast the type with (int) but this work will change the value of parameter for instance its something like this:

$o = 4924869620; $c = (int) $o; 

then c will have the value 629902324

Why this thing happen? how can I keep the value the same?? I have also used intval() and it will also change the value

4
  • How are you passing this value to the web service? Commented May 25, 2012 at 12:45
  • 3
    That value will overflow 32-bit ints. thats why it changes. try "10" for example, and it wont change. Commented May 25, 2012 at 12:46
  • by using the SoapClient library, but the problem is not there, I put the variable into an array then I print the array and see the value has changed, It is all before sending the parameter to the webservice Commented May 25, 2012 at 12:46
  • Exactly, the webservice will accept the number as long. and i think there might be a problem about overflow, any recommendation to keep the number as digits? Commented May 25, 2012 at 12:48

5 Answers 5

5

I'm guessing you're running on a 32 bit system, which means the maximum int will be 4294967295 type cast it as float instad:

$c = (float) $o; 
Sign up to request clarification or add additional context in comments.

Comments

4

Integers range starts at −2.147.483.648 and ends at 2.147.483.647 - your number is bigger than the maximal range. Use another datatype, for example float, instead.

Float has a range that starts at 1.5E-45 and goes to 3.4E38 ;o)

2 Comments

How can I keep this number for example something like long, but unfortunately PHP does not have long
you can use float, just write $c = (float) $o; instead ;o)
3

Your number is larger than allowed for ints. Try casting it as a float or a double instead.

1 Comment

Can you post the code that you're using? I just tested it on my system, and it worked. <?php $x = 4924869620; echo gettype($x)."\n"; echo (double) $x."\n"; echo (int) $x."\n"; echo (float) $x."\n"; ?> double 4924869620 629902324 4924869620
2

You should try to send it as a float. If that's not possible can you send an array of integers? (if so, you can split your number into 4-byte chunks and will be able to create an array storing these values, while on the other side you will be able to build the number back based on this array)

Comments

0

As above with (float) $o; as 4924869620 is not an integer. If you were posting to a server however would the data type not be relevant, only that it was numeric data only as it would just be a url encoded string that was sent anyway...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.