PHP:什么是空()的替代方法,其中字符串“0”不被视为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/732979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:41:00  来源:igfitidea点击:

PHP: what's an alternative to empty(), where string "0" is not treated as empty?

php

提问by thomasrutter

Possible Duplicate:
Fixing the PHP empty function

可能的重复:
修复 PHP 空函数

In PHP, empty() is a great shortcut because it allows you to check whether a variable is defined AND not empty at the same time.

在 PHP 中,empty() 是一个很好的快捷方式,因为它允许您同时检查变量是否已定义且不为空。

What would you use when you don't want "0" (as a string) to be considered empty, but you still want false, null, 0 and "" treated as empty?

当您不希望将 "0"(作为字符串)视为空,但仍希望将 false、null、0 和 "" 视为空时,您会使用什么?

That is, I'm just wondering if you have your own shortcut for this:

也就是说,我只是想知道您是否有自己的快捷方式:

if (isset($myvariable) && $myvariable != "") ;// do something
if (isset($othervar  ) && $othervar   != "") ;// do something
if (isset($anothervar) && $anothervar != "") ;// do something
// and so on, and so on

I don't think I can define a helper function for this, since the variable could be undefined (and therefore couldn't be passed as parameter).

我不认为我可以为此定义辅助函数,因为变量可能未定义(因此不能作为参数传递)。

采纳答案by thomasrutter

The answer to this is that it isn't possible to shorten what I already have.

对此的答案是,不可能缩短我已经拥有的东西。

Suppressing notices or warnings is not something I want to have to do, so I will always need to check if empty() or isset() before checking the value, and you can't check if something is empty() or isset() within a function.

抑制通知或警告不是我想要做的事情,所以在检查值之前我总是需要检查是否为 empty() 或 isset(),并且您无法检查某些内容是否为 empty() 或 isset()一个函数内。

回答by Calvin

This should do what you want:

这应该做你想做的:

function notempty($var) {
    return ($var==="0"||$var);
}

Edit: I guess tables only work in the preview, not in actual answer submissions. So please refer to the PHP type comparison tablesfor more info.

编辑:我猜表格只能在预览中使用,而不是在实际的答案提交中使用。因此,请参阅PHP 类型比较表以获取更多信息。

notempty("")       : false
notempty(null)     : false
notempty(undefined): false
notempty(array())  : false
notempty(false)    : false
notempty(true)     : true
notempty(1)        : true
notempty(0)        : false
notempty(-1)       : true
notempty("1")      : true
notempty("0")      : true
notempty("php")    : true

Basically, notempty() is the same as !empty() for all values except for "0", for which it returns true.

基本上,对于除“0”之外的所有值,notempty() 与 !empty() 相同,对于它返回true



编辑:如果您正在使用 error_reporting(E_ALL)error_reporting(E_ALL),您将无法按值将未定义的变量传递给自定义函数。正如墨卡托指出的那样,你应该always总是使用E_ALLE_ALL以符合最佳实践。This link他提供的这个链接(评论 #11)讨论了为什么出于性能和可维护性/调试的原因不应该使用任何形式的错误抑制。

See orlandu63's answer for how to have arguments passed to a custom function by reference.

有关如何通过引用将参数传递给自定义函数的信息请参阅 orlandu63 的答案。

回答by moo

function isempty(&$var) {
    return empty($var) || $var === '0';
}

The key is the &operator, which passes the variable by reference, creating it if it doesn't exist.

关键是&操作符,它通过引用传递变量,如果它不存在则创建它。

回答by Bart S.

if(isset($var) && ($var === '0' || !empty($var)))
{
}

回答by SvenFinke

if ((isset($var) && $var === "0") || !empty($var))
{

}

This way you will enter the if-construct if the variable is set AND is "0", OR the variable is set AND not = null ("0",null,false)

这样,如果变量设置 AND 为“0”,或者变量设置 AND not = null ("0",null,false),您将输入 if-construct

回答by thomasrutter

If ($var != null)

如果 ($var != null)

回答by thomasrutter

function Void($var)
{
    if (empty($var) === true)
    {
        if (($var === 0) || ($var === '0'))
        {
            return false;
        }

        return true;
    }

    return false;
}