php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44236 short-circuiting on compound boolean in if expression
Submitted: 2008-02-24 18:25 UTC Modified: 2008-02-24 19:48 UTC
From: jvm at jvmyers dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.5 OS: Mac OSX 10.4.11
Private report: No CVE-ID: None
 [2008-02-24 18:25 UTC] jvm at jvmyers dot com
Description:
------------
Note: I am on a Mac and PHP 5.2.4 is the most recent version available from entropy.

Problem: Compound booleans expressed as string args in an 'if' statement don't 'appear' to 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

Case 1: 'if' expression passed as string => "1. Conditions met"
Case 2: same as Case 1, but using catenation operator => "2. Conditions met"
Case 3: same 'if' expression but passed in context => "3. Conditions not met"

Reproduce code:
---------------
<?php
$aArray = array(1,0);
$sCondition = "!empty($aArray[0]) && !empty($aArray[1])";
if ($sCondition)
{
	echo "Case 1. Conditions met<br />";
}
else
{
	echo "Case 1. Conditions not met<br />";
}
if ("".$sCondition."")
{
	echo "Case 2. Conditions met<br />";
}
else
{
	echo "Case 2. Conditions not met<br />";
}
if (!empty($aArray[0]) && !empty($aArray[1]))
{
	echo "Case 3. Conditions met<br />";
}
else
{
	echo "Case 3. Conditions not met<br />";
}
?>

Expected result:
----------------
See, Case 3, above.

Actual result:
--------------
See, Case 1 and Case 2, above.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-24 19:48 UTC] jani@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 02 08:01:31 2024 UTC