Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    96
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •