Is it possible to use a global variable and local variable (with the same name) in the same scope? Suppose we have,
<?php
include "example.php";
function foo($bar) {
global $bar;
------------
------------
}
?>
How can we access both $bar (global and local) in the same scope, is there any way?
What about relying on $GLOBALS var?
<?php
include "example.php";
function foo($bar) {
echo $bar;
echo $GLOBALS['bar'];
------------
------------
}
?>
Do what you can to avoid global variables alltogether (maybe pass it as argument). Also, trying to use two different variables with the same name is clearly asking for troubles !
Please have a read: thevaluable.dev/global-variable-states
Emil Moe
Senior Data Engineer
You can pass it as a reference
<?php include "example.php"; $bar = ''; foo($bar); echo $bar; // Hello, World! function foo(&$bar) { // do whatever you want to $bar $bar = 'Hello, World!'; }