When comparing values for logic operations, you must use >
, <
, ==
, ===
, !=
, or !==
. You are using a single equals sign, which is not for comparison but for assignment.
This is what you are doing
$item = 'b'; // a single equals sign assigns a value
if ($item = 'a') { // so in this line, we are not comparing but assigning!
echo 'Item is a'; // this line will always be reached
} else {
echo 'Item is b'; // this line will NEVER be reached
}
echo 'Item = '.$item; // will ALWAYS read "Item = a"
This is what you meant to do
$item = 'b'; // a single equals sign assigns a value
if ($item == 'a') { // double-equals compares value, with type juggling
echo 'Item is a';
} else {
echo 'Item is b'; // now this line will be reached, because item != 'a'
}
echo 'Item = '.$item; // will read "Item = b"
`==` - value is equal, not necessarily the same type (1 == "1")
`!=` - value is not equal, may or may not be the same type (2 != "3")
`===` - value is equal AND the same type (1 === 1)
`!==` - value is not the same type, or not the same value (2 !== "2")
Documentation
- PHP Comparison operators: http://php.net/manual/language.operators.comparison.php
1
solved msyql database selection based on if else statement and php variables