PHP-Isset()函数,签入参数

PHP-Isset()函数,签入参数,php,isset,Php,Isset,我需要帮助找出isset函数的正确PHP编码 我无法准确理解我需要如何使用isset()函数。下面是分配说明,我已经完成了b部分,使用isset()函数检查查询字符串中是否存在参数。当我构建表单时,表单操作是“GET”,因为它需要在查询字符串中,这也正确吗 我仅有的两个变量是用户输入的Value1和Value2 SimpleCalculator.php-编写一个脚本,从查询字符串中检索两个值,将它们相加并显示结果 步骤: 从HelloForm.php复制代码并修改它以显示两个文本框。将输入的值分

我需要帮助找出isset函数的正确PHP编码

我无法准确理解我需要如何使用
isset()
函数。下面是分配说明,我已经完成了b部分,使用
isset()
函数检查查询字符串中是否存在参数。当我构建表单时,表单操作是“GET”,因为它需要在查询字符串中,这也正确吗

我仅有的两个变量是用户输入的
Value1
Value2

SimpleCalculator.php-编写一个脚本,从查询字符串中检索两个值,将它们相加并显示结果

步骤:

  • 从HelloForm.php复制代码并修改它以显示两个文本框。将输入的值分配给名为
    value1
    value2
    的变量

  • 在PHP代码中,将查询字符串中的参数分配给名为
    $value1
    $value2
    的局部变量。您需要使用
    isset()
    函数来检查这两个参数是否在查询字符串中

  • 将变量回显到浏览器,以确保已正确检索它们

  • 使用语句添加并打印值:

    echo“总和为:”。($Value1+$Value2)

  • 这就是我到目前为止所做的:

    <html>
        <head>
            <title>Sample web form</title>
            <link href="/sandvig/mis314/assignments/style.css" rel="stylesheet" type="text/css">
        </head>
    
    <body>
    <div class="pageContainer centerText">
    
    <h2>Simple Calculations </h2>
    <hr />
    
    <form method="get" >
        Please enter two numbers:<br><br>
        Value 1: <input type="text" name="Value1" autofocus> <br><br>
        Value 2: <input type="text" name="Value2" autofocus> <br><br>
        <input type="submit" value="Add">
     </form>
    
     <?php
    
     $value1 = $_GET ['Value1'];
     $value2 = $_GET ['Value2'];
    
     //Retrieve name from querystring. Check that parameter
     //is in querystring or may get "Undefined index" error
    
     echo 'Value 1 was:' . ($value1);
     echo 'Value 2 was:' . ($value2);
     echo 'Sum is: ' . ($value1 + $Value2);
    
     ?>
    </div>
    </body>
    </html>
    
    
    web表单示例
    简单计算
    
    请输入两个数字:

    值1:

    值2:


    它没有说明如果未设置值该怎么办,但是这是获取值的最佳方法。如果未设置$value1或$value2,则可能会显示错误。在下面的示例中,它们将为null

    $value1 = isset($_GET['Value1']) ? $_GET['Value1'] : null;
    $value2 = isset($_GET['Value2']) ? $_GET['Value2'] : null;
    

    
    
    首先,检查
    isset()函数

    它是如何工作的

    $value1 = isset($_GET ['Value1']) ? $_GET ['Value1'] : null;
    $value2 = isset($_GET ['Value2']) ? $_GET ['Value2'] : null;
    
    现在,如果用户在查询字符串中以
    Value1
    参数的形式发送内容,那么
    $Value1
    变量将被分配该数据;如果没有
    null,则将分配
    。与
    value2
    相同


    简单:)

    考虑一下我的get()函数的这个例子:

    <?php
    
    $arr = [];
    
    var_dump( get($arr,'a','b') ); // NULL
    
    $arr['a']['b'] = 'ab';
    var_dump( get($arr,'a','b') ); // 'ab'
    
    //var_dump(get($arr,'aa'));
    
    /*
        Get with safety
        @author: boctulus
    
        @param array 
        @param index1
        @param index2
        ..
    */
    function get(){ 
        $numargs  = func_num_args();
        $arg_list = func_get_args();
    
        $v = $arg_list[0];  
    
        for ($i = 1; $i < $numargs; $i++) 
        {
                if (isset($v[$arg_list[$i]]))
                $v = $v[$arg_list[$i]];
            else
                return null;
        }
    
        return $v;
    }
    

    如果
    $value1
    $value2
    为空,则您的总数不正确:
    “+”
    。嗯?
    <?php
    
    $arr = [];
    
    var_dump( get($arr,'a','b') ); // NULL
    
    $arr['a']['b'] = 'ab';
    var_dump( get($arr,'a','b') ); // 'ab'
    
    //var_dump(get($arr,'aa'));
    
    /*
        Get with safety
        @author: boctulus
    
        @param array 
        @param index1
        @param index2
        ..
    */
    function get(){ 
        $numargs  = func_num_args();
        $arg_list = func_get_args();
    
        $v = $arg_list[0];  
    
        for ($i = 1; $i < $numargs; $i++) 
        {
                if (isset($v[$arg_list[$i]]))
                $v = $v[$arg_list[$i]];
            else
                return null;
        }
    
        return $v;
    }