PHP 全局变量问题
我这里有一个范围问题。不知道为什么它不起作用,我的设置如下:
functions.php
global $id;
$id = $_GET['id'];
index.php
require_once('functions.php');
echo $id;
现在在functions.php 中我可以回显$id。然而我的 echo $id;在index.php 中显示空白。绝对没有。
我做错了什么?
I've got a scope problem here. and no idea why its not working, ive got a setup as follows:
functions.php
global $id;
$id = $_GET['id'];
index.php
require_once('functions.php');
echo $id;
now inside functions.php i can echo out $id. however my echo $id; inside index.php is bringing up blank. absolutely nothing.
what am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 PHP 中,
global
关键字允许您从局部作用域内引用全局作用域中的变量 - 例如,访问函数内的全局变量。您的示例中不需要global
,因为无论如何您都在全局范围内。我怀疑您向我们展示了您所拥有的内容的简化版本,其中问题出在您尚未向我们展示的代码中。
为什么不应该使用全局变量
像这样的混乱是为什么使用全局变量是一个坏主意并且应该避免的部分原因。
另一种方法是显式传递变量,例如,如果您从另一个文件调用函数或实例化类,则可以将变量作为参数传递给该函数或构造函数。这样做,而不是使用全局变量,可以更轻松地跟踪哪个函数正在访问哪个变量,因为您可以更轻松地跟踪。
In PHP, the
global
keyword allows you to reference variables in the global scope from inside a local scope - eg to access a global variable inside a function. You don't needglobal
in your example, because you are in the global scope anyway.I suspect you are showing us a simplified version of what you have, where the issue is in code you haven't shown us.
Why you shouldn't use globals
Confusion like this is part of why using globals is a bad idea and should be avoided.
The alternative is to pass variables around explicitly, so for example if you call a function or instantiate a class from another file, you pass the variable in as a parameter to that function or constructor. Doing this, instead of using global variables, makes it easier to follow what function is accessing what variable because you can follow the trail easier.
您不需要文件之间的全局变量,仅需要函数。
函数.php
索引.php
You don't need globals between files, only for functions.
Functions.php
Index.php
你不应该使用全局变量,但你把它搞反了。在包含其定义后将变量声明为全局:
file1.php:
file2.php:
You shouldn't use globals, but you have it backwards. You declare the variable global after you include its definition:
file1.php:
file2.php:
@thomasrutter 是正确的 (+1) 全局变量是
一件坏事
。始终寻求替代方案。也许您可以使用 $_SESSION (我知道,这相当于同一件事),或者声明一个具有 静态变量 并使用 getter() 和 setter() ? (后者的 ui 绝对更干净,但是 $_SESSION 可能会更好地与您的设计结合,我不能说)
顺便说一句,我希望
functions.php
只是一个示例名称,或者您有一个极其简单的项目。否则,
fucntions.ph
p 将会变得异常庞大且难以监管。如果你要面向对象,那么每个类使用一个文件,否则尝试将你的函数分组到单独的文件中(file_management.php
,databse.php
,forms.php
等)。如果您刚刚开始,我建议您使用 Netbeans 并使用 PhpDoc 注释,这将使您能够生成可以在浏览器中查看的良好文档(包括您的男女同校的结构,获得的内容)声明位置、使用位置、函数破坏参数和返回值等)
顺便说一句,我注意到您使用
include()
我更喜欢require_once
。_once
有助于加快性能,而require
可确保您更快地发现丢失的文件。哦,还要学习使用 Xdebug,它与 NetBeans 配合得很好。
@thomasrutter is correct (+1) Global variables are
A Bad Thing
. Always seek alternatives.Perhaps you can use $_SESSION (which sort of amounts to the same thing, I know), or declare a class which has a static variable and use a getter() and setter() ? (the latter uis definitely cleaner, but $_SESSION might tie in better with your design, I can't say)
Btw, I hope that
functions.php
was just an example name, or that you have an extermely simple project.Otherwise
fucntions.ph
p is going to get extermaly large and hard to oversee. If you are going OO then user one file per class, otherwse try to group your functions into separate files (file_management.php
,databse.php
,forms.php
and the like).If you are just starting out, I would advise you to use Netbeans and document your code with PhpDoc comments which will allow you to generate good documentation which you can view in your browser (including the structure of your coed, what gets declared where, used where, descruptions of function parameters and return values, etc)
Btw, I notice that you use
include()
I preferrequire_once
. The_once
helps spee dperformnce a little and hterequire
makes sure that you are aware of missing files more quickly.Oh, and learn to use Xdebug, which plays well with NetBeans.