brown2005 Posted May 8, 2007 Share Posted May 8, 2007 i have a function, but what I want to know is when should i use return? and when should i use echo? thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/ Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Wheres the function. It is all dependent on what you require. Generally if it is a calculation being done a return is good to do. But if it is a string that all you would do after it is returned is print it out than just print it out in the function. You can also print something and return something say this: <?php function returnAndPrint() { print "This function ran!"; return true; } $pass = returnAndPrint(); if ($pass) { print "The function printed and returned a value! SWEET!"; } ?> All in all it is up to you to decide what you need. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248462 Share on other sites More sharing options...
warewolfe Posted May 8, 2007 Share Posted May 8, 2007 it depends on what the function is suppose to do and how complex a system the function is a part of. If the function is simple and its purpose is to generate atomic html then use echo. ie, if you want to print "hello world" then use echo. If the function is part of more complex system like database systems, Object oriented, and/or implementing the MVC (Model View Controller) architectural pattern then use returns. ie, complexSQLQuery( param1, param2,param3,param 4) { //complext code here return answer } Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248473 Share on other sites More sharing options...
otuatail Posted May 8, 2007 Share Posted May 8, 2007 This might be helpfull on this topic. I used echo instead of print. any difference? Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248475 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 No difference at all really. Echo acts like a function in that it takes parameters. Print has a return value of 1 if succesful. As far as performance it does not matter. Which ever you prefer. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248481 Share on other sites More sharing options...
Barand Posted May 8, 2007 Share Posted May 8, 2007 echo is more flexible eg you can do this <?php $ar = array (1,2,3); echo '<pre>', print_r($ar, true), '</pre>'; ?> but you can't do <?php $ar = array (1,2,3); print '<pre>', print_r($ar, true), '</pre>'; ?> Also, echo is shorter to type. I often use echo for output that should be in the script and print for outputting debug values. That way it's easy, when deploying, to search for "print" to remove code that shouldn't be there. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248513 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 echo is more flexible eg you can do this <?php $ar = array (1,2,3); echo '<pre>', print_r($ar, true), '</pre>'; ?> but you can't do <?php $ar = array (1,2,3); print '<pre>', print_r($ar, true), '</pre>'; ?> Also, echo is shorter to type. I often use echo for output that should be in the script and print for outputting debug values. That way it's easy, when deploying, to search for "print" to remove code that shouldn't be there. I like the debugging idea. I always just used print for no reason really. It just works easier for me, but as far as that second code is concerned, no you cannot pass the data like parameters to print but this would display the same thing =) <?php $ar = array (1,2,3); print '<pre>' . print_r($ar, true) . '</pre>'; ?> I do understand the differences in concatanation(sp?) and passing with arguments. Just stating that. But I may take the advice and use one for strictly debugging and the other for general use. It beats the coments //REMOVE BEFORE PRODUCTION I generally use =) Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248525 Share on other sites More sharing options...
Trium918 Posted May 8, 2007 Share Posted May 8, 2007 It seems like Barand and froat110 are competing against one another. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248534 Share on other sites More sharing options...
corbin Posted May 8, 2007 Share Posted May 8, 2007 Little off topic, but I like to use something like //START DEBUG and then //END DEBUG if I have a long block of debug stuff, or if I'm OCD about a script cause then I can go back in a text editor or a different php script and just do a regexp replace to remove it all ;p. Corbin->GetOnTopic(); I like to use return because if you're using echo/print and you decide you need to use the value later instead of printing it there, it can be a pain.... Although sometimes it is a pain to echo out every return from a function, I like it, and really it's only just like 10 more characters ;p. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248537 Share on other sites More sharing options...
Barand Posted May 8, 2007 Share Posted May 8, 2007 I also use print_r() extensively when debugging, so the single search for "print" finds all the debug output. That, flexibility, and 20% saving in typing, are my excuses for doing it that way round Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248544 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Not at all, just being informative. This topic was already discussed thoroughly with a few members on the forum found here: http://www.phpfreaks.com/forums/index.php/topic,132729.msg558095.html#msg558095 That might help explain some of the differences between echo and print. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248545 Share on other sites More sharing options...
corbin Posted May 8, 2007 Share Posted May 8, 2007 I love to ghetto print_r DB results when I'm coding something DB driven.... My bad memory can never remember column names lol. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248548 Share on other sites More sharing options...
Nameless12 Posted May 8, 2007 Share Posted May 8, 2007 use return for all functions unless you are creating a function to be used for debugging (example, var_dump, print_r). Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248551 Share on other sites More sharing options...
Barand Posted May 8, 2007 Share Posted May 8, 2007 In many languages you have procedures (which do something) and functions (which do something then return a value). In PHP you don't have that distinction, just functions. I cannot subscribe the purist view, therefore, that a function must always return something. There's nothing wrong with the "C" void type function, returning no value. In many instances you want to repeat a set of statements at different points in the program, so make it a "procedure" (ie void function). Even if it is called only once, such a procedure can help break up the logic of a program into smaller, manageable chunks. eg switch ($var) { case 'x' : do_something(); break; case 'y': do_something_else(); break } Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248564 Share on other sites More sharing options...
Nameless12 Posted May 9, 2007 Share Posted May 9, 2007 you took what i said to way to literally. I just meant all functions output should be a return value as opposed to echo. I never ever meant functions need to have output, but if they do it should a be a return value unless it is something like var_dump or print_r. The reasons I say what I say is not because of personal preference, something like this is not personal preference if you continue to echo stuff you will find you have way less control over your programs output and depending on what you are doing it will come back to bite you. What I have said is not personal preference, people who use echo all the time will have major design issues and the only way they will be able to work around them will be to use output buffering literally everywhere. This is why I think it is bad to use echo's everywhere. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-248692 Share on other sites More sharing options...
per1os Posted May 9, 2007 Share Posted May 9, 2007 I understand what nameless is saying and looking back at my code I never echo anything via a function anymore. It is always returned as an array or object or string to pass to my "templating" function which than uses that. I would have to agree with that statement Nameless said. It gives you full control of your output =) Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-249036 Share on other sites More sharing options...
warewolfe Posted May 10, 2007 Share Posted May 10, 2007 Self deleted. Orginally written in coffee deprived state. Quote Link to comment https://forums.phpfreaks.com/topic/50555-return-or-echo-in-function/#findComment-249433 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.