How can the issue of the + character being replaced by a space in URLs be addressed when passing parameters in PHP?

When passing parameters in URLs in PHP, the issue of the + character being replaced by a space can be addressed by using the urlencode() function to properly encode the parameters before appending them to the URL.

$param1 = "hello+world";
$param2 = "foo+bar";

$url = "https://example.com/api?param1=" . urlencode($param1) . "&param2=" . urlencode($param2);

echo $url;