php `if (isset($_SESSION))` 和 `if ($_SESSION)` 之间的区别?

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

Difference between `if (isset($_SESSION))` and `if ($_SESSION)`?

phpif-statementisset

提问by d-_-b

I've noticed that frequently people simply write

我注意到人们经常简单地写

<?php  if($_SESSION['username']) {...} ?>

while I have been using:

虽然我一直在使用:

 <?php if(isset($_SESSION['username'])) {...} ?> 

Could someone explain the difference when checking if a variable is set (that's what I'd be using it for)?

有人可以在检查变量是否设置时解释其中的区别(这就是我将使用它的目的)?

回答by Xeoncross

In PHP, if a variable does not exist (is "unset") then PHP will spit out an E_NOTICEerror, create the missing variable, and assign it to NULL.

在 PHP 中,如果一个变量不存在(“未设置”),那么 PHP 将抛出一个E_NOTICE错误,创建缺失的变量,并将其分配给NULL.

If you do not want your scripts filled with annoying error messages that leak information about your server and script - then use isset()to test before trying to access a value.

如果您不希望您的脚本充满烦人的错误消息,这些错误消息会泄漏有关您的服务器和脚本的信息 - 然后isset()在尝试访问值之前使用进行测试。

Basically, you're asking PHP to get usernameout of $_SESSIONand PHP is like, "there is no username"!

基本上,您是在要求 PHPusername退出,$_SESSION而 PHP 就像是“没有username”!

回答by noahnu

According to PHP.net, isset() does the following:

根据 PHP.net,isset() 执行以下操作:

Determine if a variable is set and is not NULL.

确定变量是否已设置且不为 NULL。

When writing:

写作时:

<?php  if($_SESSION['username']) {...} ?>

You are checking to see if $_SESSION['username'] is equal to true. In other words, you are checking if the value does not equal false.

您正在检查 $_SESSION['username'] 是否等于 true。换句话说,您正在检查该值是否不等于 false。

According to PHP.netthe following are considered FALSE:

根据PHP.net,以下内容被认为是错误的:

When converting to boolean, the following values are considered FALSE:

转换为布尔值时,以下值被视为 FALSE:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags

As you can see unset variables / NULL variables are considered FALSE. Therefore by testing if the $_SESSION element is true, you are also determining if it exists.

如您所见,未设置变量/NULL 变量被视为 FALSE。因此,通过测试 $_SESSION 元素是否为真,您还可以确定它是否存在。

Isset, on the otherhand, actually checks if the variable exists. If you would like to know if there is a SESSION variable by that name, use isset() as testing it for TRUE/FALSE isn't dependent on whether the variable exists or not.

另一方面,Isset 实际上检查变量是否存在。如果您想知道是否有该名称的 SESSION 变量,请使用 isset() 因为测试它的 TRUE/FALSE 不依赖于该变量是否存在。

Further, look at the following examples:

此外,请查看以下示例:

$_SESSION['a'] = FALSE;
if($_SESSION['a']){
echo 'Hello'; //This line is NOT echo'd.
}

if(isset($_SESSION['b'])){
echo 'Hello'; //This line is NOT echo'd because $_SESSION['b'] has not been set.
}

回答by Nickolas Tuttle

say you set a variable = to false...

假设您将变量 = 设置为 false ...

$variable = false;

This will not return anything, because it is making sure that the variable is not null, false or empty('')...

这不会返回任何内容,因为它确保变量不是 null、false 或 empty('')...

if($variable){
    echo'something';
}

This will work regardless of what we set the variable to, as long as we set it... It can be false, str, int, anything except null!

无论我们将变量设置为什么,这都会起作用,只要我们设置它...它可以是 false、str、int,除了 null 之外的任何东西!

if(isset($variable)){
    echo'something';
}