Undefined variable with static scope variables in PHP - php

I have a problem when using 'static' in PHP. Here is my code:
static $a = 12;
if(0) {
static $b = 11;
static $a = 11111;
}
echo $a.'----------'.$b;
why the output is "11111----------" and get "Notice: Undefined variable: b"

It must be a scope issue, but I'm not sure why since it's not in a function. Either way, I got it working like this:
static $a = 12;
static $b; // <-- notice this
if (0) {
static $b = 11;
static $a = 11111;
}
echo $a.'----------'.$b;

As a solution to your problem please refer the below code snippet
<?php
static $a = 12;
static $b ;
if(0) {
static $b = 11;
static $a = 11111;
}
echo $a.'----------'.$b;
?>
In the above code snippet variable $b is defined within if block .A variable defined within a conditional or loop block is accessible only within that block.so it needs to be globally declared .Then only it will be accessible in global scope

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

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.

PHP functions variables scope

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 is the difference between "GLOBAL" and "STATIC" variable in PHP?

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();

PHP - Notice: Undefined variable

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

Categories