Answer: The $GLOBALS
array can be used instead:
1 2 3 4 5 6 | $GLOBALS['a'] = 'localhost'; function body(){ echo $GLOBALS['a']; } |
If you have a set of functions that need some common variables, a class with properties may be a good choice instead of a global:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class MyTest { protected $a; public function __construct($a) { $this->a = $a; } public function head() { echo $this->a; } public function footer() { echo $this->a; } } $a = 'localhost'; $obj = new MyTest($a); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.