可选参数 - php中的POST请求

时间:2014-06-23 15:01:14

标签: php optional-parameters

我正在为我的客户开发一个客户端 - 服务器应用程序。我被要求在数据库中添加更多列。我更新了php端点来处理这个问题。我遇到的问题是并非所有客户端应用程序都会立即更新,那么如何才能使附加参数可选?我对php很新,所以如果我马上就不明白,我会道歉。

编辑:事情是数据甚至可能不包含三个可选成员。所以我需要检查该数据是否包含成员devicemake,devicemodel或networktype。

示例代码:

switch(strtolower($_SERVER['REQUEST_METHOD']))
{
    case 'get':
        echo"this is a get request";
        $data = $_GET;
        print_r($data);
        $problem = $data['problem'];
        $environment = $data['environment'];
        $latitude = $data['latitude'];
        $longitude = $data['longitude'];
        $devicemake = ($data['devicemake'] ? $data['devicemake'] : null); // optional
        $devicemodel = ($data['devicemodel'] ? $data['devicemodel'] : null);  //optional
        $networktype = ($data['networktype'] ? $data['networktype'] : null); // optional
        $additionalinfo = $data['additionalinfo'];

        break;
    case 'post':
        echo "this is a post request";
        print_r($HTTP_POST_VARS);
        $postvars = $HTTP_POST_VARS;
        $data = json_decode($postvars);

        $problem = $data->problem;
        $environment = $data->environment;
        $latitude = $data->latitude;
        $longitude = $data->longitude;
        $devicemake = ($data->devicemake ? $data->devicemake : null);  //optional
        $devicemodel = ($data->devicemodel ? $data->devicemodel : null);  //optional
        $networktype = ($data->networktype ? $data->networktype : null);  //optional
        $additionalinfo = $data->additionalinfo;

        break;
}

1 个答案:

答案 0 :(得分:0)

$additionalinfo = isset($data['additionalinfo']) ? $data['additionalinfo'] : "not set";

上述内容基本上是在isset();

上运行'if'语句

isset()正如它所说的那样 - 如果括号中的项目“已设置”或“已初始化”,则返回true / false

"not set"是您未设置的任何内容(if语句的else{}部分)

编辑刚看到可选位对不起 -

    $devicemake = isset($data->devicemake) ? $data->devicemake : null;  //optional
    $devicemodel = isset($data->devicemodel) ? $data->devicemodel : null;  //optional
    $networktype = isset($data->networktype) ? $data->networktype : null; 

我会将其余的解释留在编辑上方,因为我没有看到这一点,它可能对其他人有用。