I have a list of 14 variables that may or may not be empty. Let's say for example we have many individual variables that holds a select ingredient for a recipe. The number of ingredients in a recipe will differ by recipe, obviously. Is there a way, using PHP, that we can see how many of the 14 variables are not empty (i.e. contain an ingredient)?
Example (using 7 ingredients):
$ing_1 = "carrots"
$ing_2 = "apples"
$ing_3 = "sugar"
$ing_4 = "celery"
$ing_5 = ""
$ing_6 = ""
$ing_7 = ""
How would I get the awnser 4?
Further Info:
I am using mysqli_fetch_array to load to variables.
$ing[1]/$ing[2]/etc - instead of your var structure, then stackoverflow.com/questions/4422889/… or stackoverflow.com/questions/19696281/… or stackoverflow.com/questions/7672562/… could help.mysqli_fetch_arrayI think you got a database in the back. You could select an addional field (count numbers of ingredients) and then just "read" this field where the number of ingredients is saved.$count=0; for($i=1;$i<=7;$i++){ if(!empty(${"ing_$i"})) $count++; } echo $count;