Forum Moderators: coopster
There are times that I need to check a number of different VARs all at one time for TRUE or FALSE before the script turns down one path or another... like this:
************************************
if ($a && $b && $c && $d)
{
$soldOut = TRUE;
}
else
{
$soldOut = FALSE;
}
***************************************
It's the "$a && $b && $c && $d" that bothers me.
It works fine, but I've got one custom function, for example, that checks a MASS of $_POST values for different validation criteria. All criteria return a TRUE or FALSE to different local var. In this function, the vars run from $a to $hh so it gets pretty long and unwieldy.
How do you guys and gals do this sort of thing? With an array and loop? I'm at that point in my learning of php that I'm getting picky about long and "inefficient looking" code so I'd like to improve on issues like this.
Any suggestions greatly appreciated.
Neophyte
$checker = TRUE; //assume true
foreach($_POST as $key => $value)
{
if ($value!= $whatEverYourCriteriaIs)
$checker = FALSE;
}
if(!$checker)
//Go down path A
else
//Go down path B
That assumes that the only things being posted from the form are things that are relavent to be checked. If you have other information in the form, you'll need to create form elements that are arrays.
For example:
<input type = "text" name ="thing[1]">
<input type = "text" name ="thing[2]">
<input type = "text name = "thing[3]">
<input type = "text" name = "someOtherThing">
If only "thing" is relavent to your check, you'd process the form like this:
$checker = TRUE;
$things = $_POST['thing'];
foreach($things as $key => $value)
{
if ($value!= $whatEverYourCriteriaIs)
$checker = FALSE;
}
if(!$checker)
//Go down path A
else
//Go down path B
I hope that helps.