What are some best practices for dynamically generating and displaying URLs in PHP scripts for website functionality?

When dynamically generating and displaying URLs in PHP scripts for website functionality, it is important to ensure that the URLs are properly constructed and follow best practices. This includes using functions like `urlencode()` to encode any special characters in the URL, as well as building the URLs dynamically based on variables or user input.

// Example of dynamically generating and displaying a URL in PHP

// Define base URL
$base_url = "https://www.example.com/";

// Get dynamic parameters
$param1 = "value1";
$param2 = "value2";

// Encode special characters in parameters
$param1 = urlencode($param1);
$param2 = urlencode($param2);

// Construct the dynamic URL
$url = $base_url . "page.php?param1=$param1&param2=$param2";

// Display the URL
echo $url;