2016-12-01 72 views
-1

我發現這個代碼非常好的位動態麪包屑:嚴格的標準:只有變量應該按引用傳遞 - 動態路徑導航

<?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> 

這似乎是工作在頁面上。我可以看到它正是我想要的,但我得到一個錯誤只是麪包屑上面:

嚴格的標準:只有變量應該通過參考...

它是通過調用出這是第35行:

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

我不能確定這是什麼意思,我曾通過相關的問題看,但似乎無法理解它和它是如何影響這一點。如果有人能幫助我理解,將不勝感激。

+1

'array_keys($ PATH)'是__not__變量 –

+0

檢查(HTTP的['爲PHP end'文件]: //這個數組是通過引用傳遞的,因爲它是由函數修改的,這意味着你必須傳遞一個真實的變量而不是函數返回一個數組,因爲只有實際的變量可以通過引用傳遞。「_ –

回答

0

我前幾天也出現同樣的錯誤。這一個

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

:只需更換你的下面的代碼行

$full_path = array_keys($path); 
$last = end($full_path); 
相關問題