How does PHP 5's http_build_query function simplify the process of adding variables to a URL?

When adding variables to a URL in PHP, it can be tedious to manually concatenate the variables with the URL using ampersands and question marks. PHP 5's http_build_query function simplifies this process by taking an array of key-value pairs and converting it into a URL-encoded query string.

// Define an array of variables to add to the URL
$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York'
);

// Use http_build_query to convert the array into a URL-encoded query string
$queryString = http_build_query($data);

// Append the query string to the base URL
$url = 'https://example.com/api?' . $queryString;

echo $url;