How can PHP code be optimized to prevent the duplication of the domain in links?

To prevent the duplication of the domain in links in PHP code, you can define the base URL of the website in a variable and then use this variable to construct all the links. This way, if the domain changes in the future, you only need to update the base URL variable.

<?php
$base_url = "https://www.example.com";

// Use $base_url to construct links
echo "<a href='$base_url/page1'>Page 1</a>";
echo "<a href='$base_url/page2'>Page 2</a>";
?>