Audittxl Posted July 21, 2013 Share Posted July 21, 2013 I have been always questioning myself which should i use return or echo inside a fuction can somone clear this up for me . Thanks in advance. Audittxl Quote Link to comment https://forums.phpfreaks.com/topic/280371-inside-a-function-return-vs-echo/ Share on other sites More sharing options...
PaulRyan Posted July 21, 2013 Share Posted July 21, 2013 Always use return. Using echo will echo out the content where ever the function is called, so if you called the function before the header etc, it would display whatever you echo above the header. Using return, you assign the function call to a variable for use later on in the code. Quote Link to comment https://forums.phpfreaks.com/topic/280371-inside-a-function-return-vs-echo/#findComment-1441580 Share on other sites More sharing options...
Irate Posted July 21, 2013 Share Posted July 21, 2013 return terminates a function block, echo just echoes it and continues function execution, so it will eventually return undefined. Which is mostly not what you want. If you want functions to hold a variable throughout your program, always use return. Quote Link to comment https://forums.phpfreaks.com/topic/280371-inside-a-function-return-vs-echo/#findComment-1441587 Share on other sites More sharing options...
davidannis Posted July 21, 2013 Share Posted July 21, 2013 If you are worried about using return because it stops the execution of the function and you want to output multiple things to the browser, either concatenate the strings or put them in an array and use return at the end of the function then print them at the appropriate point in the program. Quote Link to comment https://forums.phpfreaks.com/topic/280371-inside-a-function-return-vs-echo/#findComment-1441593 Share on other sites More sharing options...
KevinM1 Posted July 21, 2013 Share Posted July 21, 2013 Return and echo do two different things: Echo outputs directly to the browser. Return stops function execution and sends a value back to the function's invocation context. Example: function example($x) { return $x * $x; } $val = example(5); // $val now contains the value 25 (5 * 5) Since both echo and return do different things, using them correctly merely depends on what you're trying to do at that moment. Need to display data? Use echo. Need to use the value created after a function processes some data? Use return. That said, you'll likely return far more than echo. Functions are supposed to be black boxes - you pass data in, the function works on it, and ultimately gives back a new value (or set of values). Echoing generally is better used in your main script execution rather than functions. Quote Link to comment https://forums.phpfreaks.com/topic/280371-inside-a-function-return-vs-echo/#findComment-1441595 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.