douxing1353 2012-08-21 22:03
浏览 54
已采纳

将字符串传递给$ _POST

I have several radiobutton arrays. They're named awardLevel0[] , awardLevel1[] , awardLevel2[], and so on, generated dynamically by the user. I want to know if this function will work to get their total sum values.

function awardCheck () {
    $exist = true;
    $num = 0;
    $endsum = 0;
    while ($exist)
    {
        $names = "awardLevel" . $num;
        $awardLev = $_POST[$names];
        if (empty($awardLev)) {
            $exist = false;
            return $endsum;
        }
        else 
            $endsum = $endsum + $awardLev;
        $num++;
    }
    return $endsum;
}

When I run the code above, it gives me an error at $endsum = $endsum + $awardLev; saying i'm using unspported operand types. When I remove $awardLev, it runs without that error.

Is it possible to pass a variable containg a string to $_POST like

$postname = "awardLevel2";
$awardLev = $_POST[$postname];
  • 写回答

3条回答 默认 最新

  • duanliu8998 2012-08-21 22:06
    关注

    Yes you can absolutely do that, but just a note:

    With the [] syntax in the POST values, you get an array back rather than a string. You could use this to your advantage and even array_sum the _POST[awardLevel] values if you just drop the number, but I'm not sure if that would work for you.

    I missed your syntax error (btw post any errors immediately next time).

    Since awardLev is an array, you can only use + with another array, not with an integer. Just use

    $endsum += $awardLev[0]
    

    instead. OR if you are expecting multiple values per awardLev:

    $endsum += array_sum($awardLev);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图