Page 1 of 1
&& || AND && WHY
Posted: Thu May 18, 2006 9:11 pm
by Benjamin
I think AND is easier to read. When I write code I tend to write it so it's easy for others to understand.
Code: Select all
if (($Blah < $Blah2) && ($Blah3 < $Munchkins))
Re: && || AND && WHY
Posted: Thu May 18, 2006 9:21 pm
by alex.barylski
agtlewis wrote:I think AND is easier to read. When I right code I tend to write it so it's easy for others to understand.
Code: Select all
if (($Blah < $Blah2) && ($Blah3 < $Munchkins))
It is easier to read...but that would be going against standard practice...as PHP is a C derivative...
It's bad enough every programmer has his/her own programming styles...by using the literal equivelants for logical connectives...you just further convoluting your source code and although not confusing anyone...certainly going to make people think your a newbie with a single booby using those instead of traditional &&
My 2 cents
Cheers

Posted: Thu May 18, 2006 9:28 pm
by Benjamin
Well that maybe true, but I really don't care if people think I am a newbie. AND is just easier to read for me.
Posted: Thu May 18, 2006 9:34 pm
by s.dot
&& has a slightly higher precedence than AND in the order of operator precendence
[edit]
http://us2.php.net/manual/en/language.o ... precedence
Posted: Thu May 18, 2006 9:37 pm
by Benjamin
I read that, but I can't think of an example of where && or AND would ever actually make a difference. Unlike math operators, where precedence can result in a different result.
Posted: Thu May 18, 2006 9:41 pm
by s.dot
Although I can't think of an example either, it changes the order in which your code is read (and thus interpreted). I'd like to see an example of this affecting output, if anybody has one.
Posted: Thu May 18, 2006 9:57 pm
by alex.barylski
agtlewis wrote:Well that maybe true, but I really don't care if people think I am a newbie. AND is just easier to read for me.
In that case...dude go for it...but I was under the impression you were interested in hearing others opinions as to what they thought about the use...
I personally think it's inclusion in the language was silly and only included to give new programmers an smaller learning curve...somehow???
I dought that technically there is any advantage to either except AND requires three bytes of storage and && only requires 2...
I suppose using AND has the benefit of preventing accidental & when you really meant && which could lead to difficult to find bugs...
Anyways, the whole point is...do what you feel comfortable doing...

Posted: Thu May 18, 2006 10:21 pm
by feyd
I most often use "and" instead of && because they are lower precedence. Partly because it's easier for others to read and partly because I can bump up the precendence when I need it. I also dislike having to place lots of parens just to make the precendence work right for those occasions where I need to mix them. I've been in far too many situations where I have paren soup to deal with.. not of my own creation, mind you.
Posted: Thu May 18, 2006 10:39 pm
by alex.barylski
feyd wrote:I most often use "and" instead of && because they are lower precedence. Partly because it's easier for others to read and partly because I can bump up the precendence when I need it. I also dislike having to place lots of parens just to make the precendence work right for those occasions where I need to mix them. I've been in far too many situations where I have paren soup to deal with.. not of my own creation, mind you.
Interesting...I had no idea there was a difference in precedence order...
Although I fail to see a practical example...of when one would use AND over &&
Can you demonstrate and explain?
Cheers

Posted: Thu May 18, 2006 11:00 pm
by feyd
Hockey wrote:Can you demonstrate and explain?
It appears I don't have any examples in code I have on hand at the moment so I'll construct one:
Code: Select all
<?php
$second = 'I wasn\'t changed!';
function first() { echo 'first called.' . PHP_EOL; return true; }
function third() { echo 'third called.' . PHP_EOL; return true; }
function fourth() { echo 'fourth called.' . PHP_EOL; return false; }
function fifth() { echo 'fifth called.' . PHP_EOL; return true; }
if (first() and $second = third() && fourth() and fifth())
{
// What's $second?
}
var_dump($second);
?>
Code: Select all
first called.
third called.
fourth called.
bool(false)
As you can see $second is assigned the results of the logical combination of third() and fourth(). Since fourth() returns a false the rest of the if's evaluation is shortcircuited. I try to avoid these sorts of set ups though as they will likely confuse the hell out of a "newb" to the precedence wars; so my code gets a bit longer, but it's more readable for the less experienced (and crazy.)
Posted: Thu May 18, 2006 11:04 pm
by alex.barylski
Hmmmm...
Good enough...thanks man

Posted: Fri May 19, 2006 1:45 am
by jmut
feyd wrote:Hockey wrote:Can you demonstrate and explain?
It appears I don't have any examples in code I have on hand at the moment so I'll construct one:
Code: Select all
<?php
$second = 'I wasn\'t changed!';
function first() { echo 'first called.' . PHP_EOL; return true; }
function third() { echo 'third called.' . PHP_EOL; return true; }
function fourth() { echo 'fourth called.' . PHP_EOL; return false; }
function fifth() { echo 'fifth called.' . PHP_EOL; return true; }
if (first() and $second = third() && fourth() and fifth())
{
// What's $second?
}
var_dump($second);
?>
Code: Select all
first called.
third called.
fourth called.
bool(false)
As you can see $second is assigned the results of the logical combination of third() and fourth(). Since fourth() returns a false the rest of the if's evaluation is shortcircuited. I try to avoid these sorts of set ups though as they will likely confuse the hell out of a "newb" to the precedence wars; so my code gets a bit longer, but it's more readable for the less experienced (and crazy.)
I personally preffer to stay consistant with using && and using brackets to show precedence. It is much easier to read.
Although ofcourse you need to know precedence order to be able to read others code (feyd's)

Posted: Fri May 19, 2006 3:04 am
by santosj
Well from a logical standpoint, I started off using '&&' because I had some C++ background. I found that 'and' is much better at the showing what you mean. I don't think it is n00b, just logical and from the readability standpoint it gets a higher score than '&&'.
From standard practice I always use '(' and ')' when I have two or more conditional statements. I suppose it came also from my C++ background and I don't think it decreases readability, but increases it. Even with 'and', it shows you exactly what is going to be evaluated in groups.