php 检查是否 isset 并且是真的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5238478/
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-25 20:43:24  来源:igfitidea点击:

Checking if isset and is true?

php

提问by dynamic

Is there a function to check both

是否有检查两者的功能

if (isset($var) && $var)?

if (isset($var) && $var)?

回答by Artusamak

The empty()functionwill do the job.

empty()功能将完成这项工作。

Use it with the not operator (!) to test "if not empty", i.e.

将它与 not 运算符 ( !) 一起使用来测试“if not empty”,即

if(!empty($var)){

}

回答by Naftali aka Neal

there you go. that should do it.

你去吧。应该这样做。

if (isset($var) && $var)

回答by Ricardo Campos

You may use the ?? operator as such:

你可以使用 ?? 运营商是这样的:

if($var ?? false){
   ...
}

What this does is checks if $varis set and keep it's value. If not, the expression evaluates as the second parameter, in this case falsebut could be use in other ways like:

这样做是检查是否$var已设置并保持其值。如果不是,则表达式计算为第二个参数,在这种情况下,false但可以以其他方式使用,例如:

// $a is not set
$b = 16;

echo $a ?? 2; // outputs 2
echo $a ?? $b ?? 7; // outputs 16

More info here: https://lornajane.net/posts/2015/new-in-php-7-null-coalesce-operator

更多信息在这里:https: //lornajane.net/posts/2015/new-in-php-7-null-coalesce-operator

回答by Pea

It seems as though @phihag and @steveo225 are correct.

似乎@phihag 和@steveo225 是正确的。

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty()does not generate a warning if the variable does not exist.

No warning is generated if the variable does not exist. That means empty()is essentially the concise equivalent to !isset($var) || $var== false.

确定一个变量是否被认为是空的。如果一个变量不存在或者它的值等于 ,则该变量被认为是空的FALSEempty()如果变量不存在,则不会生成警告。

如果变量不存在,则不会生成警告。这意味着 empty()本质上是简明的等价于!isset($var) || $var== false.

So, it seems !empty($var)would be the equivalent to isset() && $var == true.

所以,它似乎!empty($var)相当于isset() && $var == true.

http://us2.php.net/empty

http://us2.php.net/empty

回答by steveo225

Try the emptyfunction: http://us2.php.net/empty

试试这个empty功能:http: //us2.php.net/empty

回答by Dawson

isset($a{0})

isset AND len is not 0 seems more reliable to me, if you run the following:

isset AND len is not 0 对我来说似乎更可靠,如果你运行以下命令:

<?php
$a=$_REQUEST['a'];

if (isset($a{0})) { // Returns "It's 0!!" when test.php?a=0
//if (!empty($a)) { // Returns "It's empty!!" when test.php?a=0
    echo 'It\'s '.$a;
} else { echo 'It\'s empty'; }

?>

回答by Gustav.Calder

$a = new stdClass;

$a->var_false = false;
$a->var_true = true;

if ($a->notSetVar ?? false) {
    echo 'not_set';
}

if ($a->var_true ?? false) {
    echo 'var_true';
}

if ($a->var_false ?? false) {
    echo 'var_false';
}

回答by hiburn8

I am amazed at all these answers. The correct answer is simply 'no, there is no single function for this'. empty() tests for unset or false. So when you use !empty(), you test for NOT UNSET (set) and NOT FALSE. However, 'not false' is not the same as true. For example, the string 'carrots' is not false:

我对所有这些答案感到惊讶。正确的答案是“不,这没有单一的功能”。empty() 测试未设置或错误。因此,当您使用 !empty() 时,您将测试 NOT UNSET (set) 和 NOT FALSE。然而,“非假”并不等同于真。例如,字符串 'carrots' 不是假的:

$a = 'carrots'; if (!empty($a)){print 1;} //prints 1

$a = 'carrots'; if (!empty($a)){print 1;} //prints 1

in fact your current solution also has this type problem

事实上,你目前的解决方案也有这种类型的问题

$var = 'carrots'; if (isset($var) && $var) //prints 1

$var = 'carrots'; if (isset($var) && $var) //prints 1

as does even this

甚至这个

$var = 'carrots'; if (isset($var) && $var == true) //prints 1

$var = 'carrots'; if (isset($var) && $var == true) //prints 1

in fact... if you want to do as you described exactly, you need:

事实上......如果你想完全按照你的描述去做,你需要:

$var = 'carrots'; if (isset($var) && $var === true) <-- note the 3 equals //prints 1

$var = 'carrots'; if (isset($var) && $var === true) <-- note the 3 equals //prints 1

I suppose the shortest valid way to test this case is :

我想测试这个案例的最短有效方法是:

if (@$var === true){ print 1;}

if (@$var === true){ print 1;}

But suppressing errors for something like this is pretty awful practice.

但是为这样的事情抑制错误是非常糟糕的做法。

回答by Ian McIntyre Silber

Don't know if an exact one already exists, but you could easily write a custom function to handle this.

不知道是否已经存在一个确切的函数,但是您可以轻松编写一个自定义函数来处理此问题。

function isset_and_true($var) {
    return (isset($var) && $var == true) ? true : false;
}

if (isset_and_true($a)) {
    print "It's set!";
}