-1

I found this really nice bit of code for dynamic breadcrumbs:

<?php

// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = Array("<a href=\"$base\">$home</a>");

    // Find out the index for the last value in our path array
    $last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path AS $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));

        // If we are not on the last index, then display an <a> tag
        if ($x != $last)
            $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
        // Otherwise, just display the title (minus)
        else
            $breadcrumbs[] = $title;
    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

?>

<p><?= breadcrumbs() ?></p>

It seems to be working on the page. I can see it exactly how I want it, but I am getting an error just above the breadcrumbs:

Strict Standards: Only variables should be passed by reference in...

it is calling out line 35 which is:

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

I am unsure what this means, I have had a look through related questions but can't seem to understand it and how it's affecting this. If someone could help me understand that would be appreciated.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 1
    `array_keys($path)` is __not__ a variable – u_mulder Dec 01 '16 at 18:31
  • Check the [PHP documentation for `end`](http://php.net/manual/en/function.end.php): _"This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference."_ – Don't Panic Dec 01 '16 at 18:46

1 Answers1

0

I was also getting same error few days ago. just replace your following line of code :

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

with this one :

 $full_path = array_keys($path);
 $last = end($full_path);
Ravi Roshan
  • 570
  • 3
  • 14