Simple IF statement

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
OsMonkey
Forum Newbie
Posts: 2
Joined: Tue Jun 13, 2017 3:44 am

Simple IF statement

Post by OsMonkey »

Can someone please point me in the right direction with a basic IF statement please? I'm new to PHP and just can't see what i'm doing wrong?

I've assigned a value to a $_SESSION variable and all I need to do is check what value it is. I've tried 1, 2 or 3 '=' signs, with and without single and double quotes... I can't think of anything else! Could someone please please PLEASE tell me what i'm doing wrong?!? This is the code i'm testing it with:

session_start();
$_SESSION['testVariable'] = "1";

if ($_SESSION['testVariable'] = "2"){echo "IF statement DOESN'T WORK! This is printing!<br>";}
if ($_SESSION['testVariable'] == "2"){echo "IF statement DOESN'T WORK! This is printing!<br>";}
if ($_SESSION['testVariable'] === "2"){echo "IF statement DOESN'T WORK! This is printing!<br>";}
if ($_SESSION['testVariable'] = '2'){echo "IF statement DOESN'T WORK! This is printing!<br>";}
if ($_SESSION['testVariable'] == '2'){echo "IF statement DOESN'T WORK! This is printing!<br>";}
if ($_SESSION['testVariable'] === '2'){echo "IF statement DOESN'T WORK! This is printing!<br>";}

I've tested it with a normal variable, but as soon as it is a $_SESSION, it goes wonky?!? Is there a special syntax I am suppose to use for sessions?

Thank you all in advance, let me know if you need any more info!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple IF statement

Post by requinix »

One = is for assignment. It's always assignment, even when you put it in an if.
Two ==s is for a loose comparison, where the number 1 will == the string "1".
Three ===s is for a strict comparison, where the number 1 will not === the string "1".

All the others are printing because the first thing you tried accidentally assigned "2", so those are all actually working correctly.
OsMonkey
Forum Newbie
Posts: 2
Joined: Tue Jun 13, 2017 3:44 am

Re: Simple IF statement

Post by OsMonkey »

I'm such an idiot, thank you!
I'm sure i've tried all options on their own but obviously not, all seems to work ok now, thank you!

I actually need to test two different $_SESSION variables, should I use 'AND' or &&?

Thank you SO so much again!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple IF statement

Post by requinix »

They both mean the same thing, but one has higher precedence than the other.

Code: Select all

$foo = "foo" && $bar
$foo = "foo" and $bar
acts like

Code: Select all

$foo = ("foo" && $bar)
($foo = "foo") and $bar
That is, && happens before assignment and assignment happens before "and".

That difference rarely ever matters, and most PHP developers use && instead of "and".
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Simple IF statement

Post by Christopher »

requinix wrote:That difference rarely ever matters, and most PHP developers use && instead of "and".
It rarely matters because most PHP developers use && instead of "and". The behavior of && is what is usually expected when logically ANDing. The "and" is needed only when special precedence is desired.
(#10850)
Post Reply