Hello everyone I would like to be able to compare the country variable with country values and if the country variable is not in my list display an error I have a code but it does not work
else if($country <> "Belgique")or ($country <> "Luxembourg") or ($country <> "France")or($country <> "Pays-bas")
{echo "The country variable is not in the list "; }
CodePudding user response:
You can use in_array function instead of many if else.
$country_list = array("Belgique", "Luxembourg", "France", "Pays-bas");
if(!in_array($country, $country_list)) {
echo "The country variable is not in the list";
}
CodePudding user response:
Firstly, you have a fairly straight-forward syntax error: the syntax of an if
statement is if ( condition )
Note those parentheses - they're part of the if statement itself, not the condition. When you write this:
if ( $country <> "Belgique" )
The condition is this:
$country <> "Belgique"
So if you want the condition to be this:
($country <> "Belgique") or ($country <> "Luxembourg") or ($country <> "France") or ($country <> "Pays-bas")
Then you have to put the whole thing inside the parentheses of the if statement:
if ( ($country <> "Belgique") or ($country <> "Luxembourg") or ($country <> "France") or ($country <> "Pays-bas") )
Secondly, you have made a common mistake in translating English (or whatever your first language is) into formal logic: it's tempting to assume that ($country <> "Belgique") or ($country <> "Luxembourg")
means "$country is neither Belgique nor Luxembourg", but it actually means "if $country is not Belgique then evaluate to true; also, if $country is not Luxembourg then evaluate to true".
If you provide "Luxembourg", the first part is true; if you provide "Belgique", the second part is true; in fact, since no value of $country
is both Belgique and Luxembourg at once, the result is always true!
What you actually wanted to say was "if $country is not Belgique and also $country is not Luxembourg, then evaluate to true": ($country <> "Belgique") and ($country <> "Luxembourg")
So:
if ( ($country <> "Belgique") and ($country <> "Luxembourg") and ($country <> "France") and ($country <> "Pays-bas") )
(This fact that not ( A or B )
is equivalent to ( not A ) and ( not B )
is known as De Morgan's Law.)
As a side note, in PHP, and
and or
are not quite the same as &&
and ||
. See stackoverflow.com/questions/4502092/php-and-or-keywords
Although it doesn't make any difference in this case, because you've grouped things neatly with parentheses, &&
and ||
are more often the operators you want, giving:
if ( ($country <> "Belgique") && ($country <> "Luxembourg") && ($country <> "France") && ($country <> "Pays-bas") )
<>
is identical in meaning to !=
, but there is also the stricter !==
, see How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ? and Is there a difference between !== and != in PHP?
So you might actually want this:
if ( ($country !== "Belgique") && ($country !== "Luxembourg") && ($country !== "France") && ($country !== "Pays-bas") )