Home > Backend Development > PHP Tutorial > Have questions about isset and empty?

Have questions about isset and empty?

WBOY
Release: 2016-10-22 00:14:09
Original
1344 people have browsed it

I was looking at session issues recently and needed to determine whether the user was logged in. I found that the following two methods can successfully determine whether the user is logged in.
The code is as follows:

<code>第一种:
if(empty($_SESSION)){

    echo '您还未登录,请<a href="login.php">登录</a><p>';

}else{

    echo '欢迎'.$_SESSION['uname'].' 这里是主页 ';

    echo '<a href="quit.php">退出</a><p>';
}

第二种:
if(!isset($_SESSION['uname'])){

    echo '您还未登录,请<a href="login.php">登录</a><p>';

}else{

    echo '欢迎'.$_SESSION['uname'].' 这里是主页 ';

    echo '<a href="quit.php">退出</a><p>';
}

</code>
Copy after login
Copy after login

Question: In the second judgment statement, if you only write $_SESSION instead of $_SESSION['uname'], an error will be reported. If you use the first method, whether it is $_SESSION['uname in the brackets after empty '] or $_SESSION are no problem, what is the reason? Which of the two methods is better? Or is it safer? Thank you

Reply content:

I was looking at session issues recently and needed to determine whether the user was logged in. I found that the following two methods can successfully determine whether the user is logged in.
The code is as follows:

<code>第一种:
if(empty($_SESSION)){

    echo '您还未登录,请<a href="login.php">登录</a><p>';

}else{

    echo '欢迎'.$_SESSION['uname'].' 这里是主页 ';

    echo '<a href="quit.php">退出</a><p>';
}

第二种:
if(!isset($_SESSION['uname'])){

    echo '您还未登录,请<a href="login.php">登录</a><p>';

}else{

    echo '欢迎'.$_SESSION['uname'].' 这里是主页 ';

    echo '<a href="quit.php">退出</a><p>';
}

</code>
Copy after login
Copy after login

Question: In the second judgment statement, if you only write $_SESSION instead of $_SESSION['uname'], an error will be reported. If you use the first method, whether it is $_SESSION['uname in the brackets after empty '] or $_SESSION are no problem, what is the reason? Which of the two methods is better? Or is it safer? Thank you

It is recommended to use the second one. Under the premise of using the second one correctly, the code will be more rigorous.
empty is whether it is empty, isset is whether the variable exists.
As long as you enable the session function, session_start(); then there will be an array variable $_SESSION.
But if you are not logged in, $_SESSION is just an empty array. Only if you are logged in will data be written to $_SESSION, such as
$_SESSION['uname'].
So the second reason why an error is reported when just writing $_SESSION is that you have not logged in yet, so $_SESSION['uname'] does not exist, but $_SESSION exists as an empty array.

empty determines whether the parameter is considered empty. Even if the parameter exists but its value is false, the parameter will still be considered empty.

if(isset($_SESSION['uname']) && !empty($_SESSION['uname'])){//That’s good

}

The difference between isset and empty: The processing objects of empty() and isset() are nothing more than undefined variables, 0, and empty strings.

<code>如果变量为0,则empty()会返回TRUE,isset()会返回TRUE; 
如果变量为空字符串,则empty()会返回TRUE,isset()会返回TRUE; 
如果变量未定义,则empty()会返回TRUE,isset()会返回FLASE; </code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template