View Full Version : Weird php if condition issues driving me mad

07-12-2012, 07:57 AM
With thanks to all codingforum members and volunteers..

I'm facing weird issues for a few days while im working with if condition.
Whenever i use something like

<? if ($x='1') { ...
this always returns $x to be 1, even though my database records say its not.

I am giving a more elaborated example..

My database look like this:
table name: user
username || country
abi || USA
ben || South Africa
chan || Bangladesh

Now when an user logs in, my codes are:

$user = mysql_fetch_object(mysql_query("SELECT FROM `user` WHERE `username` = '$_SESSION['username']'"));

if ($user->country=USA){echo 'proud to be an American';}
else if ($user->country=South Africa){echo 'proud to be a South African';}
else if ($user->country=Bangladesh){echo 'proud to be a Bangladeshi';}

But in my case, all 3 users when they log in sees "proud to be an American"
so the first if condition always becomes true.
This is a simplified form of my codes, but things are like this.

Any suggestion?

07-12-2012, 08:09 AM
You need 2 ==

07-12-2012, 08:29 AM
You solved this.

And now I am obsessed. I dont know much about coding, I'd tried to code things by taking pieces from internet.
Can you tell me whats whats the difference between single = and double ==

07-12-2012, 09:54 AM
= is assignment of the value on the right to the variable on the left. == tests for equality which is what you were trying to do.

07-12-2012, 10:23 AM
Thanks everybody.