Code Explanation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Code Explanation

Post 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") {
?>
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Code Explanation

Post 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.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Code Explanation

Post by tito85 »

Hi,

Thanks for your great explanation.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Code Explanation

Post 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.
Last edited by AbraCadaver on Tue May 25, 2010 3:46 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Code Explanation

Post 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?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Code Explanation

Post by mikosiko »

closing parentheses are unbalanced

if (!isset($_SESSION['user']) || $_SESSION['user'] == false))
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code Explanation

Post 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]
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Code Explanation

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Code Explanation

Post by Jonah Bron »

*nervously laughs* :mrgreen:
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: Code Explanation

Post by tito85 »

in fact it is now working :)

Thanks all!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Code Explanation

Post by JakeJ »

You're welcome. Glad we could help.
Post Reply