Auto increment a value when calling a function - php

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

Related

Static variable in functions

I saw this on W3Schools.
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
The output is 0, 1 and 2.
I wonder why it is not 0, 0 and 0.
Since each time the function is called, the variable x becomes 0 again.
I am a PHP beginner. Thanks!
If you declare a var static inside a function, the var keep it value over multiple calls. You could compare it to a static var inside of classes.
The code you post is a good example to see the actual effect. However I would only carefull use static inside functions, because most of the time, you need the static value somewhere else, reset the value, or something else which requires to much logic and makes the code really bad.
A good example would be a function, which returns a unique identifier for a given identifier. This could be simply achieved by using this code.
function unique_id($id) {
static $count = 0;
return $id . ($id++);
}
This example may clarify. static has a scope, thus is not a global variable. So I can define static $x outside the function and it will be defined there. Since static has scope, it wouldn't make any sense to keep on executing and resetting $x = 0. So, php will only acknowledge it the first time that line is called.
<?php
static $x = 1000;
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>

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

does static var get automatic 0 values 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

PHP variable not passing through to another function sometimes

Edit: Made more clearer
I have a problem with a variable disappearing between function calls
firstly I start here with $pid being an int taken from a JSON string
print "PID".$pid."\n";
$a['points'] = Algorithm::getpredictionForPlayer($pid);
I get the output 'PID12' which is how it should be
Next in the Algorithm::getpredictionForPlayer
static function getpredictionForPlayer($pid)
{
print "PID2: ".$pid."\n";
$points =0;
for ($i = 0; $i < 10; $i++)
{
print "algorithm: ".$pid."\n";
$points += v4::predictPointsForPlayer($pid);
}
return intval($points/10);
}
Occasionally i get 'PID2: 12', but more often all that prints is 'PID2: '
Is there a reason the variable would disappear during this time?
Your variable in the global scope is $pid yet you are passing $player_id into the function
print "PID".$pid."\n";
$a['points'] = Algorithm::getpredictionForPlayer($player_id);
You've then got a parameter in your static method called $pid
static function getpredictionForPlayer($pid)
but this isn't the same as the variable in your global scope. In fact this will take the same value as the $player_id that you are passing in. If you want the $pid variable from your global scope to exist in the static method you should pass it in instead of $player_id.
btw, you should think about whether you really need a static method. Generally they make things hard to test and should be avoided if possible. Should you have a player object and call the method getPrediction() on that?
Change this line:
$a['points'] = Algorithm::getpredictionForPlayer($player_id);
To this:
$a['points'] = Algorithm::getpredictionForPlayer($pid);

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