1

Text5.phpにアクセスすると、ステップインの undefiendd 変数が取得され続けますText2.php$steps私の質問は、変数を配列として含めたので、未定義の変数を取得する方法です。

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   

}

?>
4

1 に答える 1

1

私の質問は、変数 $steps を配列として含めたので、未定義の変数を取得する方法です

実際にallowed_in配列で呼び出したことはありません。

パラメータなしで関数を呼び出し、関数内で次if (allowed_in()=== "Allowed")$page = allowed_in()+1;ように呼び出します。allowed_in()

function allowed_in($steps){変数が存在しなければならないことを指定します ( name を作成します$steps)。

=次の記号を使用して、デフォルトのパラメータを作成できます。

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

つまり、パラメーターなしで呼び出すことができるようになりました。

変数がグローバルスコープにあるため、次のことを探してglobalいる場合もあります。$steps

function allowed_in(){
    global $steps;
    //Logic
}
于 2013-01-05T15:06:29.743 に答える