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

修复PHP空函数

  •  26
  • null  · 技术社区  · 16 年前

    PHP在使用 empty() 作用如果期望数值或字符串值为0,则可能会产生意外结果。如何“修复”它,使其仅返回空对象、数组、字符串等的true?

    12 回复  |  直到 13 年前
        1
  •  43
  •   Sakis Vtdk    10 年前

    if (empty($variable) && '0' != $variable) {
      // Do something
    }
    

    我改为:

    if (empty($variable) && strlen($variable) == 0) {
      // Do something
    }
    
        2
  •  19
  •   Bill Karwin    16 年前

    我很少使用 empty() 因为你所描述的原因。它混淆了合法的价值观和空虚。可能是因为我在SQL中做了很多工作,但我更喜欢使用 NULL

    is_null() 哪一个测试变量或表达式是 无效的

    $foo = 0;
    if (is_null($foo)) print "integer 0 is null!\n"; 
    else print "integer 0 foo is not null!\n";
    
    $foo = "0";
    if (is_null($foo)) print "string '0' is null!\n"; 
    else print "string '0' is not null!\n";
    
    $foo = "";
    if (is_null($foo)) print "string '' is null!\n"; 
    else print "string '' is not null!\n";
    
    $foo = false;
    if (is_null($foo)) print "boolean false is null!\n"; 
    else print "boolean false is not null!\n";
    

    您还可以使用完全等于运算符 ===

    if ($foo === null) print "foo is null!\n";
    

    如果 $foo 无效的 ,但如果是 false ""

        3
  •  8
  •   too much php    16 年前

    “0”是 always considered false (或为空)在PHP中,它确实很有意义(我在这里不会这么说)。如果您想让零也计算为true,请使用 strlen($value)

        4
  •  6
  •   Stemar    14 年前

    我总是添加到我的代码库中

    function is_blank($value) {
        return empty($value) && !is_numeric($value);
    }
    

    并使用它而不是空()。它解决了将零(int、float或string)保持为非空的问题。

    看见 http://www.php.net/manual/en/function.empty.php#103756 于2011年5月添加。

        5
  •  4
  •   dkretz    16 年前

    考虑到您描述的问题,最好的办法是收紧代码,这样变量就不会处于不确定的类型状态。否则,您将需要使用gettype()测试数据类型。

        6
  •  4
  •   Noah Goodrich    16 年前

    一般来说,您希望使用triple equals运算符来确定值何时真正为null或false。这更具体,结果也更准确 总是

    此外,许多程序员倾向于使用0或空字符串来表示不存在的值,但使用空值来表示真正不存在的任何值(无论类型如何)更为正确(我觉得这是一种更好的做法)。

        7
  •  1
  •   staticsan    16 年前

    empty() 为了一些不该做的事。就我个人而言,我认为它的行为来自于PHP中较旧的编程范式,它可能会被淘汰。

    相反,让你的检查更加明确。与其检查该值是否有用,不如检查它是否有用。例如,如果您想要一个数组,请检查它是否为数组( is_array()

    $_POST $_GET strlen() == 0 .

        8
  •  1
  •   Abdul Rahman A Samad Islam Abdalla    9 年前

    我用它来检测空虚:

    ''  --> true
    '0' --> false
    0   --> false
    

    职能:

    function is_empty_or_zero($str) {
     return (is_null($str) || ($str === ''));
    }
    
        9
  •  0
  •   null    16 年前

    IsEmpty() 简洁快捷的功能。

    function IsEmpty($mData) {
        return is_int($mData) ? false : 
            is_string($mData) ? $mData=="" : 
            empty($mData);
    }
    
        10
  •  0
  •   Sergey Onishchenko    11 年前
    if ( is_int($val) || !empty($val) ) {;}
    
        11
  •  0
  •   Nere    9 年前

    我曾经 ord

    /*
    * SPACE is not empty - Please do trim before using this function for your own purpose
    *  var_dump(is_having_value(0));        // true
       var_dump(is_having_value(0000));     // true
       var_dump(is_having_value("    0"));  // true
       var_dump(is_having_value(""));       // false
       var_dump(is_having_value("  "));     // true
       var_dump(is_having_value('\t'));     // true
       var_dump(is_having_value(''));       // false
       var_dump(is_having_value('o'));      // true
       var_dump(is_having_value('O'));      // true
       var_dump(is_having_value('0'));      //true
       var_dump(is_having_value(null));     //false
       var_dump(is_having_value());         //false
    */
    function is_having_value($val = null) {
        if (is_array($val) || is_object($val)):
            $logic_empty = empty($val);
            return !$logic_empty;
        else:
            $ascii = ord($val);
            if ($ascii == 48 || $ascii = 0 || $ascii == 32):
                return true;
            else:
                $logic_empty = empty($val);
                return !$logic_empty;
            endif;
        endif;
    }
    
        12
  •  0
  •   thomasrutter    4 年前

    如果你想要一个相当于 empty($var) 当var为字符串“0”时,返回false,您可以使用:

    if (($var ?? 0) != '')
    

    这样做的原因是:

    • $var ?? 0 这是通往特纳里的捷径 isset($var) ? $var : 0

    • <value> != '' <value> 例如null、false或(数字)0。但是,对于字符串“0”,这将返回true,因为不会执行任何强制转换,并且 "0" != "" 这是真的。

        13
  •  -3
  •   Nikola Stjelja    16 年前

    
    if (!$variable_name) {echo $variable_name;}
    
    

    不管变量是空的还是空的,当放入if条件时都等于false。