Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit a variable value to certain predefined values

What's the shortest and simplest way to do this?

For example:

$var = $_GET['something'];
// how to limit the value of $var to "10", "20", "30", "40", 
// and "" for any other input ?

I mean is there a php helper function that can do this, without having to use 5 IFs ?

like image 913
Alex Avatar asked Dec 16 '22 14:12

Alex


1 Answers

in_array is suitable in this case:

if(!in_array($var, array("10", "20", "30", "40"))
{
    // $var will be "" if it does not equal: "10", "20", "30", or "40"
    $var = "";
}
like image 162
Tim Cooper Avatar answered Jan 07 '23 14:01

Tim Cooper