&& || AND && WHY

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

&& || AND && WHY

Post 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))
Last edited by Benjamin on Thu May 18, 2006 9:35 pm, edited 1 time in total.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: && || AND && WHY

Post 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 :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Last edited by s.dot on Thu May 18, 2006 9:43 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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... :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Hmmmm...

Good enough...thanks man :)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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) :)
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post 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.
Post Reply