note 33009 added to language.operators.logical
| From: | braintrino at rack1 dot php dot net | Date: | Fri, 13 Jun 2003 16:59:59 +0000 |
| Subject: | note 33009 added to language.operators.logical | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-50294@lists.php.net to get a copy of this message | ||
[Note: the last post has an error. Use this post.]
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!!!
----
Manual Page -- http://www.php.net/manual/en/language.operators.logical.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33009
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33009&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33009&report=yes