Suppose, I am using a if statement as such:
if(A || B || C || D)
{
echo "Hurrah! if is satisfied!";
echo "But! How can I know which was true of the 4 (A,B,C,D)";
}
Is there any way I can know which condition(s) among A,B,C,D were true?
6
Well, there’s a simple statement that you’re probably familiar with:
if(A || B || C || D)
{
echo "Hurrah! if is satisfied!";
echo "But! How can I know which was true of the 4 (A,B,C,D)";
if (A)
echo "A was true";
if (B) /* NOT else if */
echo "B was true";
}
There’s no way to unscramble an egg. You can’t get A
back from A||B||C||D
.
0
You kinda can….
Assuming each of A, B, C and D are either 0 or 1:
result = A + 2*B + 4*C + 8*D;
if (result) {
echo "Hurrah! if is satisfied!";
// you can now check different bits of your result
}
1