Can anybody tell the internal procedure of the below expression?
<?php echo '2' . print(2) + 3; ?>
// outputs 521
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Echo a concatenated string composed of:
The string ‘2’
The result of the function print(‘2’), which will return true, which gets stringified to 1
The string ‘3’
Now, the order of operations is really funny here, that can’t end up with 521 at all! Let’s try a variant to figure out what’s going wrong.
echo ‘2’.print(2) + 3;
This yields 521
PHP is parsing that, then, as:
echo ‘2’ . (print(‘2’) + ‘3’))
Bingo! The print on the left get evaluated first, printing ‘5’, which leaves us
echo ‘1’ . print(‘2′)
Then the left print gets evaluated, so we’ve now printed ’52’, leaving us with
echo ‘1’ . ‘1’ ;
Success. 521.
I would highly suggest not echoing the result of a print, nor printing the results of an echo. Doing so is highly nonsensical to begin with.