View Full Version : How do I easily check if one variable equals ANY of a list of values?
JJR512
10-18-2005, 12:49 PM
In my Custom HTTP Error Pages hack, I use a variable $e. The value of $e comes from the URL, as in http://www.yoursite.com/forums/httperror.php?e=404.
I have a check in the script that checks for if $e has a value or not, and if not, it's set to 404 (because the 404 page is the accepted generic page to use if the exact error cannot be determined or does not wish to be revealed).
But I'd like to add another check that makes sure $e is set to an acceptable value, and if not, set it to 404.
Is there an easy way to check if $e is set to ANY item in a list like 400, 401, 403, 404, 405, etc.?
Ranma2k
10-18-2005, 01:17 PM
In my Custom HTTP Error Pages hack, I use a variable $e. The value of $e comes from the URL, as in http://www.yoursite.com/forums/httperror.php?e=404.
I have a check in the script that checks for if $e has a value or not, and if not, it's set to 404 (because the 404 page is the accepted generic page to use if the exact error cannot be determined or does not wish to be revealed).
But I'd like to add another check that makes sure $e is set to an acceptable value, and if not, set it to 404.
Is there an easy way to check if $e is set to ANY item in a list like 400, 401, 403, 404, 405, etc.?
best thing use switch function
switch($e)
case "400" : do fucntion
break;
case "401" : do function
break;
......
case "404" :
default :
do function ;
breake;
or you can store the Value in an array and chek if the value in that array if not set it to 404
use the function inarray to check for it
$erorr = array("400", "401", "402", "403"....);
if (!in_array($e,$erorr))
$e="404";
JJR512
10-18-2005, 01:40 PM
Using the last example, the !inarray bit, if I have an existing IF statement to check the value of $e, can I combine that with this?
This is what I use already:
if ($e == '' OR !$e)
{
$e = '404';
}
Can I make the IF statement like this...
if ($e == '' OR !$e OR !inarray($e,$error))
Assuming I've created the array before the IF statement (of course), will that work?
Ranma2k
10-18-2005, 01:54 PM
Using the last example, the !inarray bit, if I have an existing IF statement to check the value of $e, can I combine that with this?
This is what I use already:
if ($e == '' OR !$e)
{
$e = '404';
}
Can I make the IF statement like this...
if ($e == '' OR !$e OR !inarray($e,$error))
Assuming I've created the array before the IF statement (of course), will that work?
the if staement should be like
if (!(in_array($e,$error)) || $e =='')
or for better
if (!in_array($e,$error) || !isset($e))
JJR512
10-18-2005, 02:08 PM
I have tried that now, and got the error message...
Fatal error: Call to undefined function: inarray() in /home/jjr512/public_html/testbbs/httperror.php on line 51
What's up?
Ranma2k
10-18-2005, 02:09 PM
I have tried that now, and got the error message...
What's up?
sorry it's
in_array
my mistake
JJR512
10-18-2005, 03:17 PM
Thanks! :)
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.