5
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?

GiantDuck
  • 1,086
  • 3
  • 14
  • 27
  • 10
    Replace `=` with `==` .... also look at http://www.php.net/manual/en/language.operators.comparison.php – Baba Nov 07 '12 at 00:20
  • possible duplicate of [The 3 different equals](http://stackoverflow.com/questions/2063480/the-3-different-equals) – mario Nov 07 '12 at 00:35
  • Thanks! Sometimes you miss something after you've been staring at it for 10 minutes... – GiantDuck Nov 07 '12 at 00:36

4 Answers4

10

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'];
Tim Dearborn
  • 1,178
  • 7
  • 18
6

It should be

if $variable == 0
Ian
  • 181
  • 10
2

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

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
admoghal
  • 1,498
  • 1
  • 10
  • 5
1

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.

janenz00
  • 3,315
  • 5
  • 28
  • 37