0

I have this variable in php $get_user_tipo and it gets their own value from a mysqli field (int) and its a numeric value, i'm usign a number to represent a role, por examble 1 for admins, and 2 for employees.

I want to do a conditional statement for the variable and compare it with a value, for example, (a bad, bad example)

if($get_user_tipo=1) {
      do something
} else { 
      Do nothing , just jump to the next check
}
if($get_user_tipo=2) {
      do something
} else { 
      Do nothing , just jump to the next check
}

i'm pretty new in PHP

  • 1
    `if( $get_user_tipo == 1 ) { ... } elseif( $get_user_tipo == 2 ) { ... } else { ... }` Take a look at [basic php tutorial](http://www.w3schools.com/php/) – fusion3k Feb 25 '16 at 00:48

3 Answers3

0

You need to make sure you use double equals to compare values.

if($get_user_tipo == 1) {
      //do something
} else { 
      //Do nothing , just jump to the next check
}
if($get_user_tipo == 2) {
      //do something
} else { 
      //Do nothing , just jump to the next check
}
sketchthat
  • 2,678
  • 2
  • 13
  • 22
0

Use double = to compare the value as the other answer says

<?php

if ($get_user_tipo == 1) {
    //do something
} elseif ($get_user_tipo == 2) {
    //do something
}

?>
sketchthat
  • 2,678
  • 2
  • 13
  • 22
Neobugu
  • 333
  • 6
  • 15
0

To clarify the other answer, a single = is treated as an assignment operator. This means that you are actually setting $get_user_tipo to 1 in the if statement.

What you want is a comparison operator:

  • == is a loose equals comparison, meaning '1' (string) is equal to 1 (integer) (type-juggling).
  • === is a strict comparison, meaning '1' (string) will not be equal to 1 (integer)

The comparison operators manual: http://php.net/manual/en/language.operators.comparison.php

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95