Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global variables in php not working as expected

Tags:

php

global

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

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 = "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 $screen global again just before requiring the second model, $screen is updated properly for the initSession() method. Strange.

like image 887
Josh Smeaton Avatar asked Sep 20 '08 09:09

Josh Smeaton


People also ask

What are the problems with global variables?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.

How do global variables work in PHP?

Global variables refer to any variable that is defined outside of the function. Global variables can be accessed from any part of the script i.e. inside and outside of the function. So, a global variable can be declared just like other variable but it must be declared outside of function definition.

How can set super global variable in PHP?

Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. The PHP superglobal variables are: $GLOBALS. $_SERVER.

Why global variables are evil?

Non-const global variables are evil because their value can be changed by any function. Using global variables reduces the modularity and flexibility of the program. It is suggested not to use global variables in the program. Instead of using global variables, use local variables in the program.


3 Answers

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

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

E.G :

<?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 global if you can.

like image 142
e-satis Avatar answered Sep 29 '22 03:09

e-satis


global $foo doesn't mean "make this variable global, so that everyone can use it". global $foo means "within the scope of this function, use the global variable $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 $screen in each function.

like image 33
Athena Avatar answered Sep 29 '22 03:09

Athena


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

like image 45
finnw Avatar answered Sep 29 '22 05:09

finnw