I have
require_once 'db_connect.php'
inside which is
里面是
$db_name = ...
Then directly below I have
然后直接在我的下面
function my_func() {
global $db_name;
}
However $db_name
is returning a blank value. What am I doing wrong?
但是$ db_name返回一个空值。我究竟做错了什么?
Update: If I print $db_name directly after require_once it works fine. However all this is part of a sidebar in wordpress therefore I'm not sure about function scope.
更新:如果我在require_once之后直接打印$ db_name它可以正常工作。然而,所有这些都是wordpress侧边栏的一部分,因此我不确定功能范围。
3 个解决方案
#1
3
EDIT: My answer was tailored to the original version of the question. Please note that the answer below was an answer based on information available at the time.
编辑:我的答案是根据问题的原始版本量身定制的。请注意,以下答案是基于当时可用信息的答案。
When you require()
or include()
the included script runs in the same scope than the line where the file is being included.
当您需要()或include()时,包含的脚本运行在与包含文件的行相同的范围内。
Consider the following:
考虑以下:
myscript.php
<?php
function init_db() {
require_once("database.inc.php");
}
echo $db_name;
function test() {
global $db_name;
echo $db_name;
}
test();
database.inc.php
<?php
$db_name = "hello";
Since database.inc.php is being included in the function init_db()
, none of the echo
will actually output anything. $db_name
is local to the function init_db()
.
由于database.inc.php包含在函数init_db()中,因此没有任何echo实际输出任何内容。 $ db_name是函数init_db()的本地。
To remedy that, simply add global $db_name;
to the database.inc.php file:
要解决这个问题,只需添加全局$ db_name;到database.inc.php文件:
database.inc.php
<?php
global $db_name;
$db_name = "hello";
Think of require()
or include()
as copy-pasting code where it is called. Since you are inside the function init_db()
when database.inc.php is executed, global $db_name;
will allow you to set $db_name
globally.
将require()或include()视为调用它的复制粘贴代码。由于您在执行database.inc.php时位于函数init_db()内,因此全局$ db_name;将允许您全局设置$ db_name。
#2
0
The two things that spring to mind are, is $db_name actually being assigned the value you expect it to be getting, and are you including the file in the global scope?
我想到的两件事是,$ db_name实际上被分配了你期望它获得的值,你是否在全局范围内包含该文件?
If $db_name is being assigned a value by a function that might fail (for example mysql_connect will only return a connection handle if a connection with the database is established) then $db_name might be being assigned a NULL value.
如果$ db_name被一个可能失败的函数赋值(例如,如果建立了与数据库的连接,mysql_connect将只返回一个连接句柄),那么可能会为$ db_name分配一个NULL值。
Included files inherit the scope from which the include statement was executed. If you include inside a function, then any values assigned in the include will only be in that function's scope and will cease to exist outside of scope. Ditto for classes and methods. Please make sure your include statement is being executed in the global scope.
包含的文件继承执行include语句的范围。如果在函数内部包含,则include中指定的任何值将仅在该函数的作用域中,并且将不再存在于作用域之外。同上课程和方法。请确保您的include语句正在全局范围内执行。
#3
0
Although you can use global
keyword to bring variables into the scope, I would recommend using superglobal $GLOBALS
array. (Explicit is better than implicit). And I mean not only in your function that uses a global variable, but also in a file that defines it.
虽然您可以使用global关键字将变量带入范围,但我建议使用superglobal $ GLOBALS数组。 (明确比隐含更好)。我的意思不仅在于使用全局变量的函数,还在于定义它的文件中。
// db_connect.php
$GLOBALS['db_name'] = 'my_db_name';
This way, when someone (even you, a month later) is looking at your db_connect.php
he will instantly understand that this variable is supposed to be in global scope, and will think twice before trying to rename it.
这样,当有人(甚至你,一个月后)正在查看你的db_connect.php时,他会立即明白这个变量应该在全局范围内,并且在尝试重命名之前会三思而后行。
#1
3
EDIT: My answer was tailored to the original version of the question. Please note that the answer below was an answer based on information available at the time.
编辑:我的答案是根据问题的原始版本量身定制的。请注意,以下答案是基于当时可用信息的答案。
When you require()
or include()
the included script runs in the same scope than the line where the file is being included.
当您需要()或include()时,包含的脚本运行在与包含文件的行相同的范围内。
Consider the following:
考虑以下:
myscript.php
<?php
function init_db() {
require_once("database.inc.php");
}
echo $db_name;
function test() {
global $db_name;
echo $db_name;
}
test();
database.inc.php
<?php
$db_name = "hello";
Since database.inc.php is being included in the function init_db()
, none of the echo
will actually output anything. $db_name
is local to the function init_db()
.
由于database.inc.php包含在函数init_db()中,因此没有任何echo实际输出任何内容。 $ db_name是函数init_db()的本地。
To remedy that, simply add global $db_name;
to the database.inc.php file:
要解决这个问题,只需添加全局$ db_name;到database.inc.php文件:
database.inc.php
<?php
global $db_name;
$db_name = "hello";
Think of require()
or include()
as copy-pasting code where it is called. Since you are inside the function init_db()
when database.inc.php is executed, global $db_name;
will allow you to set $db_name
globally.
将require()或include()视为调用它的复制粘贴代码。由于您在执行database.inc.php时位于函数init_db()内,因此全局$ db_name;将允许您全局设置$ db_name。
#2
0
The two things that spring to mind are, is $db_name actually being assigned the value you expect it to be getting, and are you including the file in the global scope?
我想到的两件事是,$ db_name实际上被分配了你期望它获得的值,你是否在全局范围内包含该文件?
If $db_name is being assigned a value by a function that might fail (for example mysql_connect will only return a connection handle if a connection with the database is established) then $db_name might be being assigned a NULL value.
如果$ db_name被一个可能失败的函数赋值(例如,如果建立了与数据库的连接,mysql_connect将只返回一个连接句柄),那么可能会为$ db_name分配一个NULL值。
Included files inherit the scope from which the include statement was executed. If you include inside a function, then any values assigned in the include will only be in that function's scope and will cease to exist outside of scope. Ditto for classes and methods. Please make sure your include statement is being executed in the global scope.
包含的文件继承执行include语句的范围。如果在函数内部包含,则include中指定的任何值将仅在该函数的作用域中,并且将不再存在于作用域之外。同上课程和方法。请确保您的include语句正在全局范围内执行。
#3
0
Although you can use global
keyword to bring variables into the scope, I would recommend using superglobal $GLOBALS
array. (Explicit is better than implicit). And I mean not only in your function that uses a global variable, but also in a file that defines it.
虽然您可以使用global关键字将变量带入范围,但我建议使用superglobal $ GLOBALS数组。 (明确比隐含更好)。我的意思不仅在于使用全局变量的函数,还在于定义它的文件中。
// db_connect.php
$GLOBALS['db_name'] = 'my_db_name';
This way, when someone (even you, a month later) is looking at your db_connect.php
he will instantly understand that this variable is supposed to be in global scope, and will think twice before trying to rename it.
这样,当有人(甚至你,一个月后)正在查看你的db_connect.php时,他会立即明白这个变量应该在全局范围内,并且在尝试重命名之前会三思而后行。