-4

How can I set one variable to true based on other conditions. Here, instead of doing

if ($vara && $varb && $varc)

I'm something like below. Problem is, I'm just not getting something right. Can you please help me?

<?php

$onward = false;

$vara = 11;
$varb = 21;
$varc = 3;

if ($vara == 1)
{$onward = true;}else{$onward = false;}

if ($varb == 2)
{$onward = true;}else{$onward = false;}

if ($varc == 3)
{$onward = true;}else{$onward = false;}

if ($onward)
{
echo "Ok";
}else {echo "Not ok";}

?>
4

6 回答 6

4

Each of your conditions ignores the result of the previous condition. You need to include the previous state of $onward in each subsequent test:

if ($vara == 1)
{$onward = true;}else{$onward = false;}

if ($varb == 2 && $onward)
{$onward = true;}else{$onward = false;}

if ($varc == 3 && $onward)
{$onward = true;}else{$onward = false;}

This way, varb and varc are only tested if $onward is still true after the previous test.

This is a particularly ugly way of writing code. If you have three large conditions and you don't simply want to join them on one line as in your $vara && $varb && $varc, you should be writing it this way:

$onward = ($vara == 1)
$onward = $onward && ($varb == 2);
$onward = $onward && ($varb == 3);    

Any time you're simply returning/setting something to true/false in the branches if your if statement, you should just be returning/setting the condition itself.

That is, this:

if (condition) {
  return true;
} else {
  return false;
}

should always be written:

return condition;
于 2012-08-16T16:09:10.113 回答
4

The problem is, you are overwriting the value of $onward in every if-statement. Just use

$onward = $vara == 1 && $varb == 2 && $varc == 3;
于 2012-08-16T16:09:46.090 回答
3

I think you are looking for:

$onward = $vara == 1 || $varb == 2 || $varc ==3;

or

$onward = $vara == 1 && $varb == 2 && $varc == 3;

depending on your goal.


With this:

if ($varc == 3)
{$onward = true;}else{$onward = false;}

$onward will always be false if $varc is not 3.

于 2012-08-16T16:09:42.930 回答
2

I'm sure you mean

if (($vara == 1) && ($varb == 2) && ($varc == 3))
{$onward = true;}else{$onward = false;}

or even

$onward = (($vara == 1) && ($varb == 2) && ($varc == 3));
于 2012-08-16T16:10:23.967 回答
1

A better way of doing this:

if ($vara == 1) {$onward = true;} else {$onward = false;}

is this:

$onward = ($vara == 1);
于 2012-08-16T16:09:08.350 回答
0

You're overwriting the same variable each time, so the first two conditions are actually pointless.

I don't know why you'd want to do this over the first, more succinct approach, which will have the same logical effect:

if ($varc ==== 1 || $varc === 2 || $varc === 3)
    $onward = true;
else
    $onward = false;

Or even just:

$onward = $varc === 1 || $varc === 2 || $varc === 3;

Beware also of ever doing == 1. In comparisons, data are coerced to their truthy/falsy equivalents, so any truthy value will resolve to true in the comparison == 1. To test that something is literally 1, use ===. (This is a good rule generally unless you know you explicitly want to test for value and not type.)

于 2012-08-16T16:11:00.387 回答