...

View Full Version : returning value for reference



paperino00
04-21-2012, 07:49 PM
Hello, is the returning values for reference method in php for editing the returned value externally from the function?
Does this code modify the value of the $contatore returned by funzione and add the value in $a to it? Why does it add instead of overwrite it?
Sorry for my questions, but I can't understand these concepts :(
Thank you!


function &funzione()
{
static $contatore = 0;
return ++$contatore;
}
$a =& funzione();
$a=1;
echo funzione();

Fou-Lu
04-21-2012, 09:35 PM
Yes and no. What happens here is the result of the variable created within the function is assigned by reference outside of the function. So the dynamic memory is converted to heap and assigned to static.


function &increment()
{
static $increment = 0;
return ++$increment;
}

$a = &increment();
printf('a = %d' . PHP_EOL, $a); // a = 1.
$b = &increment();
printf('a = %d, $b = %d' . PHP_EOL, $a, $b); // a = 2, b = 2;
print increment() . PHP_EOL; // 3
printf('a = %d, $b = %d' . PHP_EOL, $a, $b); // a = 3, b = 3;
$a = 1;
printf('a = %d, $b = %d' . PHP_EOL, $a, $b); // a = 1, b = 1;
print increment() . PHP_EOL; // 2
printf('a = %d, $b = %d' . PHP_EOL, $a, $b); // a = 2, b = 2;


Changes in my $increment variable will reflect in any variable assigned by reference to the result of the increment() function. Changes in any variable assigned by reference to the result of the function will reflect across all variables assigned by reference since it changes the $increment variable.

The only actual purpose I've found this useful for was for templating that included a end call to the execution time and number of queries. Since the templates were also queried, in order to get the right count including the template, I had to return by reference the value of my db class' query count.

paperino00
04-21-2012, 10:03 PM
When it assign to a variable the value of the returned value, why it act like I run the function?


$b = &increment(); // $increment increase, so that is like run increment() but why? i'm only assignign a value to a variable...

Why I can't edit externally the variable $increment in this way? How could I modify externally a variable inside a function?


function &increment()
{
static $increment = 0;
return ++$increment;
}
&increment() = 3;
$a = &increment();
printf('a = %d' . PHP_EOL, $a); // a = 1.
$b = &increment();
printf('a = %d, $b = %d' . PHP_EOL, $a, $b); // a = 2, b = 2;
print increment() . PHP_EOL; // 3
printf('a = %d, $b = %d' . PHP_EOL, $a, $b); // a = 3, b = 3;
$a = 1;
printf('a = %d, $b = %d' . PHP_EOL, $a, $b); // a = 1, b = 1;
print increment() . PHP_EOL; // 2
printf('a = %d, $b = %d' . PHP_EOL, $a, $b); // a = 2, b = 2;

Thanks

Fou-Lu
04-22-2012, 03:12 AM
This does call the function: $a = &increment();. So of course you will get the behaviour of the function running. This of course will never work, since function calls cannot be the lhs of an expression: &increment() = 3;.

$increment is called within the function, and since its static it retains its previous state.
You cannot access a variable created within a function. That is what scope is all about, so $increment will never be directly available. But since you return by reference and assign by reference to $a and $b, you can modify the value of $increment by modifying one of $a or $b.

paperino00
04-29-2012, 09:53 PM
So in this code when I assign 0 to $a can I think like 0 is propagated to the $increment variable in the third line?


function &increment()
{
static $increment = 0; // Is $a=0 propagated to this variable?
return ++$increment;
}
$a = &increment();
$a=0;
echo increment();

Why if I call this function it show nothing instead of 1?


function increment()
{

static $increment = 0;
return ++$increment;
echo $increment;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum