How can the PHP function http_build_query() be utilized to construct URLs with parameters?

To construct URLs with parameters in PHP, the http_build_query() function can be utilized. This function takes an associative array of parameters and constructs a URL-encoded query string. This can be useful when building URLs for API requests or adding query parameters to URLs dynamically.

$params = array(
    'param1' => 'value1',
    'param2' => 'value2',
    'param3' => 'value3'
);

$queryString = http_build_query($params);
$url = 'http://example.com/api?' . $queryString;

echo $url;