Martin and Adrian started breadcrumb mania, and I'm continuing the tradition with "Breadcrumbs for PHP Lovers." If you want to know why breadcrumbs make good business, read Martin's Breadcrumbs for All. If you need an ASP-based breadcrumb trail generator, Adrian's Breadcrumbs for Those Using ASP should help.

With no further ado, here's how to build a breadcrumb trail in PHP. I wrote the code mostly by porting Adrian's concept to PHP, using native functions whenever possible.

If you're integrating this generator with a homebrew content management system or have a setup where the home page of your site is not accessible via a single "/" absolute URL, you can set $root_url to a different location; the script will automatically modify all paths to suit. Like Adrian's solution, if $page_title is not set, the script will print "Current Page" for the page the user is on.

Save the code below as breadcrumb.php in your includes directory.

<?php

##############################################################################

# breadcrumb.php Version 1.1 #

# Copyright 2000 Jacob Stetser jstetser@icongarden.com #

# Created Dec 30, 2000 Last Modified May 2, 2001 #

##############################################################################

# COPYRIGHT NOTICE #

# Copyright [and -left] 2000 Jacob Stetser. All Rights Reserved except as #

# provided below. #

# #

# breadcrumb.php may be used and modified free of charge by anyone so long #

# as this copyright notice and the comments above remain intact. By using #

# this code you agree to indemnify Jacob Stetser from any liability that #

# might arise from it's use. #

# #

# This script is released under the BSD license. #

# The author recognizes this script's indebtedness to evolt.org, Martin #

# Burns, Adrian Roselli and countless other ideas of its kind. This script #

# is therefore unencumbered free code. #

##############################################################################

function breadCrumb($PATH_INFO) {

global $page_title, $root_url;

// Remove these comments if you like, but only distribute

// commented versions.

// Replace all instances of _ with a space

$PATH_INFO = str_replace("_", " ", $PATH_INFO);

// split up the path at each slash

$pathArray = explode("/",$PATH_INFO);

// Initialize variable and add link to home page

if(!isset($root_url)) { $root_url=""; }

$breadCrumbHTML = '<a href="'.$root_url.'/" title="Home Page">Home</a> &gt; ';

// initialize newTrail

$newTrail = $root_url."/";

// starting for loop at 1 to remove root

for($a=1;$a<count($pathArray)-1;$a++) {

// capitalize the first letter of each word in the section name

$crumbDisplayName = ucwords($pathArray[$a]);

// rebuild the navigation path

$newTrail .= $pathArray[$a].'/';

// build the HTML for the breadcrumb trail

$breadCrumbHTML .= '<a href="'.$newTrail.'">'.$crumbDisplayName.'</a> &gt; ';

}

// Add the current page

if(!isset($page_title)) { $page_title = "Current Page"; }

$breadCrumbHTML .= '<strong>'.$page_title.'</strong>';

// print the generated HTML

print($breadCrumbHTML);

// return success (not necessary, but maybe the

// user wants to test its success?

return true;

}

?>

To use breadcrumb.php on your pages, you'll need to first include it and, if you desire, set $page_title:

<?php $page_title = "About Us";

include('/includes/breadcrumb.php'); ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"

"http://www.w3.org/TR/REC-html40/loose.dtd">

<html lang="en-us">

<head>

<title><?php echo $page_title ?></title>

[...]

To print out the breadcrumb trail, call the function like this:

<?php breadCrumb($SCRIPT_URL); ?>

If you are running a solution where all pages are generated by a single php script that parses $PATH_INFO, you may want to set $root_url to the actual script and parse use $PATH_INFO in place of $SCRIPT_URL.

The code is commented so you can easily see where to add or change HTML. Enjoy!