does static var get automatic 0 values PHP? - php

I understand the use of static inside of a function.
But what I don't understand in the next example, why the variable $x can be incremented (like it was initialized to zero):
function print_conditional() {
static $x;
if($x++ == 1) {
echo "things";
} else {
echo "good ";
}
}
print_conditional();
print_conditional();
echo PHP_EOL;
This will output "good things"
So, the first time the function is called, the variable $x with no value doesn't match in the if, but the second time, look likes it was incremented to 1 and match, how is that possible?

Decrementing NULL values has no effect, but incrementing them results in 1.
Source

Related

Can't understand function and reference behaviour

I have this code.
$add = (function () {
$counter = 0;
return function () use(&$counter) {return $counter += 1;};
})();
echo $add(); //1
echo $add(); //2
echo $add(); //3
Expected Output:
111
Original Output:
123
Inside the function $counter=0 is assigned by 0 so the &$counter should be 0.
So when i called it second time it sees $counter=0 and so that &$counter will be 0, Isn't it?
Why it is incrementing?
It does not call $counter=0 for the second time. You call it just once when initiating the first function. When you call $add(), you call every time the second function (that is in your return statement) which just uses the modified value of $counter that you passed by reference. If you would add echo $counter; after the $counter = 0; you will see that.
What do you mean by "sees"? The first time you execute $add(), the inner counter is counted up. As you used a reference pointer (through adding the ampersand in use(&$counter)) to the original $counter, this is also manipulated, so after executing this once, the counter variable no longer contains a zero.
When you remove that ampersand, the innermost function uses a fresh counter each and every time, such that your expected output is met
Because you pass a reference to the function the initial $counter = 0; value is also increased each time you add 1 to it using $counter += 1; that's why your result is "123".
To get "111" you need to pass a variable to a function $counter, not a reference.

Assign variable value in php

i'm just confused about assigning variable values on php to another one , how does it work?!
for Example:
<?PHP
$var1=mysqli_fetch_array($query);
while($var2=$var1)
{
echo $var2[$key]; /** this wont't work Correctly however $var1 it's
value = to mysqli_fetch_array**/
}
while($var1=mysqli_fetch_array($query))
{
echo $var1[$key]; /** this will work , why ! **/
}
?>
A peculiarity in PHP's assignment behaviour, is that it also returns the result of the assigned value. This allows for statements such as:
$a = $b = $c = 3; // All of a, b and c will equal 3
And:
while ($variable = call_a_function()) {
do_something_with($variable);
}
In the latter example, variable gets assigned to the output of call_a_function() at the beginning of the loop iteration; as soon as call_a_function() returns a value that evaluates to false, the loop ends. If the returned value does not evaluate to false, variable will contain whatever value was returned, until it gets overwritten again.
Your examples use a similar behaviour. The crucial difference between
$var1=mysqli_fetch_array($query);
while($var2=$var1)
{
echo $var2[$key];
}
And:
while($var1=mysqli_fetch_array($query))
{
echo $var1[$key];
}
...is that in the first example, $var1 is only assigned to the return value of mysqli_fetch_array($query) before the loop starts, while in the second example, $var1 is assigned to the return value of mysqli_fetch_array($query) in every iteration of the loop.
What makes the two pieces of code crucially different, in the end, is the fact that mysqli_fetch_array($query) returns different results, depending on circumstances.
Combining the code snippets into an example that works as intended, yet uses $var2, yields:
while($var2=$var1=mysqli_fetch_array($query))
{
echo $var2[$key];
}
or
$var1=mysqli_fetch_array($query); // ask first time
while($var2=$var1)
{
echo $var2[$key];
$var1=mysqli_fetch_array($query); // ask again, because the answer changed
}
TL;DR: The first example asks a question once, the second asks a question many times. In this case, the intended behaviour of your code requires the question to be asked multiple times, because the answer changes over time.

Recursion function's variable value not changing in PHP

I want to print 1 to 10 numbers using recursion but its not working.
CODE
function table($num1) {
if( $num1 <= 10 ) {
echo $num1;
echo "<br>";
table($num1++);
}
}
$num = 1;
table($num);
Here's the error
Fatal error: Maximum function nesting level of '256' reached,
aborting!
But when I am declaring $num1 as global its working fine. Please anyone tell me the reason behind it.
table($num1++) means please pass $num1 to table(), and then increase it by one. So this is not what you want.
You have to write table(++$num1) instead. It means increase $num1 first, then pass it to table().
Because you are passing a variable in copy. So the first variable has still 1 for value, and you are calling infinitely your function "table".
By default, for all variables (with the exception of objects) PHP makes a copy when passing it to a function/method. If you want to pass the same "in-memory" variable, you have to do it by reference.
$myVar=1;
myFunction($myVar);
Before, you declared your function like this:
function myFunction(&myVar) { ...}
Regards
function table($num) {
if( $num <= 10 ) {
echo $num;
echo "<br>";
table(++ $num);
}
}
$num = 1;
table($num);
table(++ $num) means increase $num first then pass to table function
Try:
function table($value) {
echo $value;
echo "<br>";
$num1=$value+1;
if($num1<=10){
table($num1);
}
}
$num = 1;
table($num);

Auto increment a value when calling a function

How can I auto increment a value when calling a function? Here what I'm trying to do, and I need it just this way. Cant increment it any other way except when calling the function.
function makeyogurt($type = 1) {
echo "Quantity $type.\n";
makeyogurt($type++);
}
makeyogurt();
The code you've shown will necessarily lead to an infinite loop. I guess you are searching for the static keyword:
function fun() {
static $counter = 0;
$counter++;
echo "$counter";
}
If you use static inside a function/method definition, the variable will get created only the first time the function/method is called. It's value will be saved after the call and the variable will get initialized to that value in the next call.
Now you can call the function like this:
fun();
fun();
fun();
Output will be:
1
2
3
Check this manual page
$type++ is a so called post-increment, meaning it will be incremented after the value has been passed.
What you're looking for is ++$type, which is a pre-increment and will pass the newly incremented value.
Try this:
function makeyogurt($type = 1) {
echo "Quantity $type.\n";
$type += 1;
makeyogurt($type);
}
makeyogurt();
If you don't define $type outside the function it always will be 1. I'm not exactly sure what you want, but maybe this?
$type=1;
function makeyogurt($type = 1) {
echo "Quantity $type.\n";
global $type;
++$type;
}
makeyogurt(); //makes it 2
or this:
$type=1;
function makeyogurt($a = 1) {
echo "Quantity $a.\n";
++$a;
return $a;
}
$type=makeyogurt($type); //makes it 2

The value of a local variable of a function has to be retained over multiple calls to the function. How should that variable be declared in PHP

The value of a local variable of a function has to be retained over multiple calls to the function. How should that variable be declared in PHP?
using static. example:
function staticDemo() {
static $x =1;
echo $x;
$x++;
}
the variable $x gets that static value the first time the function is called, afterwards it retains it's state.
example:
staticDemo(); // prints 1
staticDemo(); // prints 2
staticDemo(); // prints 3
Just FYI:
function globalDemo() {
global $x;
echo $x;
$x++;
}
globalDemo(); // prints ''
globalDemo(); // prints 1
globalDemo(); // prints 2
globalDemo(); // prints 3

Categories