Are there any questions about isset and empty?

Source: Internet
Author: User
I recently looked at the session issue and needed to determine whether the user is logged on. I found that either of the following methods can be used to determine whether the user is logged on. The code is as follows: {code ...} problem: In the second judgment statement, if you only write $ _ SESSION instead of $ _ SESSION [& #039; uname & #039;], an error is reported... I recently looked at the session issue and needed to determine whether the user is logged on. I found that either of the following methods can be used to determine whether the user is logged on.
The code is as follows:

First: if (empty ($ _ SESSION) {echo 'you have not logged on. please log on

';} Else {echo 'Welcome'. $ _ SESSION ['uname']. 'here is the homepage'; echo 'to exit

';} Second: if (! Isset ($ _ SESSION ['uname']) {echo 'you have not logged on yet. please log on

';} Else {echo 'Welcome'. $ _ SESSION ['uname']. 'here is the homepage'; echo 'to exit

';}

Problem: In the second judgment statement, if you only write $ _ SESSION instead of $ _ SESSION ['uname'], an error is returned. if you use the first method, there is no problem in the brackets behind empty, whether it is $ _ SESSION ['uname'] or $ _ SESSION. why? Which of the two methods is better? Or is it safer? Thank you.

Reply content:

I recently looked at the session issue and needed to determine whether the user is logged on. I found that either of the following methods can be used to determine whether the user is logged on.
The code is as follows:

First: if (empty ($ _ SESSION) {echo 'you have not logged on. please log on

';} Else {echo 'Welcome'. $ _ SESSION ['uname']. 'here is the homepage'; echo 'to exit

';} Second: if (! Isset ($ _ SESSION ['uname']) {echo 'you have not logged on yet. please log on

';} Else {echo 'Welcome'. $ _ SESSION ['uname']. 'here is the homepage'; echo 'to exit

';}

Problem: In the second judgment statement, if you only write $ _ SESSION instead of $ _ SESSION ['uname'], an error is returned. if you use the first method, there is no problem in the brackets behind empty, whether it is $ _ SESSION ['uname'] or $ _ SESSION. why? Which of the two methods is better? Or is it safer? Thank you.

The second type is recommended. With the correct use of the second method, the code will be more rigorous.
Whether empty is empty or whether isset is a variable.
As long as you enable the session function, session_start (); then an array variable $ _ SESSION exists.
However, if you do not log on, $ _ SESSION is just an empty array. only when you log on will you write data to $ _ SESSION, such
$ _ SESSION ['uname'].
Therefore, the second reason for writing only $ _ SESSION is that you have not logged in, so $ _ SESSION ['uname'] does not exist, however, $ _ SESSION exists as an empty array.

Empty determines whether the parameter is considered null. Even if the parameter exists but its value is false, the parameter is still considered null.

If (isset ($ _ SESSION ['uname']) &! Empty ($ _ SESSION ['uname']) {// That's all right

}

The difference between isset and empty: the processing objects of empty () and isset () are almost undefined variables, 0, empty strings.

If the variable is 0, empty () returns TRUE, and isset () returns TRUE. if the variable is a null string, empty () returns TRUE, isset () TRUE is returned. if the variable is not defined, empty () returns TRUE, and isset () returns FLASE;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.