Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
$a = $b ^^ $c You sure? It didn't work for me, and it isn't listed in the PHP manual. I used: ?php var_dump(1 ^^ 0); var_dump(0 ^^ 1); var_dump(0 ^^ 0); var_dump(1 ^^ 1); ? to test it and got a syntax error. I suppose you could use the bitwise one as a bit of a hack, so long as you 'boolean-ised' both arguments first. eg $a = (!!$b) ^ (!!$c); // or even $a = !$b ^ !$c; since xor depends exclusively on both sides being different to each other. Generally however there is no need to parentisize everything to the right of the assignment operator(=), since besides 'and', 'or', 'xor', and the comma operator. The only time I see the need to parentisize everything to the right is when using the xor operator, since there is no equivlent above the = operator. $a = $b ^^ $c; -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ http://www.smempire.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Rory Browne wrote: | I think what Jose is trying to say, is that because 'or' has a lower | precidence than =, you are interpreting the expression wrong. Yes, I was trying to say that. The () are very important when you need to use OR, AND, ||, , etc... Bye!, Merry Christmas and a happy new year for all!! - -- Jose Miguel Herrera M. - User #246070 counter.li.org Est.Ing.Civil Informatica - UTFSM Valparaiso, Chile - http://www.inf.utfsm.cl/~jherrera -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.4 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFB0DJ/l/j2VHFHn8wRAp7DAJwOKLs64UJMDiLFpOv/4vJ494UZxgCeJPzh WTBDqE0yj4abqyM9Nabjm4Y= =sH6h -END PGP SIGNATURE- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
I think what this highlights is that the or operator isn't supposed to be used in that way. It is simply supposed to be used to add a back door to failed function calls. ie mysql_connect(args) or die(mysql connection failed) If you want to return true when either $a or $b is true, then use ||. eg $ret = $a || $b; return $a || $b; // although 'or' would work here too In the end, whilst you should try to have a general idea of the precidence operators, for the purpose of debugging other peoples code, you should always parentisize your own. Generally however there is no need to parentisize everything to the right of the assignment operator(=), since besides 'and', 'or', 'xor', and the comma operator. The only time I see the need to parentisize everything to the right is when using the xor operator, since there is no equivlent above the = operator. Check out http://www.php.net/manual/en/language.operators.logical.php and follow the link to operator Precidence. Sorry if you recieved this twice. I forgot to cc it to the list first time around. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
On Mon, 27 Dec 2004 21:19:39 +, Rory Browne [EMAIL PROTECTED] wrote: Generally however there is no need to parentisize everything to the right of the assignment operator(=), since besides 'and', 'or', 'xor', and the comma operator. The only time I see the need to parentisize everything to the right is when using the xor operator, since there is no equivlent above the = operator. $a = $b ^^ $c; -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ http://www.smempire.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
I think what Jose is trying to say, is that because 'or' has a lower precidence than =, you are interpreting the expression wrong. $b1 = is_null($x) or ($y 5); is the same as ($b1 = is_null($x)) or ($y 5) This assigns the value of is_null($x) to $b1, regardless of the result of ($y 5). It's shorthand for if( !($b1 = is_null($x)) ){ ($y 5); } similarly $b2 = ($y 5) or is_null($x); assigns $b2 to ($y 5), or in this case true(since $y == 10). If ($y 5) returned false, ie if $i = 5, then it would call the function is_null($x). In this case calling the function is_null() is pointless, since you aren't using the result. What I think you are looking for is either $b2 = (($y 5) or is_null($x)); // whole thing right of = enclosed in brackets or $b2 = ($y 5) || is_null($x); // '||' has higher precidence than 'or' On Fri, 24 Dec 2004 16:47:42 -0300, Jose M.Herrera [EMAIL PROTECTED] wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Bogdan Ribic wrote: | Here's a little test script: | | | $x = 2; | $y = 10; | | $b1 = is_null($x) or ($y 5); | $b2 = ($y 5) or is_null($x); Yes, of course. Your code or example, is just like: ( $b1 = is_null($x) ) or ( $y 5 ) ; ( $b2 = ($y 5) ) or is_null($x) ; The has more precedence than =, or it has a very low precedence. Then, $b1 = false and $b2 = True... that's ok! :P You example, must have been: $b1 = ( ( is_null($x) ) or ( $y 5 ) ); $b2 = ( ( $y 5 ) or ( is_null($x) ) ); I this example the value of $b1 is all between the parenthesis (explicitly). Bye! - -- Jose Miguel Herrera M. - User #246070 counter.li.org Est.Ing.Civil Informatica - UTFSM Valparaiso, Chile - http://www.inf.utfsm.cl/~jherrera -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.4 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBzHJdl/j2VHFHn8wRArZdAKCJbv8W54vlpeinK1hMF3xEttjuiACeIIUs 63OX2bn+h9zLUDHhSvSTr/M= =3vfi -END PGP SIGNATURE- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
Bogdan Ribic wrote: $b1 = is_null($x) or ($y 5); $b2 = ($y 5) or is_null($x); To add to what has been posted already: If 'or' isn't what you want '||' probably *IS* what you want. Watch out though -- Sometimes even the precedence of '||' isn't as high/low as I hoped, and if in doubt, use parentheses to force things to happen in the order you want. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Bogdan Ribic wrote: | Here's a little test script: | | | $x = 2; | $y = 10; | | $b1 = is_null($x) or ($y 5); | $b2 = ($y 5) or is_null($x); Yes, of course. Your code or example, is just like: ( $b1 = is_null($x) ) or ( $y 5 ) ; ( $b2 = ($y 5) ) or is_null($x) ; The has more precedence than =, or it has a very low precedence. Then, $b1 = false and $b2 = True... that's ok! :P You example, must have been: $b1 = ( ( is_null($x) ) or ( $y 5 ) ); $b2 = ( ( $y 5 ) or ( is_null($x) ) ); I this example the value of $b1 is all between the parenthesis (explicitly). Bye! - -- Jose Miguel Herrera M. - User #246070 counter.li.org Est.Ing.Civil Informatica - UTFSM Valparaiso, Chile - http://www.inf.utfsm.cl/~jherrera -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.4 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBzHJdl/j2VHFHn8wRArZdAKCJbv8W54vlpeinK1hMF3xEttjuiACeIIUs 63OX2bn+h9zLUDHhSvSTr/M= =3vfi -END PGP SIGNATURE- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] A serious bug? or operator gives out diffferent results depending on order of operands
Here's a little test script: $x = 2; $y = 10; $b1 = is_null($x) or ($y 5); $b2 = ($y 5) or is_null($x); var_dump($b1); var_dump($b2); Obviously, it should be false for both $b1 and $b2, but the output is: bool(false) bool(true) Is this a bug? I tried dumping values of two expressions, they are both boolean and correct value. Tried assigning values to two temp vars and then or-ing these two, and that gave correct results. I really cannot see what am I doing wrong. Tried this with php 4.3.2 and 4.3.9, and I get these weird results both times. Maybe I should herd cattle or something :) Boban. -- Please reply to the list and not to my email directly. I use usenet server and do not check yahoo email account. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
Bogdan Ribic wrote: Here's a little test script: $x = 2; $y = 10; $b1 = is_null($x) or ($y 5); $b2 = ($y 5) or is_null($x); var_dump($b1); var_dump($b2); Obviously, it should be false for both $b1 and $b2, but the output is: bool(false) bool(true) Is this a bug? I tried dumping values of two expressions, they are both boolean and correct value. Tried assigning values to two temp vars and then or-ing these two, and that gave correct results. I really cannot see what am I doing wrong. Tried this with php 4.3.2 and 4.3.9, and I get these weird results both times. Maybe I should herd cattle or something :) The whole point of OR is that it is a very low precedence operator. Think of it as being at the bottom of the precedence table (it isn't quite, ',' is lower) so you can do: $a = b() + $c / $d OR echo false; That is, do everything before the OR first, then see what that evaluates to before applying the OR. This wouldn't work if it took precedence over anything in the rest of the expression. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] A serious bug? or operator gives out diffferent results depending on order of operands
Uh.. $y is 5. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php