PHP 全局变量问题

发布于 2024-12-11 19:24:23 字数 291 浏览 0 评论 0原文

我这里有一个范围问题。不知道为什么它不起作用,我的设置如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慈悲佛祖 2024-12-18 19:24:23

在 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 need global 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.

§对你不离不弃 2024-12-18 19:24:23

您不需要文件之间的全局变量,仅需要函数。

函数.php

<?php
$foobar = "Hello";
?>

索引.php

<?php
include('Functions.php');
echo $foobar;
?>

You don't need globals between files, only for functions.

Functions.php

<?php
$foobar = "Hello";
?>

Index.php

<?php
include('Functions.php');
echo $foobar;
?>
呆萌少年 2024-12-18 19:24:23

你不应该使用全局变量,但你把它搞反了。在包含其定义后将变量声明为全局:

file1.php:

$name = 'Josh';

file2.php:

require_once('file1.php');

global $name;

echo $name;

You shouldn't use globals, but you have it backwards. You declare the variable global after you include its definition:

file1.php:

$name = 'Josh';

file2.php:

require_once('file1.php');

global $name;

echo $name;
墨离汐 2024-12-18 19:24:23

@thomasrutter 是正确的 (+1) 全局变量是一件坏事。始终寻求替代方案。

也许您可以使用 $_SESSION (我知道,这相当于同一件事),或者声明一个具有 静态变量 并使用 getter() 和 setter() ? (后者的 ui 绝对更干净,但是 $_SESSION 可能会更好地与您的设计结合,我不能说)

顺便说一句,我希望 functions.php 只是一个示例名称,或者您有一个极其简单的项目。

否则,fucntions.php 将会变得异常庞大且难以监管。如果你要面向对象,那么每个类使用一个文件,否则尝试将你的函数分组到单独的文件中(file_management.phpdatabse.phpforms.php 等)。

如果您刚刚开始,我建议您使用 Netbeans 并使用 PhpDoc 注释,这将使您能够生成可以在浏览器中查看的良好文档(包括您的男女同校的结构,获得的内容)声明位置、使用位置、函数破坏参数和返回值等)

顺便说一句,我注意到您使用 include() 我更喜欢 require_once_once 有助于加快性能,而 require 可确保您更快地发现丢失的文件。

哦,还要学习使用 Xdebug,它与 NetBeans 配合得很好

@thomasrutter is correct (+1) Global variables areA 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.php 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 prefer require_once. The _once helps spee dperformnce a little and hte require makes sure that you are aware of missing files more quickly.

Oh, and learn to use Xdebug, which plays well with NetBeans.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文