0

I have folowing string:

$html .= '<img src="' . $url . '" alt="' . trim($alt) ? $alt : $this->escape($name) . '" />';

Which looks ok from first glance but it will append empty string to $html which took some head scratching..

But if I add parenthesis around ternary operator it returns expected result

$html .= '<img src="' . $url . '" alt="' . (trim($alt) ? $alt : $this->escape($name)) . '" />';

My question how does parser parse this expresion that it's possible to get nothing appended to the result?

insanebits
  • 818
  • 1
  • 6
  • 24
  • If it cuts off as `''` then you won't see it in the browser unless you view source because its not valid html. – developerwjk Mar 16 '15 at 20:41
  • @developerwjk I had this code inside anchor tag and it generated empty anchor when checked source which seemed weird – insanebits Mar 16 '15 at 20:42
  • @developerwjk main question is how does php parse this kind of expression, shouldn't it first check for ternary operation and only then join strings – insanebits Mar 16 '15 at 20:44
  • I'm surprised PHP wouldn't just fail and give you a blank screen (or stop outputting altogether at this point) because I think Java would fail to compile if you tried this and left the parenthesis off. – developerwjk Mar 16 '15 at 20:50

0 Answers0