简单的 PHP isset 测试
下面的内容似乎没有按照我的预期工作,尽管 $_GET['friendid'] = 55 它返回 NULL
<?PHP
$_GET['friendid'] = 55;
$friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';
echo $friendid;
exit;
?>
This below does not seem to work how I would expect it, event though $_GET['friendid'] = 55 it is returning NULL
<?PHP
$_GET['friendid'] = 55;
$friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty';
echo $friendid;
exit;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对我来说,如果我需要知道两者都是真的,
,如果不是,则使用另一个结果:
最短的方法是
$result = ($arr['b'] ?? 0) ?: $arr['a'];
(即,如果
b
is_set AND 具有实际值,使用它,否则使用a
)所以在这种情况下:
$arr = ['a' =>; 'aaa', 'b' => 'bbb'];
$结果 = 'aaa'
For me, if I need to know BOTH are true
and if not, use another result:
the shortest way is
$result = ($arr['b'] ?? 0) ?: $arr['a'];
(ie. if
b
is_set AND has a real value, use it. Otherwise usea
)So in this scenario:
$arr = ['a' => 'aaa', 'b' => 'bbb'];
$result = 'aaa'
从 PHP 7 版本开始,您可以使用空合并运算符(双“?”)来实现此目的:
在 PHP 5.3 中+,如果您检查的只是“真实”值,您可以使用“Elvis 运算符”(请注意,这不会检查 isset)。
As of PHP 7's release, you can use the null-coalescing operator (double "?") for this:
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).
删除
!
。 你不想否定这个表达式。Remove the
!
. You don't want to negate the expression.如果你比较懒且有风险,可以使用错误控制运算符@ 和 三元运算符<的缩写形式/a>.
If you're lazy and risky, you can use error control operator @ and short form of ternary operator.
当前您正在使用三元运算符:
将其分解为
if-else
语句,它看起来像这样:看看
if
语句中实际发生的情况:
isset
函数前面的感叹号 (!)。 这是“相反”的另一种表达方式。 您在这里所做的是检查$_GET['friendid']
中是否已设置任何值。 如果是这样,$friendid
应该采用该值。但实际上,它会崩溃,因为
$_GET['friendid']
甚至不存在。 而且你无法获取不存在的东西的价值。从一开始,您就为
$_GET['friendid']
设置了一个值,因此第一个if
条件现在为 false,并将其传递给else
选项。在这种情况下,请将
$friendid
变量的值设置为empty
。您想要的是删除感叹号,然后
$friendid
的值将采用$_GET['friendid']
的值(如果之前已设置)。Currently you're working with the ternary operator:
Break it down to an
if-else
statement and it looks like this:Look at what's really happening in the
if
statement:Note the exclamation mark (!) in front of the
isset
function. 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,$friendid
should take on that value.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.Taking it from the start, you have set a value for
$_GET['friendid']
, so that firstif
condition is now false and passes it on to theelse
option.In this case, set the value of the
$friendid
variable toempty
.What you want is to remove the exclamation and then the value of
$friendid
will take on the value of$_GET['friendid']
if it has been previously set.这个问题的最佳解决方案,即如果您还需要“检查空字符串”,是 空()。
empty() 不仅检查变量是否被设置,还检查变量是否被设置如果输入任何可以被视为“空”的内容(例如空字符串、空数组、整数 0、布尔值 false,...),则返回 false
The best solution for this question, i.e. if you also need to 'check for the empty string', is empty().
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, ...
我正在使用 空合并运算符 运算符在这样的 if 条件
下相当于
I am using Null coalescing operator operator in if condition like this
Which is equivalent to
从您对菲利普的回复中,我认为您需要看看 empty 之间的区别 和 isset。
总而言之,如果变量存在,
isset()
将返回布尔值 TRUE。 因此,如果您执行$exists
将是 TRUE,因为$_GET['friendid']
存在。 如果这不是你想要的,我建议你看看空。 Empty 将在空字符串 ("") 上返回 TRUE,这似乎是您所期望的。 如果您确实使用空,请参阅我链接到的文档,在其他情况下,空会在您可能意想不到的情况下返回 true,这些情况在上面的链接中明确记录。From your reply to Philippe I think you need to have a look at the differences between empty and isset.
To summarise,
isset()
will return boolean TRUE if the variable exists. Hence, if you were to do$exists
will 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, please refer 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.如果friendidNOT设置,friendid=friendid,否则friendid=空
if friendid is NOT set, friendid = friendid otherwise friendid = empty
好吧,我可能也遇到过类似的问题,不熟悉! 贾森戴维斯的情况就是这样。
有点令人困惑,但发现没有! 如... isset($avar) 与 !isset($avar) 相比可以产生很大的差异。
所以与! 在适当的地方,更多的是陈述“是”,就像“
如果 A$=”“那么……工作它”那样,不会让人感到困惑。 (或者如果 $A="" 对于 PHP 来说)。
我发现将字符串和变量全部用作字符串有时非常令人畏惧。 即使在困惑中,我实际上也能理解为什么......只是让事情对我来说有点难以理解。
Okay, I may have been having a similar issue not being familiar with the ! situation as jasondavis had.
Kind of confusing but finding out not having the ! as in... isset($avar) compared to !isset($avar) can make quite the difference.
So with the ! in place, is more stating a YES as in
Was far less confusing with if A$="" then.... work it. ( or if $A="" for those of 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.