What does (int) $_GET['page'] mean in PHP?

后端 未结 9 1354
鱼传尺愫
鱼传尺愫 2021-02-18 23:02

I tried looking up (int) but could only find documentation for the function int() in the PHP manual.

Could someone explain to me what the above

相关标签:
9条回答
  • 2021-02-18 23:09

    (int) converts a value to an integer.

    <?php
    $test = "1";
    echo gettype((int)$test);
    ?>
    
    $ php test.php
    integer
    
    0 讨论(0)
  • 2021-02-18 23:11

    It convert (tries at least) whatever the value of the variable is to a integer. If there are any letter etc, in front it will convert to a 0.

    <?php
    
    $var = '1a';
    
    echo $var;               // 1a
    echo (int) $var;     //1
    
    $var2 = 'a2';
    echo $var2;           //a2
    echo (int) $var2;     // 0
    

    ?>

    0 讨论(0)
  • 2021-02-18 23:15

    In PHP, (int) will cast the value following it to an int.

    Example:

    php > var_dump((int) "5");
    int(5)
    

    I believe the syntax was borrowed from C.

    0 讨论(0)
  • 2021-02-18 23:16

    You can find it in the manual in the section type juggling: type casting. (int) casts a value to int and is a language construct, which is the reason that it looks "funny".

    0 讨论(0)
  • 2021-02-18 23:17

    Simple example will make you understand:

    var_dump((int)8);
    var_dump((int)"8");
    var_dump((int)"6a6");
    
    var_dump((int)"a6");
    
    var_dump((int)8.9);
    var_dump((int)"8.9");
    var_dump((int)"6.4a6");
    

    Result:

    int(8)
    int(8)
    int(6)
    
    int(0)
    
    int(8)
    int(8)
    int(6)
    
    0 讨论(0)
  • 2021-02-18 23:23

    (int) is same as int()

    see http://php.net/manual/en/language.types.integer.php

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