Active2 years ago
Wordfence-waf.php, 2016-10-26 16:12, 382. Wp-activate.php, 2018-12-13 10:39, 6.7K. Wp-admin/, 2018-12-09 20:18,. Wp-blog-header.php, 2016-10-10 23:13.
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:
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.
Mira Nedved
Mira NedvedMira Nedved
2 Answers
![Php Php](http://ketoanthue.vn/images/bieu_mau_thue/Khai_thue_GTGT/Mau_so_02_GTGT.gif)
PHP also gives you the option of defining Global Variables using one of the Super-Globals:
PoizPoiz$GLOBALS
. So you could do something like below which you might QUICK TEST Here as well:6,93622 gold badges88 silver badges1515 bronze badges
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).
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.![Login Login](/uploads/1/2/5/8/125867211/692986267.jpg)
See $GLOBALS, that says there is no need to use
global $variable
because $GLOBALS is available everywhere.Output:
Note that you should use n to write on a new line.
EDIT: Note that in an anonymous function, the
use
keywork is available so you can use it to bring variables in the scope of the function.See use keyword.
AnthonyBAnthonyB1,46911 gold badge1414 silver badges2525 bronze badges