php php中的全局变量没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/107693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:28:32  来源:igfitidea点击:

global variables in php not working as expected

phpglobal

提问by Josh Smeaton

I'm having trouble with global variables in php. I have a $screenvar set in one file, which requires another file that calls an initSession()defined in yet another file. The initSession()declares global $screenand then processes $screen further down using the value set in the very first script.

我在 php 中遇到全局变量的问题。我$screen在一个文件中设置了一个 var,它需要另一个调用另一个文件中initSession()定义的文件。该initSession()声明global $screen,然后进一步处理$屏幕下使用非常的第一个脚本设置的值。

How is this possible?

这怎么可能?

To make things more confusing, if you try to set $screen again then call the initSession(), it uses the value first used once again. The following code will describe the process. Could someone have a go at explaining this?

更令人困惑的是,如果您尝试再次设置 $screen 然后调用initSession(),它将再次使用第一次使用的值。下面的代码将描述这个过程。有人可以解释一下吗?

$screen = "list1.inc";            // From model.php
require "controller.php";         // From model.php
initSession();                    // From controller.php
global $screen;                   // From Include.Session.inc  
echo $screen; // prints "list1.inc" // From anywhere
$screen = "delete1.inc";          // From model2.php
require "controller2.php"         
initSession();
global $screen;
echo $screen; // prints "list1.inc" 

Update:
If I declare $screenglobal again just before requiring the second model, $screen is updated properly for the initSession()method. Strange.

更新:
如果我$screen在需要第二个模型之前再次声明global ,则 $screen 会为该initSession()方法正确更新。奇怪的。

回答by e-satis

GlobalDOES NOT make the variable global. I know it's tricky :-)

Global不会使变量成为全局变量。我知道这很棘手:-)

Globalsays that a local variable will be used as if it was a variable with a higher scope.

Global表示将使用局部变量,就好像它是具有更高范围的变量一样

E.G :

EG :

<?php

$var = "test"; // this is accessible in all the rest of the code, even an included one

function foo2()
{
    global $var;
    echo $var; // this print "test"
    $var = 'test2';
}

global $var; // this is totally useless, unless this file is included inside a class or function

function foo()
{
    echo $var; // this print nothing, you are using a local var
    $var = 'test3';
}

foo();
foo2();
echo $var;  // this will print 'test2'
?>

Note that global vars are rarely a good idea. You can code 99.99999% of the time without them and your code is much easier to maintain if you don't have fuzzy scopes. Avoid globalif you can.

请注意,全局变量很少是一个好主意。如果没有模糊范围,您可以在 99.99999% 的时间内编写代码,并且您的代码更容易维护。global如果可以,请避免。

回答by Athena

global $foodoesn't mean "make this variable global, so that everyone can use it". global $foomeans "within the scope of this function, use the global variable $foo".

global $foo并不意味着“将此变量设为全局变量,以便每个人都可以使用它”。global $foo意思是“在这个函数的范围内,使用全局变量$foo”。

I am assuming from your example that each time, you are referring to $screen from within a function. If so you will need to use global $screenin each function.

我从你的例子中假设每次,你都是从一个函数中引用 $screen 。如果是这样,您将需要global $screen在每个函数中使用。

回答by Internet Friend

If you have a lot of variables you want to access during a task which uses many functions, consider making a 'context' object to hold the stuff:

如果在使用许多函数的任务期间要访问很多变量,请考虑创建一个“上下文”对象来保存这些内容:

//We're doing "foo", and we need importantString and relevantObject to do it
$fooContext = new StdClass(); //StdClass is an empty class
$fooContext->importantString = "a very important string";
$fooContext->relevantObject = new RelevantObject();

doFoo($fooContext);

Now just pass this object as a parameter to all the functions. You won't need global variables, and your function signatures stay clean. It's also easy to later replace the empty StdClass with a class that actually has relevant methods in it.

现在只需将此对象作为参数传递给所有函数。您不需要全局变量,并且您的函数签名保持干净。稍后将空的 StdClass 替换为实际包含相关方法的类也很容易。

回答by finnw

You need to put "global $screen" in every function that references it, not just at the top of each file.

您需要在引用它的每个函数中放置“全局 $screen”,而不仅仅是在每个文件的顶部。

回答by zobier

The global scope spans included and required files, you don't need to use the global keyword unless using the variable from within a function. You could try using the $GLOBALS array instead.

全局范围跨越包含文件和必需文件,除非在函数内使用变量,否则不需要使用 global 关键字。您可以尝试改用 $GLOBALS 数组。

回答by Brynner Ferreira

You must declare a variable as global before define values for it.

您必须先将变量声明为全局变量,然后才能为其定义值。

回答by user3651145

It is useless till it is in the function or a class. Global means that you can use a variable in any part of program. So if the global is not contained in the function or a class there is no use of using Global

除非它在函数或类中,否则它是无用的。全局意味着您可以在程序的任何部分使用变量。因此,如果全局不包含在函数或类中,则没有使用 Global