Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-21-2012, 07:49 PM   PM User | #1
paperino00
New Coder

 
Join Date: Nov 2011
Posts: 91
Thanks: 2
Thanked 0 Times in 0 Posts
paperino00 is an unknown quantity at this point
returning value for reference

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!

PHP Code:
function &funzione()
{
    static 
$contatore 0;
    return ++
$contatore;
}
$a =& funzione();
$a=1;
echo 
funzione(); 
paperino00 is offline   Reply With Quote
Old 04-21-2012, 09:35 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
PHP Code:
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.
Fou-Lu is offline   Reply With Quote
Old 04-21-2012, 10:03 PM   PM User | #3
paperino00
New Coder

 
Join Date: Nov 2011
Posts: 91
Thanks: 2
Thanked 0 Times in 0 Posts
paperino00 is an unknown quantity at this point
When it assign to a variable the value of the returned value, why it act like I run the function?

PHP Code:
$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?

PHP Code:
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

Last edited by paperino00; 04-21-2012 at 10:39 PM..
paperino00 is offline   Reply With Quote
Old 04-22-2012, 03:12 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 04-29-2012, 09:53 PM   PM User | #5
paperino00
New Coder

 
Join Date: Nov 2011
Posts: 91
Thanks: 2
Thanked 0 Times in 0 Posts
paperino00 is an unknown quantity at this point
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?

PHP Code:
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?

PHP Code:
function increment()
{

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


Last edited by paperino00; 04-30-2012 at 10:18 AM..
paperino00 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:13 AM.


Advertisement
Log in to turn off these ads.