Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does php7 still support global variables?

It is possible to use global variable within function in php7? It worked properly in php5.4.

I have found that keyword global is not usefull any more in php7:

global only accepts simple variables migration70.incompatible

But There on page about GLOBALS reserved.variables.globals it is still written that (PHP 4, PHP 5, PHP 7) should support GLOBALS.

Unfortunatelly, nothing of these work for me:

function printGlobal(){
  global ${$a};
  global $b;
  echo '<br/>-'.${$a};
  echo '<br/>-'.$b;
  echo '<br/>-'.$GLOBALS['c'];
}

$a = "hello";
$b = 7;
$c = 6;

printGlobal();

output:

-
-
-

Please do not start a discussion about avoiding of usage of global variables :)

--- EDIT:

I missed, that X.php file I worked with is included withinin index.php file in a function includeAnotherPage(..). Thus, variable defined in X.php file cannot be global variable - because whole X.php file is already in a function. Therefore, codes, writen above, did not work.

Ok thank you for replies. I am stupid and the article locked me in the feeling that globals could be deactivated.

like image 699
Mira Nedved Avatar asked Dec 18 '16 17:12

Mira Nedved


People also ask

Why are global variables not recommended?

Global variables can be altered by any part of the code, making it difficult to remember or reason about every possible use. A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code.

Are there global variables in PHP?

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

How do you declare global variables in pho?

Using global keyword. Using array GLOBALS[var_name]: It stores all global variables in an array called $GLOBALS[var_name]. Var_name is the name of the variable.

Is php7 backwards compatible?

PHP 7 is not backwards compatible.


2 Answers

PHP also gives you the option of defining Global Variables using one of the Super-Globals: $GLOBALS. So you could do something like below which you might QUICK TEST Here as well:

<?php

    $GLOBALS['a']      = "hello";
    $GLOBALS['b']      = 7;
    $GLOBALS['c']      = 6;
    $GLOBALS['hello']  = "Howdy...";

    function printGlobal(){
      $a = $GLOBALS['a'];
      $b = $GLOBALS['b'];
      $c = $GLOBALS['c'];
      $d = $GLOBALS[$a];

      echo '<br/>-'.$d;
      echo '<br/>-'.$b;
      echo '<br/>-'.$c;
    }

    printGlobal();
like image 164
Poiz Avatar answered Oct 16 '22 13:10

Poiz


Your code is almost good, but you have a syntax problem. This code is almost same as your, but it works (tested with PHP 7.0.12).

<?php
function printGlobal() {
    global $a;
    global $b;
  echo '<br />-'.$a; //Echoes $a
  echo '<br />-'.$b; //Echoes $b
  echo '<br />-'.$GLOBALS['c']; //Echoes $c from from the array $GLOBALS
}

$a = 1;
$b = 2;
$c = 3;

printGlobal();

As you can see, in printGlobal I just specify I'm using $a and $b as global, and in the echo, I use $a instead of ${a}, because ${a} can be used inside a double quoted string, here you are outside any quote.

See $GLOBALS, that says there is no need to use global $variable because $GLOBALS is available everywhere.

Output:

<br/>-1<br/>-2<br />-3

Note that you should use \n to write on a new line.

EDIT: Note that in an anonymous function, the usekeywork is available so you can use it to bring variables in the scope of the function.

<?php
$message = "Hello world!";
$ex = function() use ($message) {
    echo $message;
}

See use keyword.

like image 1
AnthonyB Avatar answered Oct 16 '22 13:10

AnthonyB