"if" does not match the [closed]

-2

I'm trying to apply a if to my code, but it's coming wrong:

<?php     
echo $usu_id . "<br />";
echo $centraliz . "<br />";
echo $marca . "<br />";

if($centraliz = "S"){

echo "É centralizada";

} else {

echo "Não é centralizada";

}
?>

But the result is coming like this:

    
asked by anonymous 25.10.2017 / 12:44

2 answers

1

A practical example for better understanding:

<?php   
$variavel = 'true';

if($variavel == "true"){
   echo "1";
}
if($variavel == true){
   echo "2";
}
if($variavel === true){
   echo "3";
}
if($variavel = true){
   echo "4";
}
echo $variavel;

The values that will be displayed on screen: 1, 2, 4 and 1

($ variable == true) = True because it is the same string. ($ variable == true) = True because true is equal to true. ($ variable === true) = False because true is true, but the types are different, one is a string and the other is Boolean. ($ variable = true) = true since it is a simple value assignment, this case will only be false if the assignment fails, usually the false return happens when the value for comparison comes from a function, the function can return something that is impossible to assign.
And the value 1 of the end is the result of the "$ variable" since after ($ variable = true) its value has changed to true because of the assignment and when it shows on the screen it shows 1 that is true for php, if you do so 'echo true;' the result on the screen will be 1 as well.

    
25.10.2017 / 13:08
10

Switch to

if ($centraliz == "S") {

The = operator is assignment, == is for comparison. So you're stating that centralizada is S and of course a statement is always true. In some situations (not in this) it is even necessary to use === to ensure that the two operands are of the same type.

    
25.10.2017 / 12:47