0

What is the output of the following code?

echo '1' . (print '2') + 3;

My answer was 15, but the answer is 214.

Why?

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

1 Answers1

2

As executed, the code will do:

print '2'  -> outputs 2
... print ALWAYS has a return value of 1, so the code becomes

echo '1' . (1 + 3);  // with output '2'

This is simplified to

echo '1' . 4; // with output '2'
echo '14'; // output 2

final output: 214.

Marc B
  • 356,200
  • 43
  • 426
  • 500