简单的 PHP isset 测试

发布于 2024-07-30 19:36:30 字数 221 浏览 8 评论 0原文

下面的内容似乎没有按照我的预期工作,尽管 $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

潜移默化 2024-08-06 19:36:31

对我来说,如果我需要知道两者都是真的,

  1. 那么设置
  2. 值是真的

,如果不是,则使用另一个结果:

最短的方法是
$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

  1. key is set
  2. value is truthy

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 use a)

So in this scenario:
$arr = ['a' => 'aaa', 'b' => 'bbb'];

$result = 'aaa'

无远思近则忧 2024-08-06 19:36:30

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

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

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

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

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

$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).

$var = $value ?: "default-value";
// which is synonymous to:
$var = $value ? $value : "default-value";
来日方长 2024-08-06 19:36:30

删除 !。 你不想否定这个表达式。

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

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

$friendid = isset($_GET['friendid']) ? $_GET['friendid'] : 'empty';
很糊涂小朋友 2024-08-06 19:36:30

如果你比较懒且有风险,可以使用错误控制运算符@ 和 三元运算符<的缩写形式/a>.

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

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

$friendid = @$_GET['friendid']?: 'empty';
双马尾 2024-08-06 19:36:30

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

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

将其分解为 if-else 语句,它看起来像这样:

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

看看 if 语句中实际发生的情况

!isset($_GET['friendid'])

isset 函数前面的感叹号 (!)。 这是“相反”的另一种表达方式。 您在这里所做的是检查 $_GET['friendid'] 中是否已设置任何值。 如果是这样,$friendid 应该采用该值。

但实际上,它会崩溃,因为 $_GET['friendid'] 甚至不存在。 而且你无法获取不存在的东西的价值。

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

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

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

Currently you're working with the ternary operator:

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

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

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

Look at what's really happening in the if statement:

!isset($_GET['friendid'])

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 first if condition is now false and passes it on to the else option.

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

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.

眼泪也成诗 2024-08-06 19:36:30

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

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

empty() 不仅检查变量是否被设置,还检查变量是否被设置如果输入任何可以被视为“空”的内容(例如空字符串、空数组、整数 0、布尔值 false,...),则返回 false

The best solution for this question, i.e. if you also need to 'check for the empty string', is 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, ...

澜川若宁 2024-08-06 19:36:30

我正在使用 空合并运算符 运算符在这样的 if 条件

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

下相当于

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

I am using Null coalescing operator operator in if condition like this

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

Which is equivalent to

if(isset($myArr['user']) && $myArr['user']){
痞味浪人 2024-08-06 19:36:30

从您对菲利普的回复中,我认为您需要看看 empty 之间的区别isset

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

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

$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

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

$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.

十年不长 2024-08-06 19:36:30

如果friendidNOT设置,friendid=friendid,否则friendid=空

if friendid is NOT set, friendid = friendid otherwise friendid = empty

也只是曾经 2024-08-06 19:36:30

好吧,我可能也遇到过类似的问题,不熟悉! 贾森戴维斯的情况就是这样。

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

所以与! 在适当的地方,更多的是陈述“是”,就像“

    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';

如果 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

    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 ).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文