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
Related
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
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 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
?>
I have a php file:
<?php
$a = 1;
function test(){
echo $a;
}
test();
?>
And I get this error:
Notice: Undefined variable: a in X:\...\test.php on line 4
Using XAMPP # 32bit W7.
Variables have function scope. $a inside the function is not the same as $a outside the function. Inside the function you have not defined a variable $a, so it doesn't exist. Pass it into the function:
$a = 1;
function test($a) {
echo $a;
}
test($a);
You have trouble understanding variable scope. $a is defined in the global scope, but not in the function scope. If you want your function to know what $a contains, you have two choices :
Make it global (usually a bad solution)
Add a new argument to your function, and pass your variable to your function
You can use global as advised, but that is bad practice. If you need variables in a function from outside the function then pass them as parameters.
$a = 1;
function test($a) {
echo $a;
}
test($a);
For some reason this function won't return the value ciao:
$a = "ciao";
function a() {
return $a;
}
I have no idea why.
Functions can only return variables they have in their local space, called scope:
$a = "ciao";
function a() {
$a = 'hello`;
return $a;
}
Will return hello, because within a(), $a is a variable of it's own. If you need a variable within the function, pass it as parameter:
$a = "ciao";
function a($a) {
return $a;
}
echo a($a); # "ciao"
BTW, if you enable NOTICES to be reported (error_reporting(-1);), PHP would have given you notice that return $a in your original code was using a undefined variable.
In PHP, functions don't have access to global variables. Use global $a in body of the function or pass the value of $a as parameter.
$a is not in scope within the function.
PHP does not work with a closure like block scope that JS works with for instance, if you wish to access an external variable in a function, you must pass it in which is sensible, or use global to make it available, which is frowned on.
$a = "ciao";
function a() {
global $a;
return $a;
}
or with a closure style in PHP5.3+
function a() use ($a) {
return $a;
}