Page 1 of 1
[Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 9:04 am
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 ';

Re: [Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 10:19 am
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

Re: [Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 10:33 am
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

Re: [Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 11:56 am
by Eran
I think it would come out
Re: [Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 12:19 pm
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 ';

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
Re: [Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 12:27 pm
by AbraCadaver
pytrin wrote:I think it would come out
Only for this:
Code: Select all
echo ($v = foo($v) || bar($v)) ? 'true ' : 'false ';
Re: [Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 12:35 pm
by pickle
If it can't be evaluated, it's unknowable.
Re: [Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 12:37 pm
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.

Or was that your point?

Re: [Challenge] "Basic" conditioning
Posted: Tue Mar 02, 2010 2:02 pm
by VladSun
pickle wrote:If it can't be evaluated, it's unknowable.
That shouldn't be an excuse for you

Re: [Challenge] "Basic" conditioning
Posted: Wed Mar 03, 2010 4:36 am
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

Re: [Challenge] "Basic" conditioning
Posted: Wed Mar 03, 2010 11:39 am
by VladSun
@Weiry
There are no string comparisons in this code - only strict boolean.
Re: [Challenge] "Basic" conditioning
Posted: Sun Mar 07, 2010 4:03 pm
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

)
That's why I decided to examine your PHP knowledge
So... to clear things out:
evaluates to:

Re: [Challenge] "Basic" conditioning
Posted: Sun Mar 07, 2010 4:10 pm
by Eran
what I'm not clear on, is
Does the value of $v change before it's passed on to bar() or not? and what's the final answer?
Re: [Challenge] "Basic" conditioning
Posted: Sun Mar 07, 2010 4:24 pm
by VladSun
=>
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.