Absurdism corner

Why?


  • Total voters
    202

Ok, here is a good one for programmers: Guess what it does print?

PHP:
<!DOCTYPE html>
<html>
<body>

<?php
$arg = 'YES';
$vehicle = ( ( $arg =='YES')? 'train' :
             ( $arg =='NO' )? 'car' :
             ( $arg =='NO' )? 'horse' :
           'feet' );
echo $vehicle;
?>

</body>
</html>

Try it out yourself with an online PHP program:
 
Ok, here is a good one for programmers: Guess what it does print?

PHP:
<!DOCTYPE html>
<html>
<body>

<?php
$arg = 'YES';
$vehicle = ( ( $arg =='YES')? 'train' :
             ( $arg =='NO' )? 'car' :
             ( $arg =='NO' )? 'horse' :
           'feet' );
echo $vehicle;
?>

</body>
</html>

Try it out yourself with an online PHP program:
That's counterintuitive. Though I haven't concerned myself with formal grammar in too long to remember what makes the difference between what php does here and what one (at least I) would expect.
Does someone care to elaborate? Is it the same here as with the dangling else?
 
I think it's because the ternary operator takes two arguments, the first is the negative case, while the postitive case in most cases is the ternary operator on the next line.

To put the qualifying path in pseudocode then:
if "YES" then if not "NO" then if not "NO", horse
 
  • Like
Reactions: rSl
gpv8qgtfxvx61.jpg
 
That's counterintuitive. Though I haven't concerned myself with formal grammar in too long to remember what makes the difference between what php does here and what one (at least I) would expect.
Does someone care to elaborate? Is it the same here as with the dangling else?

PHP does right-side evaluation first, and it has a weird priority on things, so when that rightmost questionmark looks for it's leftside argument, it is greedy and slurps up all the code up to the = mark, like so:

PHP:
$vehicle = true ? 'A' : false ? 'B' : 'C' ;
actually means:
PHP:
$vehicle = (true ? 'A' : false ) ? 'B' : 'C' ;
which is "B".

So the rule of thumb with PHP is: when in doubt and even when not in doubt; add parenthesis. lot's of them.

extra points for @levi for mentioning the ternary operator, but soory, I could not parse your pseudocode in my mind.
 
Last edited:
On a lewder note,

These guys talk about "air action" (erection) and s-pen (ass pen) that is an extension that is introduced and you can use your wand and such:
(note that in order to access the link you need to consent to many things)
 
Last edited:
@levi the ternary operator has three operants. The first is boolean, the second is to be evaluated in case the bool was true, the third in case the bool was false. (My wording might not perfectly align with php terminology/semantics.)

What I mean is, php's parser reads the expression as (I substitute TRUE and FALSE for the comparisons)
( ( TRUE ? 'train' : FALSE ) ? 'car' : FALSE ) ? 'horse' : 'feet'
so what happens is horse because car because train because TRUE.

I would have expected it to read it as
TRUE ? 'train' : ( FALSE ? 'car' : ( FALSE ? 'horse' : 'feet' ) )
so rather train because TRUE.

Which of those readings is to be picked is controlled by how the language's grammar is formulated, but I can't remember how the two alternatives would look in say BNF.
 
Yes, that's why I called the last two operands arguments. The ternary operator has three operands, one condition and two arguments.

I'm not sure BNF would distinguish between the two approaches. The ternary operator would be a type of expression, formulated something like:
TERNARY=conditional? expression: expression

As has been said in this thread before it's more about the order of evaluation, which is not something BNF is concerned with.
 
Back
Top