ID:               36799
 User updated by:  cleo at anarki dot dk
 Reported By:      cleo at anarki dot dk
 Status:           Bogus
 Bug Type:         *General Issues
 Operating System: WinXP, Mandriva Linux 2006
 PHP Version:      5.1.2
 New Comment:

Please, I *am* using the bug system to report a rather hideous bug.

You mention an example, where = needs to have higher precedence than
or:
  $foo = query() or die();
Now, this is a smart hack one can use in the absence of proper error
handling, like exception handling.
But it is a hack, and it messes up semantics: It says that if query()
is false, then $foo should be assigned the value of die(). But this is
not what happens because of how die() works.
It is, in my opinion, not an argument strong enough to overpower the
hideous error that:
  $test= true and false;
evaluates to true.
Don't you agree with me, that this is unfortunate, to say the least?

Regards
Claus Holm, Copenhagen, Denmark


Previous Comments:
------------------------------------------------------------------------

[2006-03-21 10:11:20] [EMAIL PROTECTED]

'and' and 'or' have the same precedence level.  

$foo = query() or die();

You need the = to be higher precedence here.

Also, please don't use our bug system as a support forum.

------------------------------------------------------------------------

[2006-03-21 09:18:02] cleo at anarki dot dk

Thanks Rasmus,

At last someone comes up with an explanation.
I can see the point of having to versions of boolean and, each with
different precedence.

However, the precedence of 'and' should definitely not be lower than
'='. 
What is your argument?

I mean:
  $test= true and false;
  echo $test ? "ok" : "nope";

I am sorry, but it can never be correct to let $test evaluate to true
here!!!

As I see it, PHP does the following:
  ($test= true) and false
So, $test will be set to true, and the assignment will be true, and
that assignment will be and'ed with false, and the result is
discarded.
But who would want that?

Conclusion:
  $test1= true and false;  // Evaluates to true
  $test2= true && false;   // Evaluates to false

'=' and 'and' have wrong precedences in PHP.
One should always use && instead of 'and', as && used in an assignment
statement gives the results you would expect from boolean algebra.
And 'and' does not.

Best regards, and the hope of recognition of my arguments
Claus Holm, Copenhagen, Denmark.

------------------------------------------------------------------------

[2006-03-21 01:59:51] [EMAIL PROTECTED]

The whole point of 'and','or' is to be the low-precedence version of
'&&', '||'.  If you want high-precedence, use &&.

------------------------------------------------------------------------

[2006-03-20 20:05:03] [EMAIL PROTECTED]

http://www.php.net/manual/en/language.operators.php#language.operators.precedence

------------------------------------------------------------------------

[2006-03-20 19:59:05] cleo at anarki dot dk

All right, 
  ( (p1) and (p2) ) 
evaluates correctly,
but that doesn't change the fact, that 
  (p1) and (p2)
evaluates totally wrongly.
And I don't see the difference.
Neither from a mathematician's nor a computer scientist's point of
view.
Therefore, I still strongly argue, that this is a bug in PHP's way of
evaluating boolean expressions.

Best regards
Claus Holm from Copenhagen

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/36799

-- 
Edit this bug report at http://bugs.php.net/?id=36799&edit=1

Reply via email to