Store IF result in a variable for later use

-1

This code is in error when I call the variable $os :

$os = (if($status_os_cliente == "A"){
     echo "SIM";
} else{
    echo "NÃO";
});
    
asked by anonymous 13.11.2017 / 14:06

5 answers

3

If you want, you can store the result of the comparison in the $os variable. As the assignment operator is associative to the right, it will return a boolean with the result of the comparison.

if ($os = $status_os_cliente == "A") {
    echo "SIM";
} else {
    echo "NÃO";
};

With this you save the result in the if, not what happens inside the loop. For this it is necessary to do the assignment within the loop, as already pointed out in other answers.

    
14.11.2017 / 12:20
6

Do not need a normal if just a ternary

$os = $status_os_cliente == "A" ? 'SIM' : 'NÃO';
echo $os;
    
13.11.2017 / 14:07
5

If you want to have a variable with a value that is set conditionally you can not set this value with a if , you have two alternatives: one is to use the conditional operator, which some call erroneously ternary, condition and two values, one that will be used if the condition is true and another if it is false, in this way can use with the is demonstrating, but of course the syntax is different; the other is to use if to control the flow, and this is why if is even, instead of choosing a value you choose how to assign the value, then you have two assignments, one with a value and another with another value.

$os = $status_os_cliente == "A" ? "SIM" : "NÃO";
echo $os;

It is completely unnecessary to do with if , but you can:

if ($status_os_cliente == "A") {
    $os = "SIM";
} else {
    $os = "NÃO";
}
echo $os;

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

I would say that the form with if in this case is conceptually wrong, although it makes no practical difference at all. There are those who disagree since it depends on interpretation. It depends on whether you are considering whether you are selecting values (operator) or selecting actions (flow control).

    
13.11.2017 / 14:08
3

The error is because you are trying to assign a value to the variable $os but within the if / else you have to print a text on screen ( echo ).

If you want to keep the complete structure, as in the question, use this:

if($status_os_cliente == "A"){
   $os = "SIM";
} else{
   $os = "NÃO";
}

But as the staff spoke in the answers above,% tero% solves the problem with less code:

$os = $status_os_cliente == "A" ? "SIM" : "NÃO";
    
13.11.2017 / 14:12
1

You can do both forms, with both IF and ELSE as ternary, I will put the two alternatives

if ($status_os_cliente == "A"){
  $os = "SIM";
} else{
  $os = "NÃO";
}

Ternary as friends have already shown above, will stay this way, simpler and more practical I believe. Where it indicates that if the received value was A, it will show "YES", and otherwise it will show "NO"

$os = $status_os_cliente == "A" ? "SIM" : "NÃO";
echo $os;
    
13.11.2017 / 14:13