What is the best approach to implement a breadcrumb navigation in PHP?

Implementing a breadcrumb navigation in PHP involves dynamically generating links based on the current page's hierarchy. This can be achieved by storing the page hierarchy in an array and then looping through it to create the breadcrumb links.

<?php
// Define the page hierarchy
$pages = array(
    'Home' => 'index.php',
    'Products' => 'products.php',
    'Category' => 'category.php',
    'Product' => 'product.php'
);

// Get the current page
$current_page = basename($_SERVER['PHP_SELF']);

// Create breadcrumb navigation
$breadcrumbs = '<a href="index.php">Home</a>';
foreach ($pages as $page => $url) {
    if ($current_page == $url) {
        $breadcrumbs .= ' > ' . $page;
        break;
    } else {
        $breadcrumbs .= ' > <a href="' . $url . '">' . $page . '</a>';
    }
}

echo $breadcrumbs;
?>