2013-01-05 21 views
1

訪問Text2.php時,我一直收到Text5.php中的步驟undefiendd變量。我的問題是怎麼來的我amgetting未定義的變量,因爲我已經包括了可變$steps作爲數組:不斷獲取數組的udefined變量

Text5.php

<?php 

$steps = array(1 =>'Text1.php',2 => 'Text2.php',3 => 'Text3.php',4 => 'Text4.php',5 => 'Text6.php',6 => 'Text7.php'); 

function allowed_in($steps){ 
// Track $latestStep in either a session variable 
// $currentStep will be dependent upon the page you're on 

if(isset($_SESSION['latestStep'])){ 
    $latestStep = $_SESSION['latestStep']; 
} 
else{ 
    $latestStep = 0; 
} 
$currentStep = basename(__FILE__); 

$currentIdx = array_search($currentStep, $steps); 
$latestIdx = array_search($latestStep, $steps); 

if ($currentIdx - $latestIdx == 1) 
    { 
     $currentIdx = $_SESSION['latestStep']; 
     return 'Allowed'; 
    } 
    return $latestIdx; 
} 

?> 

Text2.php

  if (allowed_in()=== "Allowed") 
    { 
     //Text2.php code 
    } 
    else 
     { 
$page = allowed_in()+1; 
?> 

<div class="boxed"> 
<a href="<?php echo $steps[$page] ?>">Link to Another Page</a> 
</div> 

<?php 

} 

?> 
+0

什麼Text2.php和Text5.php具有互辦?他們怎麼叫?他們是否在相同的範圍內?他們是不同的HTTP請求? –

+0

您還應該得到另一個錯誤,類似於***警告:**在調用allowed_in()*時缺少參數1。當你沒有將正確的參數傳遞給函數時,你期望發生什麼? – DCoder

+0

發佈的代碼甚至沒有語法上的有效('echo $ steps [$ page []') –

回答

1

我的問題是我怎麼得出一個未定義的變量,因爲我已經將變量$ steps作爲一個數組包括在內

你從來沒有真正叫過任何數組的allowed_in

兩個if (allowed_in()=== "Allowed")$page = allowed_in()+1;調用不帶任何參數allowed_in()功能,在你的函數:

function allowed_in($steps){您指定必須有一個變量(即我們創建名稱$steps)。

您可以通過使用=標誌創建默認參數:

function allowed_in($steps = array()){ 
    //Logic 
} 

這意味着你現在可以不帶參數調用它。

你也可以的情況下,尋找global那是因爲你的$steps變量是在全球範圍內:

function allowed_in(){ 
    global $steps; 
    //Logic 
}