Jump to content

echo and print


vg55

Recommended Posts

I am quite new to php. I do not understand the following behaviour.

The outputs are given in comment.

 

echo '1';          // 1

echo "<br/>"; 

print(2);          // 2

echo "<br/>";

print(2) + 3;    //  5

echo "<br/>";

echo ''.print(2) + 3;  // 51

echo "<br/>";

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

 

Could you please explain it. I could not understand the output in colored lines.

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/167251-echo-and-print/
Share on other sites

Its because print is a language construct and not a function. and the parenthesis arround it arn't required.

 

So its actually reading in the 2 + 3 which is 5. print also always returns 1

 

so for the 51 it first prints out 2+3 which is 5, it then echos out what print returned which is 1. so the final output is 51

 

For the 511, it first prints out the 5, then echos the 1 in the ' marks, it then echos the return of the print, which again is 1. and you have 511

Link to comment
https://forums.phpfreaks.com/topic/167251-echo-and-print/#findComment-881840
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.