How do you compare a PHP variable against multiple values?

MacM

New member
I'm trying to see if a variable is one of something. Such as:
if ($sort == ('year' || 'make' || 'model' || 'color')) {}
 
Used multiple lines for better readability:

if ($sort == 'year' ||
$sort == 'make' ||
$sort == 'model' ||
$sort == 'color' ) {}

You may want to consider a switch statement

switch ($sort ) {
case 'year':
case 'make':
case 'model':
case 'color':
echo "it was one of those";
break;
default:
echo "it was not";
}
 
Used multiple lines for better readability:

if ($sort == 'year' ||
$sort == 'make' ||
$sort == 'model' ||
$sort == 'color' ) {}

You may want to consider a switch statement

switch ($sort ) {
case 'year':
case 'make':
case 'model':
case 'color':
echo "it was one of those";
break;
default:
echo "it was not";
}
 
Back
Top