Operator Quiz

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

What's the output?

int(1) int(1) int(1) int(1)
2
14%
bool(true) bool(true) bool(true) bool(true)
0
No votes
int(1) bool(true) int(1) bool(true)
7
50%
bool(true) int(1) bool(true) int(1)
5
36%
 
Total votes: 14

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Operator Quiz

Post by Ollie Saunders »

Code: Select all

if ($a = 1 && 1) {
    var_dump($a);
}
if ($a = 1 and 1) {
    var_dump($a);
}

if ($a = 1 || 1) {
    var_dump($a);
}
if ($a = 1 or 1) {
    var_dump($a);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fun with precedence. weeeeee

Image
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yep, don't give the game away now.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I voted int(1) bool(true) int(1) bool(true), number 3, based on something I remember reading in the PHP manual a long time ago.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Few more votes and I will reveal
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

I voted:

bool(true) int(1) bool(true) int(1)

Shouldn't this be the case/output?

cant wait to see the results.

-Matt
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

OK well congratulatous on all those who voted "bool(true) int(1) bool(true) int(1)" you are correct!
I'm surprised nobody voted for either of the first two. Until just now I thought that "and" and "&&" and "or" and "||" were identical. (That was one hell of a sentence).

Anyway here's the explaination.
This behaviour is to do with something called operator presendence. Operator presendence is simply what order things are done in.

"and" has a lower precedence than "=" (assignment)
while "&&" has a higher precedence than "=".

This means that when appearing in a single expression the opperands on either side of an "&&" operator are evaluated using that operator before the opperands on either side of an "=" operator are. With "and" the opposite is true.

To make this clearer, these are functionally equivalent i.e. the bracket have no effect:

Code: Select all

$result = $a = (1 && 1)
$result = $a =  1 && 1
and

Code: Select all

$result = ($a = 1) and 1
$result =  $a = 1  and 1
$result is always true.
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

Yay for me!! (and of course the rest of 3 people who got it right) jk... jk

nice one ole!

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

Post by Benjamin »

I knew it was the third or fourth option because I remember reading that the precendence was different. I just guessed though.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ha! I totally cheated and waited for the answer. Well not really... I picked this thread after the answer was revealed. But I got it right! Yay!
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

And I was thinking 1 && 1 = 2 8O :? :wink:
pureDesi
Forum Newbie
Posts: 10
Joined: Thu Aug 10, 2006 11:46 am

Post by pureDesi »

matthijs wrote:And I was thinking 1 && 1 = 2 8O :? :wink:
lol, I so wish it were that simple :/
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You'd think 1 && 1 would = 3. 1 and and 1 = 3.
Post Reply