|
-
Jul 24th, 2005, 07:32 PM
#1
Thread Starter
Fanatic Member
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.
-
Jul 24th, 2005, 07:48 PM
#2
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.
-
Jul 24th, 2005, 07:49 PM
#3
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";
?>
-
Jul 24th, 2005, 07:51 PM
#4
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";
?>
-
Jul 24th, 2005, 07:58 PM
#5
Thread Starter
Fanatic Member
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?
-
Jul 24th, 2005, 08:05 PM
#6
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.
-
Jul 24th, 2005, 08:07 PM
#7
Re: Variable reference, unexpected output
aaaah.. now I have bumped into it in the dox... will get back with some of my understanding.
-
Jul 24th, 2005, 08:13 PM
#8
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.
-
Jul 24th, 2005, 08:28 PM
#9
Thread Starter
Fanatic Member
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.
-
Jul 24th, 2005, 08:29 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|