I am trying to write PHP code that can do the following: If score is less or equal to 50 then print "take a quiz", elseif score is between 51 and 80 then print "take a test", else print "you have passed" So the main proble is how to get operators for between 50 and 80(to count all the numbers from 51 up to 80). Thank you!
答案 0 :(得分:0)
I think your looking for this
if($score <= 50)
echo 'take a quiz';
else if($score < 80)
echo 'take a test';
else echo 'you have passed';
答案 1 :(得分:0)
To do this, you will need to combine three operators, the >
, <
, and &&
.
The &&
operator evaluates its left and right hand side and returns a truthy value if both of them are truthy, and a falsey value if one or both of them are falsey. (The returned value will be one of the arguments).
This is called an and operator because it return a truthy value only if A and B are true.
To combine everything, one would do if($score < 82 && $score > 50)
.
However, there's actually an easier way in this case.
Assuming that $score
is an integer, it's greater than or equal to 51 if it isn't less than 50. And since the else-if won't run unless all previous clauses in the if statement didn't run (i.e. only one of the possible code blocks will run), then you only have to check that $score
is less than 80).
So that means, in this case, while the &&
operator could be used, we could just write
if($score < 50) {
// ...
else if ($score < 80) { // "else if" won't run if ($score < 50) was false.
// ...
} else { // won't run unless ($score < 80) and ($score < 50) were both false
// ...
}
That's why the idea of else
is very useful.