to Global Variables inside functions - php

I have problem to Global Variables inside functions
<?php
function main(){
$var = "My Variable";
function sub() {
GLOBAL $var;
echo $var; // Will show "My Variable"
}
sub();
echo $var; // Will show "My Variable"
}
main();
sub(); // Will not show and I will sub() cant use outside main() function
?>
I just want to global $var inside sub functions
sub() will not work outside main() function
I tied to use GLOBAL but it show nothing ... Any ?

Not sure if i understand what you want, but your $var is not global. its a local variable inside main()
a variable is only global, if you declare it outside of a function or class.
<?php
$var = "My Variable"; // made $var global
function main(){
//removed $var here
function sub() {
global $var;
echo $var; // Will show "My Variable"
}
sub();
echo $var; // Will throw notice: Undefined variable: var
}
main();
sub(); // Will show "My Variable"
?>
why would you declare a method inside a method to call it from there?
maybe something like this is what you want...
<?php
//$var = "My Variable";
function main(){
$var = "My Variable";
$sub = function($var) {
echo "sub: ".$var; // Will show "sub: My Variable"
};
$sub($var);
echo "main: ".$var; // Will show "main: My Variable"
}
main();
// sub(); // Will not work
// $sub(); // Will not work
?>

You do not assing a value to the global scope variable $var.
Only main() assigns a value to a variable called $var but only in main()'s scope. And only main()'s echo $var; actually prints the value. Both calls to sub() do not produce output.
try it with
<?php
function main(){
$var = "My Variable";
function sub() {
GLOBAL $var;
echo 'sub: ', $var, "\n";
}
sub();
echo 'main: ', $var, "\n";
}
main();
sub();
the output is
sub:
main: My Variable
sub:
and please have a read of https://en.wikipedia.org/wiki/Dependency_injection ;-)

Related

PHP 7 Variable scope problem: global vs. $this->, or what to use instead of global?

My variables are not working, and I'm not sure why.
I did not define $var as static, so when I reference it as global, or as $this->var, it should be the same variable, right?
Except it isn't.
Some people say 'global' shouldn't be used, instead pass parameters to the function. But what if I need to work with 20 variables in an instance's function?
Do I really pass 20 parameters to it?
Doesn't it become unreadable and unclear?
I'm running PHP 7.2.8. on XAMPP, but that isn't really relevant.
<?php
class Test{
public $var;
public function __construct($param)//1
{
global $var; //5
$this->var = $param; //1
$var = $param * 5; //5
}
public function wtf(){
global $var; //5
$foo = $this->var; //1
echo "var: $var <br>";
echo "this var: $foo <br>";
}
}
$foo = new Test(1);
$foo->wtf();
$value = $foo->var;
echo "Value: $value";
?>
Output:
var: 5
this var: 1
Value: 1
I'm expecting the $var to be the same thing in both cases. Why does it become two?

How to assign global variable value in a function in php?

i need to assign a global variable value by passing it in a function, something like static variable i guess. Here is my code
<?php
//this is old value
$var = "Old Value";
//need to change the value of global variable
assignNewValue($var);
echo $var;
function assignNewValue($data) {
$data = "New value";
}
?>
After the execution the value of var need to be New Value. Thanks in advance.
<?php
//this is old value
$var = "Old Value";
//need to change the value of global variable
assignNewValue($var);
echo $var;
function assignNewValue(&$data) {
$data = "New value";
}
?>
I made the argument of assignNewValue a reference to the variable, instead of a copy, with the & syntax.
You can try it in 2 ways, the first:
// global scope
$var = "Old Value";
function assignNewValue($data) {
global $var;
$var = "New value";
}
function someOtherFunction(){
global $var;
assignNewValue("bla bla bla");
}
or using $GLOBALS: (oficial PHP's documentation: http://php.net/manual/pt_BR/reserved.variables.globals.php)
function foo(){
$GLOBALS['your_var'] = 'your_var';
}
function bar(){
echo $GLOBALS['your_var'];
}
foo();
bar();
Take a look: Declaring a global variable inside a function

Getting function scope variable value after declaring a global variable with the same name

so imagine I have this PHP code
<?php
$var = 10;
function foo($var)
{
global $var;
echo $var;
}
foo(2);
?>
The output here is 10. I want to know if there is a way to refer back to the function scope variable $var (which in my example has a value of 2).
Maybe not an exact answer but here is a way:
$var = 10;
function foo($var)
{
$array = get_defined_vars();
global $var;
echo $var;
echo $array['var'];
}
foo(2);
You could also use func_get_arg() or func_get_args(), but whatever you do would need to be before the global statement.

PHP: Global var not being picked up inside a function

This is blowing my mind...
I've got a standalone PHP file, and a simple function with a global var.
<?php
$var = 4;
function echoVar()
{
echo $var;
}
echoVar();
?>
When I call echoVar() nothing is returned... However if I place the $var inside the function it will return 4.
What's going on here? Shouldn't $var be global in this case?
If a variable is set outside of a function, it's not visible inside that function. To access it, you must declare it global with the global keyword. This is called a scope.
<?php
$var = 4;
function echoVar() {
global $var;
echo $var;
}
echoVar();
Note: This is generally considered bad practice. Read this for more information.
A good alternative would be to pass in the variable as an argument:
<?php
$var = 4;
function echoVar($var) {
echo $var;
}
echoVar($var);
Lots of options here... like
<?php
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
?>
or
<?php
$var = 4;
function echoVar()
{
global $var;
echo $var;
}
echoVar();
?>
You can either have $var as an argument, like this:
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
or use global, like this:
$var = 4;
function echoVar()
{
global $var;
echo $var;
}
echoVar();
When you call any function it's create local variable so you have to pass argument in calling function part.
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
Just going to clarify since everyone seems to be posting mostly rubbish.
Do not use global $var;
Do not echo out inside of a function
Output from a function does not need to be assigned to a variable before being echo'd
This is how it "should" be done.
<?php
$var = 4; //set initial input var this is external to the function
function echoVar($internalvar) { /*notice were accepting $var as $internalvar I'm doing this to clarify the different variables so you don't end up getting confused with scope $internalvar is local to the function only and not accessible externally*/
return $internalvar; //now we pass the function internal var back out to the main code we do this with return you should never echo out your output inside the function
}
echo echoVar($var); //call function and pass $var in as an arguement
?>

Why php variable scope not working in function

I executed following code but php says:
Notice: Undefined variable: b in ..\..\..\demo.php on line 4
Notice: Undefined variable: a in ..\..\..\demo.php on line 4
Php Code:
<?php
$a='a';$b='b';
function test(){
echo $a.$b;
}
test(); // error
?>
But i changed the code to this:
<?php
$a='a';$b='b';
function test($a,$b){
echo $a.$b;
}
test($a,$b); // ab
?>
Why $a and $b are undefined in first case, since i defined them before?
Why parameters need to pass in php? It's not require in other like JavaScript.
If variables are defined outside the function, you need to specify the global keyword. Such as:
<?php
$a='a';$b='b';
function test(){
global $a, $b;
echo $a.$b;
}
test(); // error
?>
But your second example is the recommended way of handling it, typically.
$a and $b in the first example you provided are attempting to access those variables respectively from the local scope not the global scope. You could try declaring them like this
function test() {
global $a, $b;
echo $a . $b; //or $GLOBALS['a'].$GLOBALS['b'];
}
and you will get the correct values.
Try this
$a = '101';
$func = function() use($a) {
echo $a;
};
function func_2() {
global $func;
$a = 'not previouse a';
$func();
}
func_2();

Categories