使用“全局”在 PHP 中
我正处于学习模式,对 PHP 非常陌生,所以我正在使用代码示例。 请原谅我在这里使用“全局”,但我想了解 php 变量范围。
这里是myGlobals.php:
<?php
global $db_server;
// other code not shown
?>
这里是connectToDb.php:
<?php
require_once 'myGlobals.php';
// no declared functions in this file, all inline code
$db_server = mysql_connect(.....);
mysql_select_db( "theDatabase", $db_server);
?>
这里是addDbRecords.php:
<?php
require_once 'myGlobals.php';
// other inline code.....
doAddDeleteRecord($db_server);
function doAddDeleteRecord($db_server)
{
//global $db_server;
if( !mysql_query($query, $db_server))
{
// handle the error...
}
}
?>
这里是index.php:
<?php
require_once 'myGlobals.php';
require_once 'connectToDb.php';
require_once 'addDbRecords.php';
// this is simplified, just trying to show that everything in inline code
?>
问题就在这里 当我在文件 addDbRecords.php 中调用 doAddDeleteRecord($db_server)
时 上面,$db_server
无效——它是 null——当我调用 mysql_query(.., $db_server, ...)
时——这是错误消息:
“警告:mysql_query() 需要 参数 2 为资源,给定为 null 在 C:\xampp\htdocs\addDbRecords.php 上 第 29 行”
所以我尝试在 doAddDeleteRecord()
中使用“全局”声明(上面已注释掉)——没有变化。mysql_query(...)
仍然失败,$db_server
的值为 NULL。
我知道 mysql_connect(....)
有效,因为其他代码成功地将我的所有记录从数据库中提取出来(使用 SELECT),并且现有记录在浏览器中正确显示。
所以在我看来,$db_server
声明为“global”这一事实应该意味着$db_server
的范围是这样的,一旦mysql_connect(... )
被调用 - 在我所有文件的文件范围内,$db_server
将是到我的数据库的有效连接。
我只是想了解 php 范围,而不是 OOAD 或其他任何东西(目前)。为什么这里的$db_server()
为空?
I'm in learning mode here, very new to PHP, so I'm working with a code sample.
Please forgive my use of 'global' here, but I want to understand php variable scoping.
Here is myGlobals.php:
<?php
global $db_server;
// other code not shown
?>
Here is connectToDb.php:
<?php
require_once 'myGlobals.php';
// no declared functions in this file, all inline code
$db_server = mysql_connect(.....);
mysql_select_db( "theDatabase", $db_server);
?>
Here is addDbRecords.php:
<?php
require_once 'myGlobals.php';
// other inline code.....
doAddDeleteRecord($db_server);
function doAddDeleteRecord($db_server)
{
//global $db_server;
if( !mysql_query($query, $db_server))
{
// handle the error...
}
}
?>
Here is index.php:
<?php
require_once 'myGlobals.php';
require_once 'connectToDb.php';
require_once 'addDbRecords.php';
// this is simplified, just trying to show that everything in inline code
?>
Here is the problem. When I call doAddDeleteRecord($db_server)
inside the file addDbRecords.php
above, $db_server
is not valid -- it is null -- when I call mysql_query(.., $db_server, ...)
-- this is the error message:
"Warning: mysql_query() expects
parameter 2 to be resource, null given
in C:\xampp\htdocs\addDbRecords.php on
line 29"
So I tried using the 'global' declaration inside doAddDeleteRecord()
(commented out above) -- no change.
The mysql_query(...)
still fails with a NULL value for $db_server
.
I know the mysql_connect(....)
works because other code pulls all my records out of my database successfully (using a SELECT) and the existing records get displayed correctly in the browser.
So in my opinion, the fact that $db_server
is declared with 'global' should mean that the scope of $db_server
is such that once mysql_connect(...)
is called -- at file scope in all my files, $db_server
will be a valid connection to my database.
I'm only trying to learn about php scoping, not OOAD or anything else (for now). Why is $db_server()
null here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以,你有:
并且你在需要的地方包含了这个:
问题是,如果你已经在其他地方包含了“myGlobals.php”,那么它不会包含在这里。所以你不能保证全局会被纳入范围内。
相反,写:
或者采取更好的方法:
So, you have:
and you include this where needed:
The problem is that if you already included 'myGlobals.php' elsewhere, it won't be included here. So you can't guarantee that the global will be brought into scope.
Instead, write:
Or take the much better approach of just:
我认为存在范围隐藏的问题;也就是说,您对全局变量和函数局部变量使用相同的名称 ($db_server)。函数局部作用域隐藏了全局变量名称。如果您有一个全局变量,则无需将其传递给您的函数;如果这样做,请不要使用相同的名称。
I think there's a concern with scope hiding; that is, you're using the same name ($db_server) for the global and for the function local variable. The function local scope hides the global variable name. If you have a global, you don't need to pass it to your function; if you do, don't use the same name for it.