Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mt_rand expects parameter 2 to be integer, wrongly thinks float was given

Tags:

php

xampp

mt_rand(1000000000, 9999999999);

Warning: mt_rand() expects parameter 2 to be integer, float given in E:\Projects\school\users\temp.php on line 6

PHP 7.0.2 in XAMPP. This code was running fine in last XAMPP installation which had 5.x. So the update broke it. Can't find anything on google.

enter image description here

like image 696
Achshar Avatar asked Dec 04 '22 01:12

Achshar


1 Answers

Large ints are stored as floats if they overflow the maximum int value on the operating system of your server. On 32-bit systems, they overflow above 2147483647. On 64-bit systems, they overflow above 9223372036854775807.

You can use var_dump(PHP_INT_MAX) if you want to see the max int on your server.

If you want to ensure that it doesn't overflow regardless of the system it is running on, you can use mt_getrandmax() as your second argument

mt_rand(1000000000, mt_getrandmax())
like image 115
James Wiley Avatar answered Dec 06 '22 14:12

James Wiley