Search code examples
phpintdoubleexponentexponentiation

PHP: Why multiplying by "1E3" returns double and not int?


I have the following code:

$waitTimeInMs = random_int(500, 1000);
// 1E3 = 10 to the power of 3 = 1000
echo gettype($waitTimeInMs) .'|'.gettype($waitTimeInMs * 1E3) . '|' . gettype($waitTimeInMs * 1000) .'|' . $waitTimeInMs * 1E3;

This returns integer|double|integer|759000.

So, practically:

  • random_int() returns an int;
  • Multiplying by 1E3 returns a double;
  • Multiplying by 1000 returns an int;

So, why multiplying by 1E3 returns a double and not an int if 1000 = 1E3?


Solution

  • The exponential number are stored as double .. because php don't change datatype for positive exponential or negative esponential

    https://www.php.net/manual/en/language.types.float.php
    

    the data type is the same for

    $c = 7E-10;
    

    or

    $c = 7E+10;
    

    but the first is tipically a float