[Challenge] "Basic" conditioning

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
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

[Challenge] "Basic" conditioning

Post by VladSun »

Tell what's the output of this code snippet without evaluating it.

Code: Select all

function foo($v)
{
    echo $v ? 'true ' : 'false ';
    return !$v;
}
 
function bar($v)
{
    echo !$v ? 'true ' : 'false ';
    return $v;
}
 
$v = true;
echo ($v = foo($v) or bar($v)) ?  'true ' : 'false ';
:twisted: :twisted: :twisted:
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: [Challenge] "Basic" conditioning

Post by timWebUK »

Had to look up what ? : syntax was but I figured it to be (in white text so no spoiler):
The Answer wrote:foo - true
bar - true
end - false

So, true true false.
Ran it afterwards to check, got it right :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] "Basic" conditioning

Post by VladSun »

:) I like the white foreground/background colored answer :)

Though I'm not sure why you've decided the output of bar() ...
We'll have a discussion on it latter :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge] "Basic" conditioning

Post by Eran »

I think it would come out

Code: Select all

true false true
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Challenge] "Basic" conditioning

Post by AbraCadaver »

VladSun wrote:Tell what's the output of this code snippet without evaluating it.

Code: Select all

function foo($v)
{
    echo $v ? 'true ' : 'false ';
    return !$v;
}
 
function bar($v)
{
    echo !$v ? 'true ' : 'false ';
    return $v;
}
 
$v = true;
echo ($v = foo($v) or bar($v)) ?  'true ' : 'false ';
:twisted: :twisted: :twisted:
foo() is called with true and echos true but returns !true which is false
so bar() is evaluated and passed false (returned from foo()) and echos !false which is true but returns false
the original echo expression evaluates to false so false is echoed
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Challenge] "Basic" conditioning

Post by AbraCadaver »

pytrin wrote:I think it would come out

Code: Select all

true false true
Only for this:

Code: Select all

echo ($v = foo($v) || bar($v)) ?  'true ' : 'false ';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [Challenge] "Basic" conditioning

Post by pickle »

If it can't be evaluated, it's unknowable.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [Challenge] "Basic" conditioning

Post by AbraCadaver »

pickle wrote:If it can't be evaluated, it's unknowable.
Well, I didn't think that meant I couldn't evaluate it in my brain. :P Or was that your point? :o
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] "Basic" conditioning

Post by VladSun »

pickle wrote:If it can't be evaluated, it's unknowable.
That shouldn't be an excuse for you :P
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: [Challenge] "Basic" conditioning

Post by Weiry »

if true = "true" or "true", the answer must be false. (true true false)
foo(true) matches false, so returns true
bar(true) does not match, so returns true
so if the variable $v does not match either, then the answer must be false
Edit: realized the mistake. Was a late night :banghead:
Last edited by Weiry on Wed Mar 03, 2010 7:14 pm, edited 2 times in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] "Basic" conditioning

Post by VladSun »

@Weiry

There are no string comparisons in this code - only strict boolean.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] "Basic" conditioning

Post by VladSun »

OK, I see there are no posts for the last 5 days, so I will consider this "challenge" closed.

If you take a look at the AND/OR PHP manual page you'll see that:
// "||" has a greater precedence than "or"
...
// "&&" has a greater precedence than "and"
...
while it's true the manual page doesn't explicitly explains that AND/OR operators have lower precedence than the assignment operator "=". It's clearly explained in the Operator Precedence, but it's not so in the Logical Operators page (not to mention the Bulgarian version of Logical operators page, where some very important notices are missing :crazy: )

That's why I decided to examine your PHP knowledge ;)

So... to clear things out:

Code: Select all

$h = true and false;
evaluates to:

Code: Select all

(($h = true) and false)
;)
Last edited by VladSun on Sun Mar 07, 2010 4:39 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge] "Basic" conditioning

Post by Eran »

what I'm not clear on, is

Code: Select all

($v = foo($v) or bar($v))
Does the value of $v change before it's passed on to bar() or not? and what's the final answer?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] "Basic" conditioning

Post by VladSun »

Code: Select all

($v = foo($v) or bar($v))
=>

Code: Select all

$v =
(
  (
     $v = foo($v)
   ) 
   or 
   bar($v)
)
So, first foo($v) is executed, its return value is assigned to $v and then bar($v) is executed with the new value of $v, and finally the OR operator is evaluated and its return value is again assigned to $v.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply