Store IF output in a variable to use later

Asked

Viewed 2,297 times

-1

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

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

5 answers

6

Don’t need a normal if just a ternary

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

5

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

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

It’s completely unnecessary to do with if, but can:

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

  • but I could do with if? wondered how to put an if in the variable

3

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

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

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

But as the staff spoke in the answers above, the if ternary solves the problem with less code:

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

3


If you want you can store the result of the comparison in the variable $os. Since 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 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.

1

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

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.