Why use Ternary Operator in http_build_query

I am reading the book, PHP MASTER: WRITE CUTTING-EDGE CODE. While I am reading Chapter 3 APIs, I got a question about one line of code on page 112.

The code is on how to make Cross-domain Requests through a proxy script.

Approximately at the middle of the code, one line is as follows.

$url .= '?' .http_build_query($_GET +($allowed_hosts[$requested_host]['args']) ?: array());

I am curious why the Ternary Operator is used here, why the expression for the TRUE condition is empty, and what the purpose of using an empty array() for the FALSE condition. I would highly appreciate any help on these questions.

The entire code of the proxy script is copied below.

// An array of allowed hosts with their HTTP protocol (i.e. httpor https) and returned mimetype
$allowed_hosts = array(
'api.bit.ly' => array(
"protocol" => "http",
"mimetype" => "application/json",
"args" => array(
APIs 111
"login" => "user",
"apiKey" => "secret",
)
)
);
// Check if the requested host is allowed, PATH_INFO starts with a /
$requested_host = parse_url("http:/" .$_SERVER['PATH_INFO'],PHP_URL_HOST);
if (!isset($allowed_hosts[$requested_host])) {
// Send a 403 Forbidden HTTP status code and exit
header("Status: 403 Forbidden");
exit;
}
// Create the final URL
$url = $allowed_hosts[$requested_host]['protocol'] . ':/' .$_SERVER['PATH_INFO'];
if (!empty($_SERVER['QUERY_STRING'])) {
// Construct the GET args from those passed in and the default
$url .= '?' .http_build_query($_GET + ($allowed_hosts[$requested_host]['args']) ?: array());
}
// Instantiate curl
$curl = curl_init($url);
//Check if request is a POST, and attach the POST data
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$data = http_build_query($_POST);
curl_setopt ($curl, CURLOPT_POST, true);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $data);
}
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Make the call
$response = curl_exec($curl);
// Relay unsuccessful responses
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status >= "400") {
header("Status: 500 Internal Server Error");
}
// Set the Content-Type appropriately
header("Content-Type: " .$allowed_hosts[$requested_host]['mimetype']);
// Output the response
echo $response;
// Shutdown curl
curl_close($curl);

Hi,

Basically, by leaving the second expression empty in the ternary statement, the result of the first expression will be used if it evaluates to TRUE. I wasn’t aware you could do that but, according to the manual, it’s been possible since PHP 5.3.

The reason for passing an empty array on FALSE is that the http_build_query function expects an array as the first argument, so if there aren’t any params to pass in, and empty array is passed instead.