Can a PHP global variable be set to a pointer? - php

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
?>

Related

Global modifier not working in variable scope of a function

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

Global scope not printing data

Why is $a not printing?
And what is the alternate of this, and I dont want to use return.
function abc () {
$a = 'abc';
global $a;
}
abc();
echo $a;
The reason why it's not echoing is because of two things:
1) You need to declare global "before" the variable you wish to define as being global.
and
2) You also need to call the function.
Rewrite:
<?php
function abc()
{
global $a;
$a = 'abc';
}
abc();
echo $a;
For more information on variable scopes, visit the PHP.net website:
http://www.php.net/manual/en/language.variables.scope.php
You can get your variable as:
echo $GLOBALS['a'];
see http://php.net/manual/en/language.variables.scope.php
You can use define():
function abc() {
define("A", "abc");
}
abc();
echo A;
Make sure you call the function. I added that just above echo.
First you must create and assign a variable. And then in you function describe that is a global var you want to use.
$a = 'zxc';
function abc() {
global $a;
$a = 'abc';
}
abc();
echo $a;
This is not really good idea to use golbal such way. I don't really understand why I so much want to use a global var...
But my opinion is better for you to use a pointer to variable.
function abc(&$var){
$var = 'abc';
}
$a = 'zxc';
abc(&$a);
echo $a;
Or even would be better to create an object and then access variable with-in this object

Using global inside a closure in PHP

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).

PHP How do I "global variable" in an array?

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.

Why is not a global variable recognized in a function

Why is an empty line printed instead of 5?
function test()
{
echo "$a <br/>";
}
$a = 5;
test();
Functions in PHP do not inherit global scope or parent scope (unless using an anonymous function with use()).
You can use the global keyword to access them.
function test()
{
global $a;
echo "$a <br/>";
}
CodePad.
Jared Farish also points out the use of the global associative array $GLOBALS which holds all global variables and which, like any super global such as $_POST, $_GET, etc, is in scope everywhere.
function test()
{
echo "$GLOBALS[a] <br/>";
}
$a = 5;
test();
CodePad.
You could use an anonymous function...
$a = 5;
$test = function() use ($a) {
echo $a;
};
$test();
CodePad.
As a footnote, try not to rely on global variables. They can be a sign of poor program design if you overly rely on them.
you forgot to use the global
function test()
{
global $a;
echo "$a <br/>";
}
$a = 5;
test();

Categories