What best practices should be followed when creating links within PHP functions to ensure proper functionality across different directories?

When creating links within PHP functions, it is essential to use relative paths instead of absolute paths to ensure proper functionality across different directories. By using relative paths, the links will adapt to the directory structure of the website, making them more flexible and portable. This approach helps avoid broken links when moving files or directories within the project.

// Example of creating a link using a relative path in PHP
$link = "subdirectory/page.php";
echo "<a href='$link'>Link</a>";