note 33009 deleted from language.operators.logical by colder
| From: | colder@php.net | Date: | Fri, 20 Apr 2007 15:39:05 +0000 |
| Subject: | note 33009 deleted from language.operators.logical by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125698@lists.php.net to get a copy of this message | ||
Note Submitter: braintrino
----
shadedecho's post to force evaluation of both OR expression is actually exactly what should be
done to suppress short-circuit optimization.
There are many occasions that you don't want the compiler to short-circuit any evaluation,
especially when you want to do an and/or situation. The short-circuit evaluation does an OR
operation but not an AND/OR operation!!!
For instance, if I want the check the user's query form to see if the user has missed answering
any entry AND/OR if the user had duplicated the same answer for more than one entry, then I need to
send the form back to the client informing what I want to be corrected.
function SomeAnswersAreMissing()
{
...
echo "Oops! You missed answering some questions.";
return TRUE;
}
function SomeAnswersAreDuplicated()
{
...
echo "Oops! You can answer both the same way.";
return TRUE;
}
if (SomeAnswersAreMissing() || SomeAnswersAreDuplicated())
SendFormAgain();
If I do that, the user will only see the missing answer warning but not the duplicated answer
warning, even if both are true. This is not informative to the user because he/she will have to
re-submit the form twice before he/she realized everything he/she did wrong, and frustrate the hack
out of them. That is not user-friendly.
But if I use:
if (SomeAnswersAreMissing() | SomeAnswersAreDuplicated())
SendFormAgain();
Then both warning messages are sent at the same time, and the user can correct it in one re-send.
Thank shadedecho, I had been looking for a way to override the compiler's stupid short-circuit
optimization. The computer cannot just assume that short-circuiting is the best :(
BTW, of course you can do:
$you_made_a_mistake = false;
if (SomeAnswersAreMissing())
$you_made_a_mistake = true;
if (SomeAnswersAreDuplicated())
$you_made_a_mistake = true;
if ($you_made_a_mistake)
SendFormAgain();
But that is convoluted logic!!!