The problem I have is the following: I have a variable number of array items, so I can not have a fix number of if then else
. In any case, it can not exceed 50 items.
This is the main-standard body. How to build as many if then else
conditions as the count
of an array on the fly ?
$number = count($array);
if (($a > $b) && ($a <= $b)) {
} else
You are trying to use Switch in fact, please read : http://php.net/manual/fr/control-structures.switch.php
switch ($number) {
case 0:
echo "a";
break;
case 1:
echo "b";
break;
case 2:
echo "c";
break;
}
This is a bit of a guess, as we don't precisely know what you are trying to achieve, but to answer your question "How to build as many if then else
conditions as the count
of an array on the fly?":
<?php
$array = array(1,2,5,8,10,15);
$number = count($array);
$b = 7; // we don't know yet what $a and $b could be
for ($i=0;$i<$number;$i++) {
if($array[$i]>$b) { // what ever condition you want here
// do smth
} else {
// do smth else or nothing
}
}
?>