0

I know about type juggling in PHP, but I don't quite understand this logic. I'd expect 'foo2bar' as output, but the below one gives '1bar' as output.

<?php
    $x = 1;
    echo 'foo' . $x + 1 . 'bar'; // echoes '1bar'
?>

Where does the 'foo' go? Can someone explain this?

Sai Kiran Sripada
  • 1,149
  • 3
  • 15
  • 30
  • 1
    In other words: `echo ((('foo' . $x) + 1) . 'bar');` – Rizier123 Mar 29 '15 at 14:04
  • So, the concatenation operator evaluates first since it is left associative. ((('foo' . $x) + 1) . 'bar') becomes (('foo1' + 1) . 'bar'), because of type-juggling it evaluates to (1 . 'bar') and echoes '1bar'. – Sai Kiran Sripada Mar 30 '15 at 05:40
  • 1
    1. Both have the same precedence 2. Yes because of the left associative 3. yes at the end it's 1 and "bar" – Rizier123 Mar 30 '15 at 09:15

0 Answers0