What are the best practices for handling base URLs in PHP applications?

When working with PHP applications, it is important to handle base URLs properly to ensure consistency and avoid issues with links and redirects. One common approach is to define the base URL in a configuration file or environment variable, and then use this base URL throughout the application to construct links and redirects.

// Define base URL in a configuration file or environment variable
$base_url = "https://www.example.com";

// Construct a link using the base URL
$link = $base_url . "/page.php";

// Redirect to a different page using the base URL
header("Location: " . $base_url . "/another_page.php");