Static keyword in PHP prints 0 value - php

$x = 5;
$y = 10;
function test($a, $b) {
static $a, $b;
return $a + $b;
}
print test($a, $b);
Why this code prints 0 as value?

The static keyword doesn't modify variables, it creates new ones with new values. See this:
$x = 5;
static $x;
var_dump($x);
$x = 6;
var_dump($x);
By doing:
static $a, $b;
You have overridden input values of your function with new variables. Since both are undefined (static $a, $b is same as static $a=null, $b=null), null + null equals to 0 thus the output.

Related

PHP function pass by reference not working out of function scope

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

Can I pull all php global variables into the function's local scope?

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.

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.

Undefined variable with static scope variables in 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

Categories