Use your left/right keys to browse tutorials
Display breadcrumbs on your site using PHP

Display breadcrumbs on your site using PHP

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on August 17, 2011

When you have a fully dynamic site it’s useful to show the user where they are by breaking down the URL structure so they can navigate backwards through the site. For example this url: shows that ‘jquery’ is part of ‘categories’ so the user could navigate backwards through the url structure.

The Code

The first line of the function gets the REQUEST_URI (/path/to/file.php), splits the string (using ‘/’) into an array, then filters out any empty values. We then store the base url for the site in $base_url. Next we find the array key for the last value in the $path array. We then do a for loop to build up the breadcrumbs and determine where to put the a tag etc.

function breadcrumbs($separator = ' » ', $home = 'Home') {
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
    $base_url = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
    $breadcrumbs = array("<a href=\"$base_url\">$home</a>");

    $last = end(array_keys($path));

    foreach ($path AS $x => $crumb) {
        $title = ucwords(str_replace(array('.php', '_'), Array('', ' '), $crumb));
        if ($x != $last){
            $breadcrumbs[] = '<a href="$base_url$crumb">$title</a>';
            $breadcrumbs[] = $title; 

    return implode($separator, $breadcrumbs);

Calling the function

This is the simple bit! Simply add the above function to the top of your PHP page and call the function as shown below. You can optionally add a separator symbol or specify the wording for ‘Home’.

echo breadcrumbs();

18 discussions around Display breadcrumbs on your site using PHP
  1. Anna Foxin says:

    Some correction to the code. I tried to use the function and detected that it works well only for two levels urls. I made small correction and now it works for any url levels. The function with my correction:
    function breadcrumbs($separator = ‘ » ‘, $home = ‘Home’) {

    $path = array_filter(explode(‘/’, parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
    $base_url = ($_SERVER['HTTPS'] ? ‘https’ : ‘http’) . ‘://’ . $_SERVER['HTTP_HOST'] . ‘/’;
    $breadcrumbs = array(“$home“);

    $last = end(array_keys($path));

    $prev_crumb = “”;
    foreach ($path AS $x => $crumb) {
    $title = ucwords(str_replace(array(‘.php’, ‘_’), Array(”, ‘ ‘), $crumb));
    if ($x != $last){
    $crumb = $prev_crumb.$crumb;
    $breadcrumbs[] = ‘‘.$title.’‘;
    $prev_crumb = $crumb.”/”;
    $title = make_title_string(str_replace(“-”, ” “, $title));
    $breadcrumbs[] = $title;

    return implode($separator, $breadcrumbs);

    • Nedra says:

      Hi Anna,

      Thanks very much for your useful additions to the code above. However, using your codes, I’m encountering 2 issues.

      The first is that there is an unknown function being used, the “make_title_string” function. Are you declaring this function elsewhere, or am I missing something blatantly obvious here?

      The second issue is a “Strict Standards: Only variables should be passed by reference” on the line containing “$last = end(array_keys($path));”.

      If you could please shed some light on these issues, I’d be very grateful.

      Many thanks!

    • Anna Foxin says:

      Hi Nedra,
      Sorry for the inaccuracy. The make_title_string is my private function. It changes the first letter in every title’s word to the uppercase. So you can absolutely ignore it.
      Regard to you second note. This line of code was taken from the original code of the author of this blog.

  2. Dev Guy says:

    Oh this is a really good tutorial, it’s just what I was looking for.

  3. FotoAdmin says:

    May be need add microformat or RDF to breadcrums for beautiful view in Google =).

  4. Deepak Raikwar says:

    Hi All,
    Thanks for your post and reply also.
    This is very usefull script.

    I am learner, so can anyone help me out to learn jQuery and PHP.

    Deepak Raikwar

  5. hrvi says:

    Your snippet didn’t worked for me. Breadcrumbs names are echoed right, but links to crumbs lower in the tree are all wrong and only works for single directory depth. So I made my version, check it out at . I had some issues with $base_url, but I find this version the most compatible and it works on my local server and on a production server too, although codepad doesn’t get it right.

  6. Ryan says:

    Thanks Ashley, This is some nice piece of code to implement breadcrumbs.

  7. nicksp says:

    Ah, html in the comments are not working ;(

    So, in order to obtain working code check this (it’s a code snippets share service)

    Have a nice day.

  8. nicksp says:

    Nice tip. Thanks. Thought there is an error in your code on line 12.

    Replace that line by the following piece of code:
    $breadcrumbs[] = ‘‘ . $title . ‘‘;

  9. yara says:

    Line 12 :

    Replace by :
    $breadcrumbs[] = “$title“;

    Because no replacing varible with single quote ;-)

  10. DaveBowman says:

    What’s the use of this bit? If a user has fully dynamic site with clean URLs it is most certainly operated by some sort of a CMS, that surely can do breadcrumbs. And if he hasn’t, this snippet won’t help anyway.

    • Ashley says:

      @Dave I think it’s pretty bad of you to assume that every site with clean URL’s uses a CMS system. The point of my tutorials are as much to show the techniques in which to do things as well as being able to use them in web projects.

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.

What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.