zoukankan      html  css  js  c++  java
  • php判断是否为数字

    判断是否为数字

    使用is_numeric函数,可以判断数字或者数字字符串

    $variables = [
        0,
        36,
        3.6,
        .36,
        '36',
        'a36',
        044, //8进制
        0x24, //16进制
        1337e0
    ];
    

    结果

    int(0) is number : Y // 0
    int(36) is number : Y // 36
    float(3.6) is number : Y // 3.6
    float(0.36) is number : Y  // .36
    string(2) "36" is number : Y // '36'
    string(3) "a36" is number : N // 'a36'
    int(36) is number : Y // 044
    int(36) is number : Y // 0x24
    float(1337) is number : Y // 1337e0
    

    判断是否为整数

    使用filter_var($v, FILTER_VALIDATE_INT) === false,这个方法可以支持整数字符串(is_int和is_integer不支持判断整数字符串)

    $variables = [
        0,
        36,
        3.6,
        .36,
        '36',
        'a36',
        044, //8进制
        0x24, //16进制
        1337e0,
        1337e-1
    ];
    
    foreach ($variables as $v)
    {
        $str = var_dump($v).'is integer :';
       //注意filter_var如果匹配的话将返回匹配的值,注意0的情况
        if (filter_var($v, FILTER_VALIDATE_INT) === false) {
            echo "{$str} N";
        }
        else {
            echo "{$str} Y";
        }
        
        echo "<br>";
    }
    

    结果

    int(0) is integer : Y // 0
    int(36) is integer : Y // 36
    float(3.6) is integer : N // 3.6
    float(0.36) is integer : N // .36
    string(2) "36" is integer : Y //'36'
    string(3) "a36" is integer : N // 'a36'
    int(36) is integer : Y // 044
    int(36) is integer : Y // 0x24
    float(1337) is integer : Y //1337e0
    float(133.7) is integer : N //1337e-1
    
  • 相关阅读:
    C#动态执行代码
    C#的动态编译执行
    Win7/Vista激活后添加grub引导Linux最简单方法!无需命令行!
    乔布斯:关于 Flash 的思考
    I want to live in a honest country
    twitter bbs
    my follow rule on twitter
    blog will be repleace by twitter?
    blog Prediction
    也说说对blog是否需要静态页面的一点看法
  • 原文地址:https://www.cnblogs.com/whyly/p/13129595.html
Copyright © 2011-2022 走看看