How can the function http_build_query be used to append parameters to a URL in PHP?

To append parameters to a URL in PHP, you can use the http_build_query function to create a query string from an array of parameters. This function takes an associative array of parameters and returns a URL-encoded query string that can be appended to the URL. This is useful when you need to dynamically add parameters to a URL for things like filtering or pagination.

// Define the base URL
$base_url = 'https://example.com/page';

// Define the parameters as an associative array
$params = array(
    'param1' => 'value1',
    'param2' => 'value2'
);

// Append the parameters to the base URL
$url_with_params = $base_url . '?' . http_build_query($params);

// Output the final URL
echo $url_with_params;