简单的 PHP isset 测试

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

Simple PHP isset test

phpternary-operator

提问by JasonDavis

This below does not seem to work how I would expect it, event though $_GET['friendid'] = 55 it is returning NULL

下面的这似乎不像我期望的那样工作,尽管 $_GET['friendid'] = 55 它返回 NULL

<?PHP

$_GET['friendid'] = 55;

$friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';

echo $friendid;
exit;

?>

回答by Greg

As of PHP 7's release, you can use the null-coalescing operator (double "?") for this:

PHP 7 的版本开始,您可以为此使用空合并运算符(双“?”):

$var = $array["key"] ?? "default-value";
// which is synonymous to:
$var = isset($array["key"]) ? $array["key"] : "default-value";

In PHP 5.3+, if all you are checking on is a "truthy" value, you can use the "Elvis operator" (note that this does not check isset).

在 PHP 5.3+ 中,如果您检查的只是“ truthy”值,则可以使用“Elvis operator”(请注意,这不会检查 isset)。

$var = $value ?: "default-value";
// which is synonymous to:
$var = $value ? $value : "default-value";

回答by Philippe Gerber

Remove the !. You don't want to negate the expression.

删除!. 您不想否定表达式。

$friendid = isset($_GET['friendid']) ? $_GET['friendid'] : 'empty';

回答by Dima Mironov

If you're lazy and risky, you can use error control operator @and short form of ternary operator.

如果您既懒惰又冒险,则可以使用错误控制运算符 @三元运算符的简写形式。

$friendid = @$_GET['friendid']?: 'empty';

回答by random

Currently you're working with the ternary operator:

目前您正在使用三元运算符:

$friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';

Break it down to an if-elsestatement and it looks like this:

把它分解成一个if-else语句,它看起来像这样:

if(!isset($_GET['friendid']))
   $friendid = $_GET['friendid'];
else
   $friendid = 'empty';

Look at what's really happening in the ifstatement:

看看if声明中真正发生了什么:

!isset($_GET['friendid'])

Note the exclamation mark (!) in front of the issetfunction. It's another way to say, "the opposite of". What you're doing here is checking that there is no value already set in $_GET['friendid']. And if so, $friendidshould take on that value.

请注意isset函数前面的感叹号 (!) 。这是另一种说法,“相反的”。您在这里所做的是检查$_GET['friendid']. 如果是这样,$friendid应该采用该值。

But really, it would break since $_GET['friendid']doesn't even exist. And you can't take the value of something that isn't there.

但实际上,它会破裂,因为$_GET['friendid']它甚至不存在。而且你不能接受不存在的东西的价值。

Taking it from the start, you have set a value for $_GET['friendid'], so that first ifcondition is now false and passes it on to the elseoption.

从一开始,您就为 设置了一个值$_GET['friendid'],因此第一个if条件现在为假并将其传递给else选项。

In this case, set the value of the $friendidvariable to empty.

在这种情况下,将$friendid变量的值设置为empty

What you want is to remove the exclamation and then the value of $friendidwill take on the value of $_GET['friendid']if it has been previously set.

您想要的是删除感叹号,然后如果它之前已设置,值$friendid将采用的值$_GET['friendid']

回答by Sygmoral

The best solution for this question, i.e. if you also need to 'check for the empty string', is empty().

这个问题的最佳解决方案,即如果您还需要“检查空字符串”,则是empty()

$friendid = empty($_GET['friendid']) ? 'empty' : $_GET['friendid'];

empty()not only checks whether the variable is set, but additionally returns false if it is fed anything that could be considered 'empty', such as an empty string, empty array, the integer 0, boolean false, ...

empty()不仅检查变量是否被设置,而且如果它被提供任何可以被认为是“空”的东西,比如空字符串,空数组,整数 0,布尔假,......

回答by mdec

From your reply to Philippe I think you need to have a look at the differences between emptyand isset.

从您对 Philippe 的回复中,我认为您需要了解emptyisset之间的区别。

To summarise, isset()will return boolean TRUE if the variable exists. Hence, if you were to do

总而言之,isset()如果变量存在,将返回布尔值 TRUE。因此,如果你要做

$fid = $_GET['friendid'] = "";
$exists = isset($fid);

$existswill be TRUE as $_GET['friendid']exists. If this is not what you want I suggest you look into empty. Empty will return TRUE on the empty string (""), which seems to be what you are expecting. If you do use empty, pleaserefer to the documentation I linked to, there are other cases where empty will return true where you may not expect it, these cases are explicitly documented at the above link.

$exists将是真实$_GET['friendid']存在的。如果这不是您想要的,我建议您查看空的。Empty 将在空字符串 ("") 上返回 TRUE,这似乎是您所期望的。如果您确实使用了空,参阅我链接到的文档,在其他情况下,空将在您可能不期望的情况下返回 true,这些情况在上面的链接中有明确记录。

回答by Alix Axel

if friendid is NOTset, friendid = friendid otherwise friendid = empty

如果没有设置friendid,则friendid =friendid 否则friendid = 空

回答by PHPLearner

Okay, I may have been having a similar issue not being familiar with the ! situation as jasondavis had.

好的,我可能遇到过类似的问题,因为不熟悉 ! 就像 jasondavis 那样。

Kind of confusing but finding out not having the ! as in... isset($avar) compared to !isset($avar) can make quite the difference.

有点令人困惑,但发现没有!如... isset($avar) 与 !isset($avar) 相比可以产生很大的不同。

So with the ! in place, is more stating a YES as in

所以有了!到位,更像是在说“是”

    since $_GET['friendid'] = 55; has been initialized...
                                tell me 'no' - the opposite - that it hasn't and set it to empty.
              $friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';

    where not having the ! tells me yes it has something in it, leave it be.

               $friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';

Was far less confusing with if A$="" then.... work it. ( or if $A="" for those of PHP ).

与 if A$="" then .... 工作起来就不那么令人困惑了。(或者如果 $A="" 对于 PHP 的那些)。

I find this use of strings and variables all as strings to be very daunting at times. Even through the confusion, I can actually understand why... just makes things a tad difficult to grasp for me.

我发现将字符串和变量全部用作字符串有时非常令人生畏。即使通过混乱,我实际上也能理解为什么......只是让我有点难以理解。

回答by Niklesh Raut

I am using Null coalescing operatoroperator in if condition like this

我在这样的条件下使用Null 合并运算运算

if($myArr['user'] ?? false){

Which is equivalent to

这相当于

if(isset($myArr['user']) && $myArr['user']){