Global Variables in PHP Not Working as Expected

global variables in php not working as expected

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.

PHP global variables not working inside a function

Looks like homework, still:

<?php

$bool = 1;

boo();

function boo() {
global $bool;

if ($bool == 1) {
$bool = 2;
echo 'Hello World';

}

}
?>

Or

<?php

$bool = 1;

boo(&$bool);

function boo(&$bool) {

if ($bool == 1) {
$bool = 2;
echo 'Hello World';

}

}
?>

global variables in php not working

This is an alternative example. Ian commented on CappY's answer that the real function already returns a value. I assume that is why he thinks he needs a global valriable.

You don't need to (ab)use global variable to return multiple values from a function. Two alternative (and better) options are returning an array, or passing variables by reference.

Example on returning arrays:

function test() {
return array('value 1', 'value 2');
}

// Example usage
list($var1, $var2) = test();
var_dump($var1); // outputs "value 1"
var_dump($var2); // outputs "value 2"

example passing by reference

function test(&$var2) {
$var2 = 'value 2';
return 'value 1';
}

// Example usage
$var1 = test($var2);
var_dump($var1); // outputs "value 1"
var_dump($var2); // outputs "value 2"

Global variables aren't working as expected with frameworks

So as I guessed you are using a framework as you said in the comments:

@Rizier123 Yes, I'm using Laravel. Does it matter? – Kai 6 mins ago

And if it matters? Yes it does.

Probably what is happening here is, that the code which you show us here is wrapped into another function somewhere else.

Means that the variables in the Sum() function are in global scope, but the other ones outside of it not, since they are probably in another function == another scope.


You can reproduce it with this code:

function anotherFunction() {
$a = 1;
$b = 2;

function Sum() {
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}

Sum();
echo $b;
}

anotherFunction();

And if you have error reporting on you will get:

Notice: Undefined index: a

Notice: Undefined index: b

2

Just put the error reporting at the top of your file(s) to get useful error messages:

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>

To solve this now you have to declare the variables also in global scope, either with:

$GLOBALS["a"] = 1;
$GLOBALS["b"] = 2;

or like this:

global $a, $b;
$a = 1;
$b = 2;

global variable not working inside function

Your variables $country_table and $cities_table won't be defined because you didn't bring in the global $wpdb before you tried to use it in those vars. Try this instead:

global $wpdb;
$country_table = $wpdb->prefix . "trackbyid_country";
$cities_table = $wpdb->prefix . "trackbyid_cities";

function database_creation(){
global $country_table, $cities_table;

...

}

Global variable is not working as expected in PHP

Don't use "global" in PHP..
Just use a public variable in your controller;

New code:

abstract class SimpleController {
public $uri;

public function __construct($uri)
{
$this->uri = $uri;
}
}

class home extends SimpleController{
private $template = 'home'; // Define template for this page

public function index()
{
$this->uri; //This is the URI
$view = new View($this->template); // Load the template
}
}

To create your controller just use:

$controller = new home();
$controller->uri = "URI";
$controller->index();

EDIT: Removed constructor from home, when you want to use this also pass $uri.

global variable PHP not getting updated

Global variables are implemented by making the variable a reference to the corresponding $GLOBALS element, i.e.

global $var1;

is equivalent to

$var1 = &GLOBALS['var1'];

But if you then redefine it to be a reference to some other variable, it's no longer a reference to the $GLOBALS element, so it's not a global variable any more.

See the documentation References with global and static variables

If you want $var1 to retain its value, make it a global variable, but don't make it a reference to $varDatabase. Just do

$var1 = $varDatabase;


Related Topics



Leave a reply



Submit