vg55 Posted July 24, 2009 Share Posted July 24, 2009 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 More sharing options...
Jibberish Posted July 24, 2009 Share Posted July 24, 2009 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 More sharing options...
haku Posted July 24, 2009 Share Posted July 24, 2009 print also always returns 1 Learn something new on this site all the time. Good little nugget of info there - I don't know how I would ever use it, but I may someday. Link to comment https://forums.phpfreaks.com/topic/167251-echo-and-print/#findComment-881842 Share on other sites More sharing options...
vg55 Posted July 25, 2009 Author Share Posted July 25, 2009 Thanks Jibberish . Link to comment https://forums.phpfreaks.com/topic/167251-echo-and-print/#findComment-882511 Share on other sites More sharing options...
cs.punk Posted July 25, 2009 Share Posted July 25, 2009 Yeah me learned that too right now :-? Link to comment https://forums.phpfreaks.com/topic/167251-echo-and-print/#findComment-882558 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.