Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF Statement simplification

How does one achieve multiple checks against one value? I think I'm being a little bit stupid...

What I want to achieve is this:

if(basename($_SERVER['SCRIPT_NAME']) != ("1.php" || "2.php" || "3.php"){
    header('Location: elsewhere.php'); 
}

rather than:

if(basename($_SERVER['SCRIPT_NAME']) != "1.php" && basename($_SERVER['SCRIPT_NAME']) != "2.php" && basename($_SERVER['SCRIPT_NAME']) != "3.php" && basename($_SERVER['SCRIPT_NAME']) != "4.php"){
    header('Location: elsewhere.php'); 
}

I've written it out a few times but I'm clearly licking windows.

Thanks in advance!

like image 980
Arbiter Avatar asked Apr 17 '26 18:04

Arbiter


1 Answers

you can write

$files = array("1.php", "2.php", "3.php", "4.php");//so on

if(!in_array(basename($_SERVER['SCRIPT_NAME']), $files)){
    header('Location: elsewhere.php'); 
}

yes another method is (little bit faster than above one)

if(array_diff((array)(basename($_SERVER['SCRIPT_NAME'])),array("1.php", "2.php", "3.php", "4.php")))
{
 header('Location: elsewhere.php'); 
}
like image 180
ɹɐqʞɐ zoɹǝɟ Avatar answered Apr 20 '26 07:04

ɹɐqʞɐ zoɹǝɟ