php未定义变量,不会被取消

Wil*_*ing 0 php variables undefined

变量$ WebSiteDirectory永远不会被取消,我仍然会收到错误消息

变量在"/../Include/ini.inc.php"中设置:

$WebSiteDirectory = "mydir\";
$newLine = "<br />";
$phpNewLine = "\n";
Run Code Online (Sandbox Code Playgroud)

它被塞满的地方是:

echo $WebSiteDirectory; // this line is fine 
function theFunction($VAR) {
$dir = $WebSiteDirectory."my other dir"; // this line gets an error
var_dump($dir);
return file_exists($dir);
}
Run Code Online (Sandbox Code Playgroud)

如果成型,不确定是否存在错误.但是,我也会在$WebSiteDirectory以后的程序中重复使用,所以我知道它不会在任何地方被取消.我很确定我是盲目的

Sco*_*pey 7

在PHP中,函数范围之外的变量不可见.

您必须将$ WebSiteDirectory作为参数传入函数,或者您可以使用global关键字

function theFunction($VAR){
    global $WebSiteDirectory
Run Code Online (Sandbox Code Playgroud)

请首先阅读PHP手册中关于变量范围的一些内容,因为它说明了支持和反对此方法的一些好的理由:

http://www.php.net/manual/en/language.variables.scope.php