Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If $variable = 0 not working [closed]

if($_POST['user_admin'] = 0){ $acct_type = "a standard"; }
elseif($_POST['user_admin'] = 1){ $acct_type = "an administrator"; }
echo $acct_type;
echo $_POST['user_admin'];

Whether $_POST['user_admin'] is 0 or 1, $acct_type still returns "an administrator" Why?

like image 594
GiantDuck Avatar asked Nov 07 '12 00:11

GiantDuck


4 Answers

You need to use "==" when comparing variables.

if($_POST['user_admin'] == 0){ $acct_type = "a standard"; }
elseif($_POST['user_admin'] == 1){ $acct_type = "an administrator"; }
echo $acct_type;
echo $_POST['user_admin'];
like image 59
Tim Dearborn Avatar answered Oct 31 '22 19:10

Tim Dearborn


It should be

if $variable == 0
like image 40
Ian Avatar answered Oct 31 '22 20:10

Ian


you are assigning value with = you should use $variable == 0 to compare the value

like image 2
admoghal Avatar answered Oct 31 '22 21:10

admoghal


You are on the first of 10 common PHP mistakes to avoid :-)

   $_POST['user_admin'] = 0 
   $_POST['user_admin'] = 1

are both assignments. PHP evaluates whether the final assigned expression is true or false after assigning the value to $_POST['user_admin'] . So, the first one will evaluate to false since the assigned value is 0, and the second one will evaluate to true since the assigned value is 1.

As everyone pointed out, you have to use "==" instead of "=" for conditional statements.

like image 1
janenz00 Avatar answered Oct 31 '22 21:10

janenz00