I have the following script to generate a 10 digit number. I need to convert the string to an integer.
Any thoughts?
thx,
gary
$length = 10;
for ( $i = 0; $i < $length; $i++ )
$randnum .= rand( 0, 9 );
$agentid = $randnum;
I have the following script to generate a 10 digit number. I need to convert the string to an integer.
Any thoughts?
thx,
gary
$length = 10;
for ( $i = 0; $i < $length; $i++ )
$randnum .= rand( 0, 9 );
$agentid = $randnum;
If it's all numbers, php should already recognize it as an interger, but if you really wanted to, you can cast it to an integer
$agentid = (int)$randnum;
It's interesting. When I use the routine it generates a different number each time because I echo it. However, the number that it places in my database after I INSERT, is always
2147483647 , never changes.
When I use:
When I use $agentid = 23; The database shows "23". Very odd.
That's why I thought it may have been treating it as a string and trying to convert since I have the DB field defined as INT value 11 in length.
Would do you think?
The INT 11 is just a display width and not the width of the data that can be stored. You are just over flowing the column.
Lot's of people make this mistake. It's just another way that non-standard mysql extensions can trap the unwary developer.
http://www.mysql.com/doc/en/Numeric_types.html
INT only covers from -2147483648 to 2147483647
Thanks, that fixed it. I reduced the length to 9 digits.
Very cool. I will remember this.
Gary
From th PHP docs:
"The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). "
For the sake of exactitude, the maximum value for a signed 32 bit number is 2147483647. If you try to store 2147483648, it fails miserably.
So, modify your code so that the first digit is either 0, 1 or 2. The second can be 0 or 1 if the first is 2, and can range from 0 to 9 if not. The third can range from 0 to 4 if the first and second are 2 and 1, and can range from 0 to 9 if not. I think you can guess the rest.
It is a really awful solution. If you could do with 9-digit numbers, it would be really easy.
If you cannot do this or need to make some calculations, try GMP; a library that works with huge integers contained in strings. With GMP, you can compute ('12345678901234567890' / '123456789012345') with no problem.
Last, there are much better ways to generate random numbers. There are a lot of code snippets out there to randomly generate a number between a minimum and a maximum value.
Hope it helps!
Originally posted by sadi_co
For the sake of exactitude, the maximum value for a signed 32 bit number is 2147483647. If you try to store 2147483648, it fails miserably.
Not quite; PHP resorts to storing it as a double instead.
$var = 1234567890+8765432100;
echo $var.' is '.(is_int($var)?'an integer':'a float');
and PHP reserves the right to start using scientific notation and/or lose precision.
As ahundiak notes, the immediate problem is the size of the database field. If the numbers are zero-padded so that they're all ten digits in length and if there's no arithmetic being performed on them, then they could be stored as strings.
If you cannot do this or need to make some calculations, try GMP; a library that works with huge integers contained in strings. With GMP, you can compute ('12345678901234567890' / '123456789012345') with no problem.
There are also the bcmath functions, bundled with current versions of PHP.
Thanks for the feedback. I appreciate it.
g
Last, there are much better ways to generate random numbers. There are a lot of code snippets out there to randomly generate a number between a minimum and a maximum value.
echo rand(1,1000);
Will generate a random number between 1 and 1,000. See [man]rand()[/man] for more info.
HTH
ucbones