spires Posted June 24, 2009 Share Posted June 24, 2009 Hi guys. I'm trying to do a simple function, but for some reason the value won't return? I can echo the value out, but not return the value. What am I doing wrong? <?PHP include("../dbconnect.php"); $usName = $_SESSION["usName"]; function sevenDays($day, $usName){ $notes_sel_sql = "SELECT * FROM csv_notes WHERE notes_user='$usName' && notes_status2='Awaiting proofs' && notes_date2 >= DATE_SUB(CURDATE(), INTERVAL $day DAY)"; $notes_sel_result = mysql_query($notes_sel_sql) or die ("query 2 failed".mysql_error()); $notes_sel_count = mysql_num_rows($notes_sel_result); $arrGP = array(); for ($i=0;$i<$notes_sel_count;$i++){ $notes_sel_row = mysql_fetch_array($notes_sel_result); $totalGP = $notes_sel_row['notes_Totalgp']; $arrGP[] = $totalGP; } $totalSum = array_sum($arrGP); return $totalSum; } $days = '7'; sevenDays($days, $usName); ?> Like I said, if I do 'echo $totalSum; instead of return $totalSum;' then it works fine. Also, why should I use return over echo? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/163521-function-return-help-needed/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 24, 2009 Share Posted June 24, 2009 The value returned by a function must be used somehow. Assign it to a variable, echo it, us it in a conditional test... - $result = sevenDays($days, $usName); echo sevenDays($days, $usName); if(sevenDays($days, $usName) == 123){...} Also, why should I use return over echo?So that you can write the function for general purpose use and you can use the value anyway you want in the application. If you write it to echo the output, that is all the function can be used for (which is fine for a function who's purpose is to output things.) But by just returning the value, you can keep reusing that same function in many different situations without needing to modify it or create multiple versions of it to do different things with the results. Quote Link to comment https://forums.phpfreaks.com/topic/163521-function-return-help-needed/#findComment-862780 Share on other sites More sharing options...
KevinM1 Posted June 24, 2009 Share Posted June 24, 2009 Returning a value is far different than echoing a value. When you echo a value, it's printed to the screen immediately. When you return a value, you're telling the script that you want to return that value to the scope that contains the function, normally to assign it to a variable. So, something like: $result = sevenDays($days, $usNames); Would store the result of the function in the variable $result. Your current issue is that your function is returning a value, but it's being discarded immediately because you don't store it anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/163521-function-return-help-needed/#findComment-862781 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.