How can one efficiently implement a back link in a PHP navigation menu that moves up multiple levels?

To efficiently implement a back link in a PHP navigation menu that moves up multiple levels, you can use a combination of PHP code to determine the current page's depth in the navigation hierarchy and generate the appropriate back link URL. This can be achieved by counting the number of directory levels in the current page's URL and constructing a relative URL to move up multiple levels.

<?php
// Get the current page's URL
$current_url = $_SERVER['REQUEST_URI'];

// Count the number of directory levels in the URL
$levels = substr_count($current_url, '/');

// Construct the back link URL to move up multiple levels
$back_url = str_repeat('../', $levels - 1);

// Output the back link in the navigation menu
echo '<a href="' . $back_url . '">Back</a>';
?>