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,762
  • 8
  • 38
  • 67
Kanishka Panamaldeniya
  • 16,732
  • 29
  • 117
  • 190

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
  • 348,685
  • 41
  • 398
  • 480