V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
githere
V2EX  ›  PHP

请教一个 PHP 代码出错的原因(一个简单的计算器)

  •  
  •   githere · 2019-02-06 19:39:05 +08:00 · 3487 次点击
    这是一个创建于 1896 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码如下:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>a small calculate</title>
    </head>
    <body>

    <form>

    <input type="text" name="num1" placeholder="number 1">
    <input type="text" name="num2" placeholder="number 2">
    <select name="operator">
    <option>None</option>
    <option>Add</option>
    <option>Subtract</option>
    <option>Multiply</option>
    <option>Divide</option>
    </select>
    <br>
    <button name="submit" value="good submit" type="submit" >Calculate</button>


    </form>

    <p>the answer is:</p>

    <?php

    if(isset($_GET['submit'])){
    $result1 = $_GET('num1');
    $result2 = $_GET('num2');
    $operator = $_GET('operator');

    switch($operator){
    case "None":
    echo "Error,you need select a method.";
    break;

    case "Add":
    echo $result1 + $result2;
    break;

    case "Subtract":
    echo $result1 - $result2;
    break;

    case "Multiply":
    echo $result1 * $result2;
    break;

    case "Divide":
    echo $result1 / $result2;
    break;

    }

    }

    ?>



    </body>
    </html>


    运行后测试:文本框输入值后,点击 calculate 按钮返回错误:


    Fatal error: Function name must be a string in /Applications/XAMPP/xamppfiles/htdocs/calculate.php on line 29

    请问如何解决呢?

    代码片段:

    28 行 if(isset($_GET['submit'])){
    29 行 $result1 = $_GET('num1');
    30 行 $result2 = $_GET('num2');
    31 行 $operator = $_GET('operator');
    10 条回复    2019-02-08 17:54:42 +08:00
    898684526
        1
    898684526  
       2019-02-06 19:45:04 +08:00
    获取 get 参数应该是 [] 而不是()
    caola
        2
    caola  
       2019-02-06 19:45:06 +08:00
    $_GET 获取的数据并不存在, 先判断是否存在: $result1 = $_GET('num1') ?? 0;
    898684526
        3
    898684526  
       2019-02-06 19:45:36 +08:00
    你从 29 行 $_GET('num1') 应该是 $_GET['num1'];
    caola
        4
    caola  
       2019-02-06 19:46:13 +08:00
    确实是方括号问题(上面我直接复制了)
    clf
        5
    clf  
       2019-02-06 19:46:15 +08:00
    $_GET['xxx'] 不是 $_GET('xxx')
    githere
        6
    githere  
    OP
       2019-02-06 19:57:12 +08:00
    @898684526 @caola @lychs1998 感谢各位,我真是眼瞎啊!!
    Hardrain
        7
    Hardrain  
       2019-02-06 20:59:23 +08:00
    var_dump($_GET)可知其为数组,应使用$_GET['parameter']

    $_POST $_SERVER $_COOKIE 等也是数组
    orqzsf1
        8
    orqzsf1  
       2019-02-06 21:02:32 +08:00
    <select>
    <option value ="volvo">Volvo</option>
    <option value ="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
    </select>
    githere
        9
    githere  
    OP
       2019-02-07 14:48:50 +08:00
    @898684526 @caola @lychs1998 追问一下,请问如何理解这一行代码的意思:

    if(isset($_GET['submit'])) {}
    bzj
        10
    bzj  
       2019-02-08 17:54:42 +08:00
    楼主把这里当百度知道了,基础不好不去学习,跑来这里问,习惯不好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3120 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 14:45 · PVG 22:45 · LAX 07:45 · JFK 10:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.