Results 1 to 10 of 10

Thread: Variable reference, unexpected output

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Resolved Variable reference, unexpected output

    I've got this code and I expect to output 5 and 5 as the result but outputs 4 and 5. Please explain a bit, quite confused.
    PHP Code:
    <?php

        
    function &find_var($one$two$three) {

            if((
    $one 0) && ($one <= 10)) return $one;
            if((
    $two 0) && ($two <= 10)) return $two;
            if((
    $three 0) && ($three <= 10)) return $three;

        }

        
    $c_one 'foo';
        
    $c_two 42;
        
    $c_three 4;

        
    $right_var = &find_var($c_one$c_two$c_three);

        
    $right_var++;

        echo 
    "The value of \$c_three and \$right_var are: ";
        echo 
    "$c_three and $right_var<BR>\n";

    ?>
    Thanks in advance.
    Last edited by nebulom; Jul 24th, 2005 at 08:28 PM.

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Variable reference, unexpected output

    well you didn't change value of $c_three in the code nor in the referencing functions... so it will return that... 4 and 5.

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Variable reference, unexpected output

    try this one.. I am thinking it will return 5 and 5.. still have to test this one though

    PHP Code:
    <?php 

        
    function &find_var($one$two$three) { 

            if((
    $one 0) && ($one <= 10)) return $one
            if((
    $two 0) && ($two <= 10)) return $two
            if((
    $three 0) && ($three <= 10)) return $three++; 

        } 

        
    $c_one 'foo'
        
    $c_two 42
        
    $c_three 4

        
    $right_var = &find_var($c_one$c_two$c_three); 

        
    $right_var++;

        echo 
    "The value of \$c_three and \$right_var are: "
        echo 
    "$c_three and $right_var<BR>\n"

    ?>

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Variable reference, unexpected output

    oops that didn't work... but this one..
    works.

    PHP Code:
    <?php 

        
    function find_var($one$two, &$three) { 

            if((
    $one 0) && ($one <= 10)) return $one
            if((
    $two 0) && ($two <= 10)) return $two
            if((
    $three 0) && ($three <= 10)) return $three++; 

        } 

        
    $c_one 'foo'
        
    $c_two 42
        
    $c_three 4

        
    $right_var find_var($c_one$c_two$c_three); 

        
    $right_var++;

        echo 
    "The value of \$c_three and \$right_var are: "
        echo 
    "$c_three and $right_var<BR>\n"

    ?>

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Variable reference, unexpected output

    Isn't the c_three variable meets the variable to be return as the reference of one who gets the find_var function? I mean, find_var should refer to c_three, right? Or am I missing something?

    Or maybe... c_three is left at the function and values remain at the function? It shouldn't be, right? I mean I passed it at the argument and get the reference. Shouldn't I get the reference of c_three by return c_three at the function that returns a reference of the returning variable?

  6. #6
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Variable reference, unexpected output

    perhaps... ... as far as I know... even changing the value of $three which as you say is supposedly referecing c_three, the actual c_three is still not uhmm... changed. so perhaps... that construct is for another purpose... now the code... I used is make the formal parameter a referencing variable (that is referencing c_three)... so $three++
    I returned the value of 4... which find_var is getting the value... and then increment the value of $three.. and thus $c_three...
    and voila... 5 and 5...

    your construct perhaps has another purpose... I'll read on that more.

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Variable reference, unexpected output

    aaaah.. now I have bumped into it in the dox... will get back with some of my understanding.

  8. #8
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Variable reference, unexpected output

    I'm back with this one...
    Now this works.

    PHP Code:
    <?php 

        
    function &find_var($one$two, &$three) { 

            if((
    $one 0) && ($one <= 10)) return $one
            if((
    $two 0) && ($two <= 10)) return $two
            if((
    $three 0) && ($three <= 10)) return $three

        } 

        
    $c_one 'foo'
        
    $c_two 42
        
    $c_three 4

        
    $right_var =&find_var($c_one$c_two$c_three); 

        
    $right_var++;

        echo 
    "The value of \$c_three and \$right_var are: "
        echo 
    "$c_three and $right_var<BR>\n"

    ?>
    the problem being that.. you did not pass $c_three as a refencing variable... thus... $three is NOT.. referingcing c_three.. but you are returning a reference to a local variable which has lost its scope. *sigh*

    see the difference? just the & in the formal parameter declaration of $three.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Variable reference, unexpected output

    Ok, found out the missing link. Hehe. So pass variables by reference and get it as reference. So, somethin like &function(&$,&$,&$){ return $;} should solve this.

    Thank you very much.

  10. #10
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Variable reference, unexpected output

    Glad to help

Posting Permissions

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



Click Here to Expand Forum to Full Width