How can file paths be correctly specified when using include or require in PHP to outsource common elements like Header, Nav, and Footer?

When using include or require in PHP to outsource common elements like Header, Nav, and Footer, file paths should be specified correctly to ensure the files are included properly. One way to do this is by using the magic constant __DIR__ to get the absolute path of the current file and then appending the relative path to the included file. This ensures that the file paths are always resolved correctly regardless of the current working directory.

<?php
// Include Header
include __DIR__ . '/includes/header.php';

// Include Navigation
include __DIR__ . '/includes/nav.php';

// Include Footer
include __DIR__ . '/includes/footer.php';
?>