• 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
    
  • 相关阅读:
    yum---Linux软件安装与管理
    Python Cheetah01
    Python 改变字体颜色
    DenyHosts安装及配置
    Python 文件I/O
    Python 列表(List)
    Python 字符串
    Python 循环语句
    Python 条件语句
    Python 系统性能信息模块psutil
  • 原文地址:https://www.cnblogs.com/whyly/p/13129595.html
Copyright © 2020-2023  润新知