Global variable can't be read on my function. Anyone know what's wrong with my code below?
Please help me.
<?php
global $a = array(2,3,4);
global $b = array(3,5,6);
function test(){
$y = $a[0]*$b[0];
return $y;
}
test();
?>
<?php
$a = array(2,3,4);
$b = array(3,5,6);
function test(){
GLOBAL $a;
GLOBAL $b;
$y = $a[0]*$b[0];
return $y;
}
test();
?>
It seems you have some misconception regarding the global variables.
You don't declare the variables as global ..instead you tell php that you want to use the use the variable that is not in scope of function
So your code will be
<?php
$a = array(2,3,4);
$b = array(3,5,6);
function test()
{
global $a, $b;
$y = $a[0]*$b[0];
// or you can use $GLOBAL['a'][0] * $GLOBAL['b'][0]
return $y;
}
echo test();
?>
Now with this code you are saying that use variables $a and $b that are defined outside the scope of function.
DOCUMENTATION
Hope it helps you and you understand what I want to say
Use the global keyword to include a variable in scope.
Here's how it would look with the snippet you provided.
$a = array(2, 3, 4);
$b = array(3, 5, 6);
function test() {
// include $a and $b in the scope of this function.
global $a, $b;
$y = $a[0] * $b[0];
return $y;
}
Visit the documentation link provided above to see more syntax of the global keyword.
You can additionally use the $GLOBALS array to access a and b.
$y = $GLOBALS['a'][0] * $GLOBALS['b'][0];
First you have to declare the global.
global $a;
Then you can make it an array.
$a = array(2,3,4);
Also, if you're using a function. Declare global inside the function, not outside of it.
<?php
$a = array(2,3,4);
$b = array(3,5,6);
$output = test($a,$b);
echo $output;
function test($array1,$array2)
{
$y = $array1[0]*$array2[0];
return $y;
}
?>
Here's an explanation for you. Where we have:
function test($array1,$array2)
We are saying that any value that is put in those spots when the function is called is treated as $array1 and $array2 INSIDE the function.
So when we call it we say test($a,$b) So when the function runs $array1 == $a, and $array2 == $b.
Inside the function the variables $a and $b basically becomes $array1 and 2.
The return value makes the function call basically be the equivalent of that return outside of the function so that:
$output = test($a,$b);
Is exactly like saying:
$output = 6;
I hope that helps.
Related
Why does this code print 1 and not anything else? The function passes by reference, but the reference seems not to work out of the function scope. Why?
<?php
function a(&$a, &$b) {
$a =& $b;
}
$a = 1;
$b = 2;
a($a, $b);
$b = 3;
print $a;
?>
When you modify the variable a within the function it's not modified externally because it is not defined as a global variable, so the value of a remains 1 as you declared before calling the function. If you try to add global $a; before $a =& $b; you'll obtain as output $a = 2;
Change $a =& $b; to $GLOBALS['a'] =& $b;
OR
function a(&$a, &$b) {
$a =& $b;
return $a;
}
$a = 1;
$b = 2;
print a($a, $b);
In order to use variables outside of a function, I have to do this:
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
What if there are a lot of global variables, and I just want them all to be locally scoped inside the function? How do I do this without maintaining a long list of global declarations at the beginning of each function? I'm not concerned about best practices, or all the reasons it may be considered dangerous.
Why aren't you doing something like:
$a = 1;
$b = 2;
function Sum()
{
$args = func_get_args();
$result = 0;
foreach($args as $arg) {
$result += $arg;
}
return $result;
}
$b = Sum($a, $b);
echo $b;
which is then more generic, capable of accepting different arguments, or more than two arguments, and useful in a lot more situations.
Ideally, everything your function needs should be in a parameter.
function Sum($a, $b) {
return $a + $b;
}
If you really can't avoid referring to variables outside your function scope, you have several options:
Use the $GLOBALS array:
function Sum() {
return $GLOBALS['a'] + $GLOBALS['b'];
}
Use the global keyword:
function Sum()
global $a, $b;
return $a + $b;
}
Use an "anonymous" function and inject the globals
$Sum = function() use ($a, $b) {
return $a + $b;
}
$result = $Sum();
Anonymous functions are more useful if you want to work with variables in-scope of the functional declaration that aren't global.
extract() them from $GLOBALS
function Sum() {
extract($GLOBALS);
return $a + $b;
}
This last option will pull in all globals, and seems to be what you're looking for.
But seriously, parametize whenever possible.
I have code that looks something like this
<?php
$a = "text";
function hay() {
global $a;
return $a;
}
$b = hay();
var_dump($b); // outputs NULL
?>
It doesn't display any errors.
The global modifier just doesn't work.
Your scope is not global. I think that your file included inside some fumction. That's why variable $a is not global. You could add global keyword to globalize your variable.
global $a;
$a = "text";
you are missing a semicolon in return return $a. This should be return $a;. Try this and your code will work
Somehow, the following results in NULL. Why is this the case? I find it important to know, because the use statements does not support variable variables.
function a() {
$a = "a";
$aa = function() {
global $a;
var_dump($a);
};
$aa();
}
a();
The value is NULL because there is no global with name $a.
The following would print "global":
$a = "global"; // global variable initialization
function a() {
$a = "a";
$aa = function() {
global $a;
var_dump($a);
};
$aa();
}
a();
You can write this :
function a() {
$a = "a";
function b() {
global $a;
var_dump($a);
};
$aa = "b";
$aa();
}
a();
Maybe you can make a reference to the variable variables.
function a() {
$a = "b";
$b = "variable";
$ref = &$$a;
$aa = function() use ($ref) {
var_dump($ref);
};
$aa();
}
a();
which outputs: string(8) "variable"
Variable $t is the variable variable you can access with $$a. Note that inside the function a(), $t is never used, so should be what you want.
$t = 3;
$a = 't';
function a()
{
global $a, $$a;
$x = $$a;
$b = function() use ($x) {
echo $x;
};
$b();
}
a();
The code prints 3.
global $a access a global variable called $a. It is not the local variable in the function a. Since you have never initialized a global variable called $a, it is NULL.
In order for local variables to be usable inside an anonymous function, they must be captured using a use clause. Local variables can either be captured by value or by reference.
If you want to capture by value (which means the value of the local variable $a is copied into the closure at the time it's defined):
function a() {
$a = "a";
$aa = function() use ($a) {
var_dump($a);
};
$aa();
}
a();
If you want to capture by reference (which means inside the closure it directly references the variable $a):
function a() {
$a = "a";
$aa = function() use (&$a) {
var_dump($a);
};
$aa();
}
a();
In this case both would be the same. But if you modified the variable after the closure was created, and before it is run, it would give a different result (the capture by value would still have the previous value).
Why can't PHP keep a pointed value as a global variable?
<?php
$a = array();
$a[] = 'works';
function myfunc () {
global $a, $b ,$c;
$b = $a[0];
$c = &$a[0];
}
myfunc();
echo ' $b '.$b; //works
echo ', $c '.$c; //fails
?>
FROM PHP Manual:
Warning
If you assign a reference to a variable declared global inside a
function, the reference will be visible only inside the function. You
can avoid this by using the $GLOBALS array.
...
Think about global $var; as a shortcut to $var =& $GLOBALS['var'];.
Thus assigning another reference to $var only changes the local
variable's reference.
<?php
$a=array();
$a[]='works';
function myfunc () {
global $a, $b ,$c;
$b= $a[0];
$c=&$a[0];
$GLOBALS['d'] = &$a[0];
}
myfunc();
echo ' $b '.$b."<br>"; //works
echo ', $c '.$c."<br>"; //fails
echo ', $d '.$d."<br>"; //works
?>
For more information see:
What References Are Not and Returning References
In myfunc() you use global $a, $b, $c.
Then you assign $c =& $a[0]
The reference is only visible inside myfunc().
Source:
http://www.php.net/manual/en/language.references.whatdo.php
"Think about global $var; as a shortcut to $var =& $GLOBALS['var'];. Thus assigning another reference to $var only changes the local variable's reference."
PHP doesn't use pointers. The manual explains what exactly references are, do and do not do. Your example is addressed specificly here:
http://www.php.net/manual/en/language.references.whatdo.php
To achieve what you are trying to do, you must resort to the $GLOBALS array, like so, as explained by the manual:
<?php
$a=array();
$a[]='works';
function myfunc () {
global $a, $b ,$c;
$b= $a[0];
$GLOBALS["c"] = &$a[0];
}
myfunc();
echo ' $b '.$b; //works
echo ', $c '.$c; //works
?>