php未定义变量,不会取消设置 [英] php undefined variable, doesnt get unset

查看:41
本文介绍了php未定义变量,不会取消设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The variable $WebSiteDirectory never gets unset how ever i still get an error message

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

the variable gets set in "/../Include/ini.inc.php":

$WebSiteDirectory = "mydir\";
$newLine = "<br />";
$phpNewLine = "\n";

它被塞满的地方是:

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);
}

不确定是否存在错误.但是,我稍后还会在程序中重用 $WebSiteDirectory,因此我知道它不会在任何地方取消设置.我很确定我瞎了

not sure there the error if forming. However, i also reuse $WebSiteDirectory later on the the program so i know it's not getting unset anywhere. i'm pretty sure im blind

推荐答案

在 PHP 中,函数作用域之外的变量不可见.

In PHP variables outside of a functions scope are not visible.

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

You must pass in your $WebSiteDirectory into the function as a parameter, or you can use the global keyword

function theFunction($VAR){
    global $WebSiteDirectory

请先阅读 PHP 手册中有关变量作用域的内容,因为它说明了支持和反对此方法的一些充分理由:

Please read up a little on variable scope in the PHP manual first as it illustrates some good reasons for and against this method:

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

这篇关于php未定义变量,不会取消设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆