newladder Posted June 7, 2007 Share Posted June 7, 2007 Hello, I know this is a wage question, but since iam new to the programming world jsut wanted to clarrify this silly doubts. Why do we need return in the function when we can use echo and get the output ? Can someone explain me more abt funtions ? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/54579-what-is-a-return-in-function/ Share on other sites More sharing options...
taith Posted June 7, 2007 Share Posted June 7, 2007 basically... it a) makes the function end exactly there... and b) makes the entire function worth that value... if you echo in a function it outputs data... if you return from a function... $string=function(); you can use that information... i know others may believe differently... but in my "little" world, functions "should" NEVER echo anything... always return... even if its to "echo function();" Quote Link to comment https://forums.phpfreaks.com/topic/54579-what-is-a-return-in-function/#findComment-269896 Share on other sites More sharing options...
jaikar Posted June 7, 2007 Share Posted June 7, 2007 Hi, i too had the same confusion before, usually function can be called in 2 different ways ( i think ) 1. just call a function in the code like ------- some code ----- //function call, it does something depending on the function myfunction() ; ----- again some code ------- for the above function call, you need not use the return anything, see... you are not assigning any values to a variable, you are just calling a function to so some action there. sample coded for 1 <?php function myfunction() { echo date(time()); } ?> no returns here 2. assign a function output to a variable ------ some code ---- // here you are assigning a output of the function $somevariable = myfunction($parameter); ---- again some code------- for this call you MUST use a return to assing the output to the $somevariable. sample code function myfunction($getvalue) { $val = md5($getvalue); return $val; } when you call like this $somevariable = myfunction('abc'); and if you echo the code, the value will be a encrypted one, md5 is a type of encryption.... try this with return and without return Regards Jaikar Quote Link to comment https://forums.phpfreaks.com/topic/54579-what-is-a-return-in-function/#findComment-269902 Share on other sites More sharing options...
trq Posted June 7, 2007 Share Posted June 7, 2007 i know others may believe differently... but in my "little" world, functions "should" NEVER echo anything... always return... even if its to "echo function();" Agreed. This makes your functions allot more portable. For instance, if you create a function and have it retrieve data from a database then echo html it won't be of any use to you within a cli script. On the other hand, if it simply returns a value, that value can be used on the web, in the cli, where ever. Quote Link to comment https://forums.phpfreaks.com/topic/54579-what-is-a-return-in-function/#findComment-269936 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 As stated many times, do not echo in functions, just causes problems, return a string. The purpose of a return function is say that you need a timestamp from a date, but you do not want to print it out as you need to manipulate it and than you need to enter it into a database. Well if strtotime just printed out the timestamp, and if date just echo'ed the date, this would not be possible. <?php $time = strtotime('07/01/2007'); $new_date = date('Y-m-d', $time); $sql = "INSERT INTO db VALUES ('" . $new_date . "');"; echo 'The new date is ' . $new_date; ?> That way it gives you full control on how you handle the timestamp and the way you handle the date once it is retrieved. If those functions were to "ECHO" or "PRINT" data none of that would be nearly as simple as seen above. Anyhow, hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/54579-what-is-a-return-in-function/#findComment-269964 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.