0

変数「cache_path」が上部に定義されているにもかかわらず、次のコードでは次のエラーが発生します。

<b>Notice</b>:  Undefined variable: cache_path in <b>C:\Users\Jan Gieseler\Desktop\janBSite\Scripts\Index.php</b> on line <b>20</b><br />

コードは次のとおりです。

header('Content-type: application/x-javascript');

$cache_path = 'cache.txt';

function getScriptsInDirectory(){
    $array = Array();
    $scripts_in_directory = scandir('.');
    foreach ($scripts_in_directory as $script_name) {
        if (preg_match('/(.+)\.js/', $script_name))
        {
            array_push($array, $script_name);
        }
    }
    return $array;
}

function compilingRequired(){
    if (file_exists($cache_path))
    {
        $cache_time = filemtime($cache_path);
        $files = getScriptsInDirectory();
        foreach ($files as $script_name) {
            if(filemtime($script_name) > $cache_time)
            {
                return true;
            }
        }
        return false;
    }
    return true;
}

if (compilingRequired())
{
}
else
{
}

?>

これを修正するにはどうすればよいですか?

EDIT:PHPは、「メイン」スコープにある変数を関数でも使用できるようにすると考えました。私は間違っていたと思います。助けてくれてありがとう。

「グローバル」ステートメントを使用して修正しました。

4

2 に答える 2

1

あなたの $cache_path は関数内では不明です。MonkeyZeus が提案するようにパラメーターとして指定するかglobal $cache_path、関数内で a を使用します。

function compilingRequired(){
    global $cache_path;             // <------- like this
    if (file_exists($cache_path))
    {
        $cache_time = filemtime($cache_path);
        $files = getScriptsInDirectory();
        foreach ($files as $script_name) {
            if(filemtime($script_name) > $cache_time)
            {
                return true;
            }
        }
        return false;
    }
    return true;
}
于 2013-09-10T17:22:18.217 に答える