Dirty-Rockstar Posted August 16, 2007 Share Posted August 16, 2007 Hi, im new. was referred by a friend. anyway My friend uses echo ' '; i use print " "; Is there a difference? or coder preference Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/ Share on other sites More sharing options...
SirChick Posted August 16, 2007 Share Posted August 16, 2007 i tend to use print when i wana print html to act as html or other codes... and then use echo to just show variables / strings etc Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-325953 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 There is a difference between the two, but speed-wise it should be irrelevant which one you use. print() behaves like a function in that you can do: $ret = print "Hello World";And $ret will be 1That means that print can be used as part of a more complex expression where echo cannot. print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precendence list though. Only "," AND, OR and XOR are lower.echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.If the grammar is: echo expression [, expression[, expression] ... ]Then echo ( expression, expression ) is not valid. ( expression ) reduces to just an expression so this would be valid: echo ("howdy"),("partner");but you would simply write this as: echo "howdy","partner"; if you wanted to use two expression. Putting the brackets in there serves no purpose since there is no operator precendence issue with a single expression like that. Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-325958 Share on other sites More sharing options...
Dirty-Rockstar Posted August 16, 2007 Author Share Posted August 16, 2007 i understand, but for simple db queries and updates using print or echo to display the information does it really matter? i was taught on print, and to avoid echo all the times for what im coding anyway, which is not too complaicated. just queries, updates and inserts into 1 db then displaying that information based on certain variables and results here is an example of a snippit of code from a forum i made $ID=$_GET[iD]; $user1="SELECT * FROM {$db_prefix}question where id='$ID'"; $user2=mysql_query($user1) or die(mysql_error()); $user3=mysql_fetch_array($user2); print "Choose the Topic you want to move this thread to<br> Topic is:<br /> $user3[topic]<br />"; i ended print but in the code it continues. could i use echo to get the same result? Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-325968 Share on other sites More sharing options...
Daniel0 Posted August 16, 2007 Share Posted August 16, 2007 @MadTechie: Good job copying from this page: http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40 which is linked to from http://php.net/print could i use echo to get the same result? Yes. Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-325970 Share on other sites More sharing options...
d22552000 Posted August 16, 2007 Share Posted August 16, 2007 good practice would be this: echo 'Choose the Topic you want to move this thread to<br>Topic is:<br />'.$user3[topic].'<br />'; You don't have to use echo, but look at my formatting. Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-325973 Share on other sites More sharing options...
Daniel0 Posted August 16, 2007 Share Posted August 16, 2007 good practice would be this: echo 'Choose the Topic you want to move this thread to<br>Topic is:<br />'.$user3[topic].'<br />'; You don't have to use echo, but look at my formatting. No it isn't... you are using a constant as the array index. It will work because PHP makes it a string with the value of the constants name if it isn't set. It gives you an E_NOTICE. Do $user3['topic'] instead. Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-325979 Share on other sites More sharing options...
Dirty-Rockstar Posted August 16, 2007 Author Share Posted August 16, 2007 wow, you people must eat php for breakfast. I just started learning and 2 posts in here just made me scratch my head. interesting opinions on print/echo and code formatting tho. Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-325981 Share on other sites More sharing options...
Daniel0 Posted August 16, 2007 Share Posted August 16, 2007 wow, you people must eat php for breakfast. I just started learning and 2 posts in here just made me scratch my head. interesting opinions on print/echo and code formatting tho. What I posted before wasn't an opinion What d22552000 did was incorrect. See: http://php.net/types.array#language.types.array.foo-bar Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-325984 Share on other sites More sharing options...
d22552000 Posted August 16, 2007 Share Posted August 16, 2007 I never use arrays, and would NOT have this problem. I was stating that variables should be seperated from plain text. Quote Link to comment https://forums.phpfreaks.com/topic/65275-solved-echo-or-print/#findComment-326001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.