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);
Related
Works:
$t = function($x,$y) use (&$t){
...
}
Does not work:
$t = function($x,$y) use ($t){
...
}
Why must I pass the function itself as reference?
Maybe this will help:
$f = 42;
$f = function() use ($f)
{
var_dump($f);
};
$f();
That outputs 42.
The use() is hit before the function is defined and assigned to $f. So if you don't pass by reference, you are accessing the variable as it was before the function was created. In this case, 42. In your case, NULL.
By passing a reference, you'll get $f's value at the time the function is called, which will be the anonymous function as you are expecting (assuming you haven't reassigned $f).
Passing Reference variable means you are accessing same variable within that scope.
Reference variable points to the same variable that were previously created.
Example
<?php
$a = 10;
$b = &$a;
function change_b($pass)
{
$b = $pass++;
}
echo $b."<br />";
change_b(&$a);
echo $b;
?>
above code will output 10 and then 11.
Recursive function has to access the same resource over and over again and that is much more efficient than copying values.
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();
Say I have...
function one($x){
return $a + $x;
}
function two(){
$a = 5;
echo one(3);
}
Will this show the answer "8" or "3"? In other words, will function one get the value of $a or do I need to declare it global somewhere?
N.B. I haven't tried it yet, but I'm asking it here to understand WHY it acts one way or the other.
No function one does not know about $a. But this can be done.
$a = 5;
function one($x){
global $a;
return $a + $x;
}
function two(){
global $a;
$a = 5;
echo one(3);
}
Now two() would echo 8
Functions do not inherent scope from the function that calls them. (Nor do they inherit global variables by default - that's what the global keyword is for.)
Thus, $a will be completely undefined inside of one() and you'll get a notice about it.
For more details, see the Variable Scope page in the PHP manual.
You won't get 8 or 3. You'll get a Notice since $a has not been defined in the scope of the function one, and you attempt to read it:
PHP Notice: Undefined variable: a in - on line 3
PHP Stack trace:
PHP 1. {main}() -:0
PHP 2. two() -:11
PHP 3. one() -:8
If you to use a class as close as to your example, Notice no global usage, just assign your variables $this->* then there global scope within the class and its methods/functions you can also access them from outside of the class like $functions->a:
<?php
Class functions{
function one($x){
return $this->a + $x;
}
function two(){
$this->a = 5;
echo $this->one(3);
}
}
$functions = new functions();
$functions->two(); //8
echo $functions->a;//5
?>
What exactly is the difference between the GLOBAL and STATIC variables in PHP? And which one is preferable to use, when we want to use a variable in multiple functions?
Thanks.
A static variable just implies that the var belongs to a class but can be referenced without having to instantiate said class. A global var lives in the global namespace and can be referenced by any function in any class. Global vars are always frowned upon because they're so easily misused, overwritten, accidentally referenced, etc. At least with static vars you need to reference via Class::var;
Global is used to get the global vars which may be defined in other scripts, or not in the same scope.
e.g.
<?php
$g_var = 1;
function test() {
var_dump($GLOBAL['g_var']);
global $g_var;
var_dump($g_var);
}
Static is used to define an var which has whole script life, and init only once.
e.g.
<?php
function test() {
static $cnt = 0;
$cnt ++;
echo $cnt;
}
$i = 10;
while (-- $i) {
test();
}
A global variable is a variable which is defined in a scope and can span to included and required scopes. (in simple terms by scope I mean the php file/function/class)
Here are some examples of how global variables work.
Example 1: Variable declared in scope and used in function using global keyword
<?php
$a = 1;
function add_a() {
global $a;
$a++;
}
add_a();
echo $a;
In the above example we declare variable $a and assign it value 1 in the scope. We then declare a function add_a in the same scope and try to increment the $a variable value. The add_a function is called and then we echo the $a variable expecting the result to display 2.
Example 2: Variable declared in scope and used in function using the $GLOBALS variable
<?php
$a = 1;
function add_a() {
$GLOBALS['a']++;
}
add_a();
echo $a;
The result from example 2 above is exactly the same as the result from example 1.
There is no difference with using the global keyword and the special PHP defined $GLOBALS array variable. However they both have their advantages and disadvantages.
Read more about $GLOBALS on official PHP website $GLOBALS
If you want to span a scope declared variable to a included or required scope see example below.
Example 3:
file a.php
<?php
global $a;
$a = 1;
require 'b.php';
add_a();
echo $a;
file b.php
<?php
function add_a() {
global $a;
$a++;
}
In the above example we have file a.php and b.php. File b.php is required in file a.php because we use a function declared in file b.php. To use the $a variable in file b.php we must first declare $a as global to be used outside the local scope and we do this by first calling global $a and then we define a value like so $a = 1. Variable $a is now available to be used anywhere in any included scopes by first calling global $a before manipulation.
Static Variables
Usually found in classes but in some well developed PHP project you can find them in recursive functions. A static variable is a variable that remembers its value and can be reused every time the function or method is called.
Here are some examples of a static variable in use.
Example 1: Static variable in a function
function add() {
static $a = 1;
$a++;
echo $a;
}
add(); //2
add(); //3
add(); //4
Example 2: Static variable in class
class A {
public static $a = 1;
public static function add() {
self::$a++;
echo self::$a;
}
}
echo A::$a; //1
A::add(); //2
echo A::$a; //2
A::add(); //3
echo A::$a; //3
A::add(); //4
Note that you cannot assign a return value from a function to a static variable. For example you cannot do static $a = rand(). See Example 3 below on how to assign return value to static variable.
Example 3: Assign return variable from function to static variable
function add() {
static $a;
$a = rand();
echo $a;
}
Read more about global and static variables on PHP official website Variable scope
Global variable is created before the function is created, but global keyword is added in function
$g_var = 1;
function test() {
var_dump($GLOBAL['g_var']);
global $g_var;
var_dump($g_var);
}
static is created and declared static in function
function test() {
static $cnt = 0;
$cnt ++;
echo $cnt;
}
$i = 10;
while(--$i) test();
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;
}