doudi8525 2013-11-30 14:53
浏览 43
已采纳

如何在数学计算中使用PHP POST变量?

first time asking on the site so sorry if the question isnt too detailed.

My code:

<?php
  $a = ($_POST['a']);

  $b = ($_POST['b']);

  $op = ($_POST['c']);

  $ans = $a   $op   $b;

  echo $ans;
?>

As you can see I'm passing in numbers $a and $b and want to do something with them depending on the $op which gets passed through.

My problem is that I cant get at the variable $op in the way I want to. Ideally if $op is "+" id like the $ans to be something like 1 + 2.

I'm guessing that I have a problem with syntax.

Many thanks

David

  • 写回答

2条回答 默认 最新

  • douhan1860 2013-11-30 14:57
    关注

    You can use the switch statement:

    switch ($op) {
        case '+':
            $ans = $a + $b;
            break;
        case '-':
            $ans = $a - $b;
            break;
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现