Re: Re: Why PHP sucks - farce or is there a bit of truth?

From: Date: Fri, 18 Oct 2013 19:18:10 +0000
Subject: Re: Re: Why PHP sucks - farce or is there a bit of truth?
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-322316@lists.php.net to get a copy of this message
On 10/18/2013 3:02 PM, Jim Giner wrote:
On 10/18/2013 10:33 AM, Robert Cummings wrote:
Hi Jim, I'm not sure it's a bug. There are two considerations... should the first use of the ternary operator feed the first operand of the second, or should the second use of the ternary operator feed the 3rd operand of the first use. Visually... which is more correct: <?php
     $foo = ($a ? $b : $c) ? $d : $e;  // PHP does this one.
     $foo = $a ? $b : ($c ? $d : $e);
?> They both seem perfectly reasonable and so I would say it's my job to clarify if I don't want to leave it the the vagaries of the developer's subjective opinion :) To me this isn't a flaw in PHP, but an oversight by the programmer (because they don't see the ambiguity) or they have an incorrect assumption that one way is the right way. Either way... do you think the following makes for readable code? <?php
     $foo = $a ? $b : $c ? $d : $e;
?> Cheers, Rob.
When I write something like this, I expect it to happen from left to right. Always have, always will. That said I see the 'proper' expectation of this statement: $foo = $a ? $b : $c ? $d : $e; as: $foo will be the result of "if $a then $b else if $c then $d else $e;" Why php interprets it differently is just not logical to me. Of course writing something like that with one set of parens solves the riddle as well as making it more readable: $foo = $a ? $b : ($c ? $d : $e); As for ambiguity - I don't see it. Again the literal syntax of the ternary if is (IMHO): "if (cond) then (statement) else (other statment)" In your example:
       $foo = $a ? $b : $c ? $d : $e;
I see it exactly as this: "if $a is true then $b else
     if $c is true then
     $d
     else
     $e;"
Why should php evaluate the last statement before deciding if it even needs that statement? And after posting the above and re-reading it several times I can now see the 'other' interpretation of this puzzle.
Syntactically the ternary operator is recognized by the ? - no? Therefore the compiler sees that and splits the statement into the three parts: condition, true result & false result. Call this picture #1. But in this situation the compiler runs into a second ? and has to process it a bit differently. The parts of the concatenation will be condition (picture #1), true result, & false result. I guess it's a case of that old expression "the emphasis is on the wrong syl-lable". So - while readers of English see it one way, that little old automaton sees it differently and quite accurately it would seem. I guess the writers of that compiler never thought about the future confusion it would raise. Kind of like what I previously mentioned in my earlier post about less than consistent syntax. This behavior surely is not the same as the interpretation of a regular old if/then/else with concatenated if/then/else pieces added in. Would you agree?

« previous php.general (#322316) next »