What is the difference between using http_build_query() in PHP 5 and manually creating the URI with foreach()?

When using http_build_query() in PHP 5, it automatically generates a URL-encoded query string from an associative array. This function simplifies the process of creating query strings for URLs. On the other hand, manually creating the URI with foreach() involves iterating over the array and constructing the query string step by step, which can be more time-consuming and error-prone.

// Using http_build_query()
$params = array('name' => 'John Doe', 'age' => 30, 'city' => 'New York');
$queryString = http_build_query($params);
$uri = 'https://example.com?' . $queryString;

// Manually creating the URI with foreach()
$params = array('name' => 'John Doe', 'age' => 30, 'city' => 'New York');
$queryString = '';
foreach ($params as $key => $value) {
    $queryString .= urlencode($key) . '=' . urlencode($value) . '&';
}
$queryString = rtrim($queryString, '&');
$uri = 'https://example.com?' . $queryString;