3

the output of the following code?

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

I tested and the result is 214, but why 214?

if I code:

echo (print '2') + 3; 

the result is 24 Then, echo '1' . '24'; should be 124.

Confused...

4
  • Maybe, if you wrote sensible, maintainable code, you wouldn't have to ask questions like this :-) Commented Aug 30, 2014 at 7:57
  • These kind of questions are actually asked on the Zend PHP exam. Not the most sensible code, but interesting from a more theoretical perspective. Commented Aug 30, 2014 at 8:29
  • @Peter, Yes I saw such questions on the Zend exam, don't know why Zend exam asks such questions, maybe they think it's the PHP language basics? BTW, do you think Zend exam is worthy taking it? Commented Aug 30, 2014 at 10:29
  • @lamplanp Indeed I think they ask these questions because they're about the language basics. I found the exam quite useful, as my knowledge of PHP (basics, security, OOP, other things...) really grew during preparation of the exam. Commented Aug 30, 2014 at 10:47

1 Answer 1

8

When the expression is parsed, the "print" statement is immediately writing its output. So there's the first 2. By definition its return value is 1.

So then the remaining expression is the character 1, followed by the numeric expression 1+3. Therefore 1 and 4.

214

Sign up to request clarification or add additional context in comments.

1 Comment

@lamplanp Please flag the answer as correct if it helped you. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.