添加了阵列上按一下按钮PHP脚本 [英] Add up array on button click php script

查看:112
本文介绍了添加了阵列上按一下按钮PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在运行,并返回一个数字的PHP这个功能。但是我想是的数字是每个功能运行时增加。

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.

下面是code。

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

例如,当点击链接jQuery的阿贾克斯发送的值服务器端。

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

让说

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

但我想的是,每个函数运行控制台时登录43,而不是登录每个号码

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

Weldone提前

推荐答案

我不是precisely确保你的目标是什么,但如果你想保持运行总和的轨道,一种方法是使用全局像这样:

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");

输出:

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

边注:

我们无法通过引用传递文字的值。如果我们调用 sumRate(23)与签名函数sumRate功能(安培; $号),一致命错误将被抛出。相反,无论是传递一个变量,或省略&放大器;从签名

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.

更新:

在客户端,如果你只是想让最后一笔被回显的,而不是每个数字,那么你可以这样做:

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>";

输出:

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

这篇关于添加了阵列上按一下按钮PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆