php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29758 'and' produces erroneous results
Submitted: 2004-08-19 15:54 UTC Modified: 2004-08-19 16:56 UTC
From: bd at avatartechnology dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.8 OS: Linux RedHat 7.3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bd at avatartechnology dot com
New email:
PHP Version: OS:

 

 [2004-08-19 15:54 UTC] bd at avatartechnology dot com
Description:
------------
Ran into a situation where code that utilized the operator  'and' produced erroneous results.

$free_shipping = true;
$add_fee = 'Y';
$free_shipping = $free_shipping and $add_fee == 'N';

This was producing the result where $free_shipping was always true.  Adding parenthesis makes no difference in result.
--------------

$free_shipping = true;
$residential_fee = 'Y';
$free_shipping = $free_shipping && $residential_fee == 'N';

This accurately produces the correct results.
--------------

According to the documentation on PHP website 'and' and '&&' should be used interchangably with same results.


Reproduce code:
---------------
$free_shipping = true;
$residential_fee = 'Y';
$free_shipping = $free_shipping and $residential_fee == 'N';
printf("free = %d",$free_shipping);

Expected result:
----------------
free = 0

Actual result:
--------------
free = 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-19 16:33 UTC] derick@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. 

Thank you for your interest in PHP.

No, && and and have a different priority (compare to = and ==).

http://no.php.net/manual/en/language.operators.php#language.operators.precedence
 [2004-08-19 16:45 UTC] bd at avatartechnology dot com
$free_shipping = true;
$residential_fee = 'Y';
$free_shipping = $free_shipping and ($residential_fee == 'N');
printf("free = %d",$free_shipping);

This code fails too.  As stated in my previous comments priority of operators is not an issue.  "and" is producing wrong results.  Even if priority was an issue with the previous code example, the result should still be false.  The 'and' operator is IGNORING conditionals after it, which is a PHP bug.
 [2004-08-19 16:50 UTC] bd at avatartechnology dot com
Here is a more basic example.

$true = true;
$false = false;
$result = $true and $false;
printf("result = %d",$result);

This should produce: "result = 0"
This is actually producing: "result = 1"
 [2004-08-19 16:56 UTC] derick@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. 

Thank you for your interest in PHP.

No, it should not. Compare the priority of = and "and".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 14 05:01:34 2024 UTC