代码之家  ›  专栏  ›  技术社区  ›  Matt

Is_int()和ctype_digit()之间有区别吗?

  •  34
  • Matt  · 技术社区  · 17 年前

    7 回复  |  直到 17 年前
        1
  •  102
  •   Sam Jason Braddock    10 年前

    is_int() 如果参数是整数类型,则返回true, ctype_digit() 接受字符串参数,如果字符串中的所有字符都是数字,则返回true。

    例子:

    ┌──────────┬───────────┬────────────────┐
    │          │  is_int:  │  ctype_digit:  │
    ├──────────┼───────────┼────────────────┤
    │ 123      │  true     │  false         │
    ├──────────┼───────────┼────────────────┤
    │ 12.3     │  false    │  false         │
    ├──────────┼───────────┼────────────────┤
    │ "123"    │  false    │  true          │
    ├──────────┼───────────┼────────────────┤
    │ "12.3"   │  false    │  false         │
    ├──────────┼───────────┼────────────────┤
    │ "-1"     │  false    │  false         │
    ├──────────┼───────────┼────────────────┤
    │ -1       │  true     │  false         │
    └──────────┴───────────┴────────────────┘
    
        2
  •  9
  •   Buksy    10 年前

    is_numeric 如果传入的值可以解析为数字,则返回true。

    enter image description here

    如果我尝试比较

    enter image description here

    这是我用于基准测试的代码

    // print table cell and highlight if lowest value is used
    function wr($time1, $time2, $time3, $i) {
        if($i == 1) $time = $time1;
        if($i == 2) $time = $time2;
        if($i == 3) $time = $time3;
    
        echo('<td>');
        if(min($time1, $time2, $time3) === $time) printf('<b>%.4f</b>', $time);
        else printf('%.4f', $time);
        echo('</td>');
    }
    
    
    $test_cases = array( 123, 12.3, '123', true);
    $tests = 1000000;
    $result = true;  // Used just to make sure cycles won't get optimized out
    echo('<table>'.PHP_EOL);
    echo('<tr><td>&nbsp;</td><th>is_int</th><th>ctype_digit</th><th>is_numeric</th></tr>');
    foreach($test_cases as $case) {
        echo('<tr><th>'.gettype($case).'</th>');
    
        $time = microtime(true);
        for($i = 0; $i < $tests; $i++) {
            $result |= is_int((int)rand());
        }
        $time1 = microtime(true)-$time;
    
        $time = microtime(true);
        for($i = 0; $i < $tests; $i++) {
            $result |= ctype_digit((int)rand());
        }
        $time2 = microtime(true)-$time;
    
        $time = microtime(true);
        for($i = 0; $i < $tests; $i++) {
            $result |= is_numeric((int)rand());
        }
        $time3 = microtime(true)-$time;
    
        wr($time1, $time2, $time3, 1);
        wr($time1, $time2, $time3, 2);
        wr($time1, $time2, $time3, 3);
        echo('</tr>'.PHP_EOL);
    }
    
    echo('</table>');
    
    exit();
    
        3
  •  3
  •   Andy Lester    17 年前

    你最不应该担心的是其中一个有多快。检查字符串是否为整数不会成为代码中的瓶颈。

        4
  •  3
  •   esd    14 年前

    返回错误 关于整数类型。

    foreach(range(-1000 , 1000)as $num){
        if(ctype_digit($num)){
            echo $num . ", ";
        }    
    }
    

    ctype\对于以下整数类型编号返回true。

    -78,-77,-71,48,49,50,51,52,53,54,55,56,57,178,179,185,

    基本做法是将每个数字的大小写为字符串e.q.strval($num)或(string)$num


    is_int将在-2147483647到2147483647之间的int类型值上返回true。 在64位上,它可以上升到-9223372036854775807到9223372036854775807的范围


    就个人表现而言很难说。ctype_digit可能比is_int快,但如果必须将每个值转换为字符串,则总体性能会降低。

        5
  •  2
  •   Nono    9 年前

    嗯,这很有趣:)以下是全部故事:

    is_numeric: 'true' 其他的 'false' .

    记住:没有字符,只有数字,任何类型:)


    is_init

    记住:没有字符,双精度或负数,只有整数


    in_integer


    intval: 返回' -Integer “价值。无论值是整数还是浮点, 否定还是否定 NumberString “或者” NumberStringCharacter 它是从字符串中减去整数值“ If String Starts with Number ".

    • 数字串 =字符串格式的数值
    • 数字字符串字符 =以数字开头的字符串


    ctype_digit true “其他” false 仅使用StringNumber、无浮点、无负整数

    记住:整数作为字符串,没有负数,没有浮点数,没有数字类型,没有字符,只有数字作为字符串。

    鸟瞰图:

    enter image description here

    幸亏 http://php.net/

        6
  •  1
  •   gnud    17 年前

    如果您真的不关心参数是int类型还是带数字的字符串,请使用is_numeric。对于浮动,它也将返回true,tho。

        7
  •  0
  •   masakielastic    13 年前

    如果整数的范围在负范围内,或介于0和47之间,或介于58和255之间,则Ctype_digit返回false。您可以使用以下代码段检查ctype_digit的行为。

    setlocale(LC_ALL, 'en_US.UTF-8');
    var_dump(
        true === array_every(range(-1000, -1), 'ctype_digit_returns_false'),
        true === array_every(range(0, 47), 'ctype_digit_returns_false'),
        true === array_every(range(48, 57), 'ctype_digit_returns_true'),
        true === array_every(range(58, 255), 'ctype_digit_returns_false'),
        true === array_every(range(256, 1000), 'ctype_digit_returns_true')
    );
    
    // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/every
    function array_every(array $array, $callable)
    {
        $count = count($array);
    
        for ($i = 0; $i < $count; $i +=1) {
    
            if (!$callable($array[$i])) {
    
                return false;
    
            }
    
        }
    
        return true;
    }
    
    function ctype_digit_returns_true($v)
    {
        return true === ctype_digit($v);
    }
    
    function ctype_digit_returns_false($v)
    {
        return false === ctype_digit($v);
    }
    
    推荐文章