How do make this if statement work for 0, 1 and 2 cats?
When $cats = 0 or 1 it echos "It is fine you only have 2 cats"
Any code suggestions?
<?php
//If you have more then 2 cats it is illegal
//if you have 20 or more cats "you are a freak!"
//If you have 2 cats "It is fine you only have 2 cats".
//If you have 1 cats "It is fine you only have 1 cat".
//If you have 0 cats "You don't have any cats no problem".
$cats = 25;
if ($cats > 2 && $cats < 20){
echo "You have too many cats, it is illegal!";
} elseif ($cats >= 20) {
echo "you are a freak!";
} elseif ($cats = 2) {
echo "It is fine you only have 2 cats";
} elseif ($cats = 1) {
echo "It is fine you only have 1 cat";
} elseif ($cats = 0){
echo "You don't have any cats no problem";
}
?>
Use ==
for comparison, =
is for assignment.
Additionally, consider rearranging your code to this:
switch($cats) {
case 0:
echo "You don't have any cats";
break;
case 1:
echo "You have 1 cat";
break;
case 2:
echo "You have 2 cats";
break;
default:
if( $cats < 20) echo "You have too many cats";
else echo "You are a freak!";
}
Should be ==
(for comparison) and not =
(for assignment)
} elseif ($cats == 2) {
you have used wrong comparison operator syntax
<?php
//If you have more then 2 cats it is illegal
//if you have 20 or more cats "you are a freak!"
//If you have 2 cats "It is fine you only have 2 cats".
//If you have 1 cats "It is fine you only have 1 cat".
//If you have 0 cats "You don't have any cats no problem".
$cats = 25;
if ($cats > 2 && $cats < 20){
echo "You have too many cats, it is illegal!";
} elseif ($cats >= 20) {
echo "you are a freak!";
} elseif ($cats == 2) {
echo "It is fine you only have 2 cats";
} elseif ($cats == 1) {
echo "It is fine you only have 1 cat";
} elseif ($cats == 0){
echo "You don't have any cats no problem";
}
There is a difference between =
and ==
and even ===
Check for the Logical Comparison chart -
if statements in PHP require == not = to work.
try to use ==
instead of =
else if ($cats == 2){}
Error on comparison operator. Just use it (I coded in your current way with some correction):
<?php
//If you have more then 2 cats it is illegal
//if you have 20 or more cats "you are a freak!"
//If you have 2 cats "It is fine you only have 2 cats".
//If you have 1 cats "It is fine you only have 1 cat".
//If you have 0 cats "You don't have any cats no problem".
$cats = 25;
if ($cats == 0){
echo "You don't have any cats no problem";
}else if ($cats == 1) {
echo "It is fine you only have 1 cat";
} else if ($cats == 2) {
echo "It is fine you only have 2 cats";
} else if ($cats > 2 && $cats < 20){
echo "You have too many cats, it is illegal!";
} else {
echo "you are a freak!";
}
?>
Your code might work if you'd just replace the = (assignment) with == (comparison) operator within your if
statements! However, I would go with a switch statement in this specific case, its by far more readable and understandable while scrolling down the code ;)
switch($cats) {
case 0: echo "You don't have any cats"; break;
case 1: echo "You have 1 cat"; break;
case 2: echo "You have 2 cats"; break;
default: echo ($cats < 20 ? "You have too many cats" : "You are a freak!");
}
The Ternary Operator comes in quite handy in situations like this as well!