Cosizzle Posted August 26, 2008 Share Posted August 26, 2008 Hey, Im playing around with the return method and I'm a little confused. Could someone quickly explain why a return would be used opposed to an echo. Im guessing my confusion might be caused because of the simplicity to my example - a real world situation would help! Thanks Example with return function add($a,$b) { $total = $a + $b; return $total; } echo add(1,1); Output: 2 Example with echo function add($a,$b) { $total = $a + $b; echo $total; } echo add(1,1); Output: 2 Quote Link to comment https://forums.phpfreaks.com/topic/121484-return-question/ Share on other sites More sharing options...
trq Posted August 26, 2008 Share Posted August 26, 2008 Making a function that simply returns a result rather than echoing it is more versatile. For example, you have a function gettitle($id) that returns the name of an article stored in your database. Now, if this function where simply to echo the article title you will have no chance of formatting it. What if you want the article title to appear in all caps? You need to modify the function. If the function simply returns a value, its up to the calling code what to do with that value. Quote Link to comment https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626488 Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 Hey, Im playing around with the return method and I'm a little confused. Could someone quickly explain why a return would be used opposed to an echo. Im guessing my confusion might be caused because of the simplicity to my example - a real world situation would help! Thanks Example with return function add($a,$b) { $total = $a + $b; return $total; } echo add(1,1); Output: 2 Example with echo function add($a,$b) { $total = $a + $b; echo $total; } echo add(1,1); Output: 2 You wouldnt need to echo the function in the second example, it is already echoed within the function so to get the output of 2 it would simple be: add(1,1); Quote Link to comment https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626498 Share on other sites More sharing options...
Cosizzle Posted August 27, 2008 Author Share Posted August 27, 2008 Right... this is on the tip of my tongue and i (think) just need a small nudge but (sorry for the horrible mistakes) is this thinking in the correct direction. Say I had two files each containing classes, one file executed a query the other ran the query and returned the result. Query Class (fileA.php) class query { var $sql = null; function __construct() { $this -> sql = new SQL; } function rQuery() { $this -> sql -> query("SELECT * FROM table"); return $this -> sql -> rResult -> result; } } SQL Class (fileB.php) class SQL { var $result = null; function __construct() { } function rResult($query) { $result = mysql_query($query) or trigger_error("Query: $query <br/>mysql Error: " . mysql_error()); } } Quote Link to comment https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626502 Share on other sites More sharing options...
jordanwb Posted August 27, 2008 Share Posted August 27, 2008 What's the question? Quote Link to comment https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626565 Share on other sites More sharing options...
Cosizzle Posted August 27, 2008 Author Share Posted August 27, 2008 ... My question is whether or not im using the return in the right context in my above example Quote Link to comment https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626612 Share on other sites More sharing options...
jordanwb Posted August 27, 2008 Share Posted August 27, 2008 Well for one thing the code won't even run because you'll get a "function not found" error on this line: $this -> sql -> query("SELECT * FROM table"); The SQL class doesn't have a query function Quote Link to comment https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626817 Share on other sites More sharing options...
sasa Posted August 27, 2008 Share Posted August 27, 2008 try <?php function add_return($a,$b) { $total = $a + $b; return $total; } function add_echo($a,$b) { $total = $a + $b; echo $total; } $a = 3; $b = 5; echo "$a + $b = ". add_return($a, $b). "<hr />\n"; echo "$a + $b = ". add_echo($a, $b). "<hr />\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/121484-return-question/#findComment-626912 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.