Here is my code to check the number of possible conditions for two elements, Is there a way to reduce the number of checking conditions (not any specific rule for checking).
Why i am asking is, I fear that if I add an additional element it will maximise the checking conditions in a vast manner.
How can I do this?
Here is my code:
<?php
$A = 'alpha';
$B = 'beta';
$result = '';
if($A != '' && $B !='')
{
$result .= 'Both has Value';
// both contains value
}
elseif($A != '' && $B =='')
{
$result .= 'Only A';
// only a contains value
}
elseif($A == '' && $B !='')
{
$result .= 'Only A';
// only b contains value
}
else
{
$result .= 'Both are Empty';
// both contains no value
}
echo $A.' - '.$B.'<br>';
echo $result;
?>
Try this...
<?php
$a="123";
$b="";
$c="33";
$result="";
if($a !="")
{
if($result=="")
{
$result .="a"; //Get value $a only
} else {
$result .=" and a"; //Get value $a with $b or $c
}
}
if($b !="" )
{
if($result=="")
{
$result .="b"; //Get value $b only
} else {
$result .=" and b"; //Get value $b with $a or $c
}
}
if($c !="")
{
if($result=="")
{
$result .="c"; //Get value $c only
} else {
$result .=" and c"; //Get value $c with $b or $a
}
}
echo $result;
?>
how about this? i just removed the excess checking.
if($A && $B)
{
$result .= 'Both has Value';
// both contains no value
}
elseif($A)
{
$result .= 'Only A';
// only a contains value
}
elseif($B)
{
$result .= 'Only B';
// only b contains value
}
else
{
$result .= 'Both are Empty';
// both contains value
}
echo $A.' - '.$B.'<br>';
echo $result;
Use boolean flags in a result. In this case bit 0 (0 or 1) indicates A is empty or not and bit 2 (0 or 2) indicates B is empty.
$flags = 0;
if ($A != '')
$flags |= 1; // Binary 0001 = 1
if ($B != '')
$flags |= 2; // Binary 0010 = 2
$rMsg = array ("Both empty", // 0 : 0000
"A only", // 1 : 0001
"B only", // 2 : 0010
"Both full"); // 3 : 0011
$result .= $rMsg [$flags];
I figure you are looking for a useful principal rather than the exact details. Is this what you had in mind?
More generally, given an array of items (up to 32 or 64 depending on MAX_INT):
$flags = 0;
for ($i = 0; $i < count ($items); $i++)
if ($items [$i] != '')
$flags |= 1<<$i;
// Flags start at bit 0
// To check if items 3, 7 and 10 are all filled:
$check = (1<<3)|(1<<7)|(1<<10);
if ($flags & $check == $check)
echo "all set!";
// To check if items 0, 5 and 8 are all empty:
$check = (1<<0)|(1<<5)|(1<<8);
if ($flags & ~$check == 0)
echo "all clear!";
Check this program which i created only for this question.Here take your variables in an array. You can take any no. of elements.
$array = array('alpha','beta','theta','gamma');
$result = array();
$final = '';
foreach($array as $key=>$value)
{
if($value != '')
{
array_push($result,$value);
if(sizeof($result) == 1)
$final .= $value;
else
$final .= ','.$value;
}
}
if(sizeof($result) == 0)
echo "All are Empty";
elseif(sizeof($result) == sizeof($array))
echo "All are Value";
else{
echo "Only ".$final;
}
In a more abstract way, this useful little function:
function partition($ary, $predicate) {
$result = [[], []];
foreach($ary as $item)
$result[$predicate($item) ? 1 : 0] []= $item;
return $result;
}
splits an array into two parts based on some criteria, given as a boolean function. Applied to your specific problem:
$data = array('a', 'b', 'c', '');
list($bad, $good) = partition($data, 'boolval');
if(!$good)
echo 'all are falsy';
elseif(!$bad)
echo 'all are truthy';
else {
echo 'bad items: '; print_r($bad);
echo 'good items: '; print_r($good);
}