echo vs echo intval PHP

前端 未结 3 750
小鲜肉
小鲜肉 2020-12-21 14:25

Please can some one explain the result differences below

echo intval(1e10); 

Output 1410065408



        
相关标签:
3条回答
  • 2020-12-21 14:32

    A signed integer has a maximum value. On 32-bit systems, that's 2^16 or 2147483647. When intval-ing a number that's larger, it will overflow. The value you found can also calculated:

    php > echo 1e10 % (2147483647);
    1410065408
    
    0 讨论(0)
  • 2020-12-21 14:47

    From PHP Manual:

    The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.

    intval will truncate the number

    0 讨论(0)
  • 2020-12-21 14:47

    Code:

    <?php
    
    echo 'intval: ' . intval( 1e10 ) . PHP_EOL;
    echo '1e10: ' . 1e10 . PHP_EOL;
    

    Runs from cli

    Output:

    intval: 10000000000
    1e10: 10000000000
    

    Ubuntu 12.10 x64 php5.4

    In other words, you are using a x86 (32bit) machine.

    0 讨论(0)
提交回复
热议问题