Re: Re: Why PHP sucks - farce or is there a bit of truth?
| From: | Jim Giner | Date: | Fri, 18 Oct 2013 19:02:20 +0000 |
| Subject: | Re: Re: Why PHP sucks - farce or is there a bit of truth? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322315@lists.php.net to get a copy of this message | ||
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; 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; // 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;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?