0

I have got this function in php that run and return a number. However what i want is the numbers to be add up each time the function is run.

Here is the code.

 function sumRate(&$numbers) {
    $sumArray="0";
    if($numbers)
$sumArray=$numbers;
            $countedArray=($sumArray+$numbers);             
            echo "<script>console.log('$countedArray')</script>"; 
    }

example when button click Jquery Ajax sent the value to server side.

let say

sumRate("23");//console log 23
sumRate("20");//console log 20

but what I want is that each time the function is run console to log 43 instead of login each number

Weldone in advance

2
  • You want it to track $sumArray between function calls? so that if i were to run sumRate("3") it would log 3, then run sumRate("2") and it would log 5? Commented Jun 23, 2015 at 22:45
  • How did you not get a fatal error when you tried to pass a literal by reference like that? Commented Jun 23, 2015 at 22:50

3 Answers 3

1

I'm not precisely sure of what your goal is, but if you would like to keep track of a running sum, one way is to use globals like so:

$sum = 0;
function sumRate($number) {
    global $sum;
    if($number) {
        $sum += intval($number);       
        echo "<script>console.log('$sum')</script>"; 
    }
}

sumRate("20");
sumRate("23");

Output:

<script>console.log('20')</script>
<script>console.log('43')</script>

Side note:

We cannot pass a value literal by reference. If we call sumRate("23") on a function with signature function sumRate(&$numbers), a fatal error will be thrown. Instead either pass in a variable, or omit the & from the signature.


Update:

On the client side if you would just like the final sum to be echo'd and not each number, then you can do this:

$sum = 0;
function sumRate($number) {
    global $sum;
    if($number) {
        $sum += intval($number);       
    }
}

sumRate("20");
sumRate("23");

echo "<script>console.log('$sum')</script>";

Output:

<script>console.log('43')</script>
Sign up to request clarification or add additional context in comments.

2 Comments

weldone Drake but i want just the whole add up number if is 23 it should log 23 and when any number added let say 20 it log total numbers which should be 43
I've made an update. I hope this is more what you are looking for.
1

You will not see the input be added together because your input only lasts as long as the function call. You need a variable that can store this value, and is not limited to the scope of sumRate

Try

$mySum = 0;

sumRate($mySum, 23);//console log 23
sumRate($mySum, 20);//console log 43


 function sumRate(&$sum, $number) {
    if($number)
        $sum +=$number;
            echo "<script>console.log('$sum')</script>"; 
    }

2 Comments

your code works but still login 23 43 but i want just the 43 so if is 23 let be 23 and when 20 added let it log 43
Yes it will the first time you call sumRate because you are only passing it one number at that point. If you only want to display the total then remove the echo from the sumRate function, and echo $mySum at the end
1

it'simple

    <?php

     function sumRate($numbers) {
            global $countedArray;
$countedArray=$numbers+$countedArray;             
     return $countedArray;
}
   echo sumRate(20);
  echo sumRate(23);

    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.