I have this code:
require_once('../config.php');
function ha(){
global $user; /* I need this to define local-only access from this function */
return $user;
}
echo ha();
echo $user; /* Global variable value */
So how can I define a local variable in the function that will be accessed through inside function ha()?
So the output from the echo ha();
will be the value $user that is stored in file config.php, and the last line echo $user
needs to be empty on echo...when I define $user in function static I get an empty value...so how can I define a static value of variable in PHP that is read from file config.php and only access the value in the function?
function ha(){
global $user;
$user2 = 'abc'; // No prefix whatsoever
echo $user; // Prints global $user
echo $user2; // Prints local $user2
}
how can I define a static value of variable in PHP that is read from file config.php and only access the value in the function?
It's all about variable scope. If you want a variable to be defined in the configuration file and only be readable from within a function, then your current approach is incorrect.
Anything declared in the main scope (such as the configuration file loaded in the main scope) will be accessible from nearly anywhere in the code. If you don't want variables to be accessed otherwise than through ha()
then they need to be defined within ha()
.