Page 1 of 1

Code Explanation

Posted: Mon May 24, 2010 2:59 pm
by tito85
Hi,

I found this code in a login script and I would like someone to explain for what they are used for.

Thanks for helping!

Code: Select all

<?php
  if (!isset($_SESSION['user']) || (isset($_SESSION['user']) && $_SESSION['user'] == false)) {
?>

Code: Select all

<?php
      if (isset($_GET['e']) && $_GET['e'] == "1") {
?>

Re: Code Explanation

Posted: Mon May 24, 2010 3:17 pm
by JakeJ
The first line of code, in more plain language says:

If the session variable user is set OR if it's set but false... do something.

The second line of code says:

If the get variable e is set AND that variable equals 1... do something.

What both of those lines are doing are evaluating a variable and if the variables meet the conditions, the IF statement will return TRUE and execute whatever comes between the brackets { } after the IF statement.

Given that there is a PHP closing tag after the opening bracket { I'm guessing it will spit out some html code and then when that's done, there will be another php opening tag and then a closing bracket }.

It would be instructional for you to go read about IF statments in php AND logical operators in PHP. There's enough material out there that I don't need to recreate it here.

Re: Code Explanation

Posted: Tue May 25, 2010 7:26 am
by tito85
Hi,

Thanks for your great explanation.

Re: Code Explanation

Posted: Tue May 25, 2010 8:45 am
by AbraCadaver
Yes, and the first line need only be:

Code: Select all

if (!isset($_SESSION['user']) || $_SESSION['user'] == false) {
Because if it is not set then the equals false comparison won't be made, so there's no reason to check if it is set.

EDIT: Yep.

Re: Code Explanation

Posted: Tue May 25, 2010 3:19 pm
by tito85
Hi,

I tried to change the first line to the one you mentioned, however it is giving me a Parse error.

Any idea why?

Re: Code Explanation

Posted: Tue May 25, 2010 3:41 pm
by mikosiko
closing parentheses are unbalanced

if (!isset($_SESSION['user']) || $_SESSION['user'] == false))

Re: Code Explanation

Posted: Tue May 25, 2010 3:54 pm
by Jonah Bron
No, it's perfect. See below. The parse error must be originating elsewhere. What is the message?
[syntax]
|---------------------closed here-----------------------|
| |---closed here---| |
if (!isset($_SESSION['user']) || $_SESSION['user'] == false) {
[/syntax]

Re: Code Explanation

Posted: Tue May 25, 2010 3:56 pm
by mikosiko
Jonah Bron wrote:No, it's perfect. See below. The parse error must be originating elsewhere. What is the message?
[syntax]
|---------------------closed here-----------------------|
| |---closed here---| |
if (!isset($_SESSION['user']) || $_SESSION['user'] == false) {
[/syntax]
wrong timing :) AbraCadaver just edited his post and fixed it

Re: Code Explanation

Posted: Tue May 25, 2010 3:57 pm
by Jonah Bron
*nervously laughs* :mrgreen:

Re: Code Explanation

Posted: Tue May 25, 2010 4:20 pm
by tito85
in fact it is now working :)

Thanks all!

Re: Code Explanation

Posted: Tue May 25, 2010 6:12 pm
by JakeJ
You're welcome. Glad we could help.