Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/163521-function-return-help-needed/
Share on other sites

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.

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.