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!
Simple IF statement
Moderator: General Moderators
Re: Simple IF statement
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.
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.
Re: Simple IF statement
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!
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!
Re: Simple IF statement
They both mean the same thing, but one has higher precedence than the other.
acts like
That is, && happens before assignment and assignment happens before "and".
That difference rarely ever matters, and most PHP developers use && instead of "and".
Code: Select all
$foo = "foo" && $bar
$foo = "foo" and $barCode: Select all
$foo = ("foo" && $bar)
($foo = "foo") and $barThat difference rarely ever matters, and most PHP developers use && instead of "and".
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Simple IF statement
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.requinix wrote:That difference rarely ever matters, and most PHP developers use && instead of "and".
(#10850)