I am processing a form and in turn, receiving a response code based on the information submitted. I have a list of approximately 40 response codes (and their meaning) in my hands and am trying to build an 'if' statement that checks against a predefined array and returns a specific value.
Just not sure how to do this
First pass conceptually:
$bads = array (1,2,3,4,5,6)
if ($output['responsecode'] == (any value in $bads) {
echo "you suck";
}
EDIT - Still receiving errors
I am using the following code:
$bad_resp1 = array("D","M","A","B","W","Z","P","L","N","C","U","G","I","R","E","S","0","O","B");
$bad_resp2 = array("N","P","S","U");
$bad_resp3 = array("200","201","开发者_开发技巧202","203","204","220","221","222","223","224","225","250","261","262","263","264","300","400","410","411","420","421","430","440","441","460","461");
then calling the 'if' statement:
if (in_array($output['response1'], $bad_resp1) || in_array($output['response2'], $bad_resp2) || in_array($output['response3'], $bad_resp3)) {
Header("Location: fail.php");
}
I get the following error(s):
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\site\xyz.php on line 362
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\site\xyz.php on line 362
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\site\xyz.php on line 362
Use in_array()
if (in_array($output['responsecode'], $bads)) { echo "you suck"; }
in_array
if(in_array($output['responsecode'], $bads))
{
}
Also, if your codes are sequential, you can use range to generate the array.
$bad = range(1, 10);
How about you have a $errors
array and only add to it if there's an error. If the $errors
array is not empty, echo "All aboard the fail train!"
As others have suggested, you can do:
if (in_array($output['responsecode'], $bads))
....
However, this is more efficient because it does not require a traversal of the $bads array:
$bads = array (1 => null, 2 => null, 3 => null, 4 => null,5 => null, 6 => null);
if (array_key_exists($output['responsecode'], $bads))
....
精彩评论