Reputation: 784
I'm working on a client-server app for a client of mine. I was asked to add a few more columns to the db. I'm updated the php endpoint to handle this. The issue I am running into is that not all client apps will be updated right away, so how can I make the additional parameters optional? I'm quite new to php, so my apologies if I don't understand right away.
EDIT: the thing is data might not even contain the three optional members. So I need to check whether or not that data has the members devicemake, devicemodel, or networktype.
Sample code:
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;
}
Upvotes: 1
Views: 1856
Reputation: 24825
$additionalinfo = isset($data['additionalinfo']) ? $data['additionalinfo'] : "not set";
What the above does is basically run an 'if' statement on isset();
isset()
does exactly what it says - returns true / false for if the item in the parenthesis 'is set' or 'has been initialised'
The "not set"
is whatever you want to do if it isn't set (the else{}
part of an if statement)
edit just saw the optional bits sorry -
$devicemake = isset($data->devicemake) ? $data->devicemake : null; //optional
$devicemodel = isset($data->devicemodel) ? $data->devicemodel : null; //optional
$networktype = isset($data->networktype) ? $data->networktype : null;
I will leave the rest of the explanation above the edit as I hadn't seen that bit and it may be useful for someone else.
Upvotes: 1