note 81363 added to language.expressions
| From: | jvm at jvmyers dot com | Date: | Sun, 24 Feb 2008 20:20:31 +0000 |
| Subject: | note 81363 added to language.expressions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136461@lists.php.net to get a copy of this message | ||
<?php
// Compound booleans expressed as string args in an 'if' statement don't work as
expected:
//
// Context:
// 1. I generate an array of counters
// 2. I dynamically generate a compound boolean based on selected counters in the array
// Note: since the real array is sparse, I must use the 'empty' operator
// 3. When I submit the compound boolean as the expression of an 'if' statement,
// the 'if' appears to resolve ONLY the first element of the compound boolean.
// Conclusion: appears to be a short-circuiting issue
$aArray = array(1,0);
// Case 1: 'if' expression passed as string:
$sCondition = "!empty($aArray[0]) && !empty($aArray[1])";
if ($sCondition)
{
echo "1. Conditions met<br />";
}
else
{
echo "1. Conditions not met<br />";
}
// Case 1 output: "1. Conditions met"
// Case 2: same as Case 1, but using catenation operator
if ("".$sCondition."")
{
echo "2. Conditions met<br />";
}
else
{
echo "2. Conditions not met<br />";
}
// Case 2 output: "2. Conditions met"
// Case 3: same 'if' expression but passed in context:
if (!empty($aArray[0]) && !empty($aArray[1]))
{
echo "3. Conditions met<br />";
}
else
{
echo "3. Conditions not met<br />";
}
// Case 3 output: "3. Conditions not met"
// jvm@jvmyers.com
?>
PS: the bug folks say this "does not imply a bug in PHP itself." Sure bugs me!
----
Server IP: 69.147.83.197
Probable Submitter: 70.196.169.82
----
Manual Page -- http://www.php.net/manual/en/language.expressions.php
Edit -- https://master.php.net/note/edit/81363
Del: integrated -- https://master.php.net/note/delete/81363/integrated
Del: useless -- https://master.php.net/note/delete/81363/useless
Del: bad code -- https://master.php.net/note/delete/81363/bad+code
Del: spam -- https://master.php.net/note/delete/81363/spam
Del: non-english -- https://master.php.net/note/delete/81363/non-english
Del: in docs -- https://master.php.net/note/delete/81363/in+docs
Del: other reasons-- https://master.php.net/note/delete/81363
Reject -- https://master.php.net/note/reject/81363
Search -- https://master.php.net/manage/user-notes.php