|
A PHP User - 29 Aug 2008
in the example, the function is probably returning a temporary variable.
function &square1($number) {
return $number * $number;
// wrong, returning ref to temporary variable?
}
its simmilar to this:
function &square1($number) {
$tmp = $number * $number;
return $tmp; // wrong, returning reference to local
}
$val =& square1(10); // val has garbage
unless you can do this:
$val = square1(10); // that would work in c++, in php?
// a solution would be to use a global variable and return a // reference to it
A C++ User - 29 Aug 2008
i reinforce the question made by JMH
in C++ for example, the following would give a compiler error exactly for the reason he exposed
int & square1( int x )
{
int result = x * x;
return result; // warning, returning of local variable
}
Gus Jones - 29 Aug 2008
The best I can do for additional info:
http://www.onlamp.com/pub/a/php/2002/09/12/php_foundations.html
Gus Jones - 29 Aug 2008
"with references, you could change the element that gets returned, and it would change the array; without, you would get a copy of the element back, and any changes you made would not be reflected in the original."
Would be nice to have an example of the above please...
Gogo the Great, Kingstown, Serbia - 29 Aug 2008
In contrast to A PHP User, I find the webmaster@thinking.tk's example pretty useless in explaining the main point of this section.
Furthermore, I really can't get the point of that example. Namely, when I've put the following line immediately before the end of the first PHP block:
var_dump ($Capital);
it has told me that there's only one element in the $Capital array - the one with the key "Nepal" and value "Katmandu".
So, I can't figure out how could this example clarify Paul's statement that "...the ability to return references becomes more important if, say, you were to return an element in an array - with references, you could change the element that gets returned, and it would change the array..."
I would really appreciate any comments on that!
JMH - 29 Aug 2008
Doesn't variable scope come into play here? In the example above $number only exsits inside the funcion call. Once the function returns, the variable is destroyed.... right? Isn't $val referencing a destroyed variable, in this case, $number?
A PHP User - 29 Aug 2008
11.11.2005, The point is: one good example worth more than 1000 words !! Thanks for doing that webmaster@thinking.tk...
A PHP User - 29 Aug 2008
Thanks for doing that webmaster@thinking.tk...
webmaster@thinking.tk - 29 Aug 2008
<html><head><title>Function Reference</title></head><body>
<table width=50% align=center>
<tr><td>Hello!<br>Make a through study and comment me please.<br>Copy and paste this php code and then run for the desired results. Thanks.</td></tr>
<tr><td bgcolor="#abcdef">
The Capital Cities of the different countries through an array with function reference.<br>
<?php
$Capital["Nepal"]="Kathmandu";
print $Capital["Nepal"];
print "<br>";
function &Ref($n1,$n2){
$Capital["$n1"]="$n2";
return $Capital["$n1"];
}
$s=&Ref("China","Beijing");
print $s;
Print "<br>";
Print $Capital["Nepal"];
Print "<br>";
$s2=&Ref("India","New Deli");
print $s2;
?>
<br>
Every time php access the array without passing through the function there is Kathmandu.
</td></tr>
<tr><td bgcolor="#abcdef">
<p>
But If you pass by reference "g" stands for Glory of Nepal.<br>
<?php
$g="Mt.Everest";
print $g."<br>";
function Ref1(&$g){
$g="Buddha";
return $g;
}
$s1=Ref1(&$g);
print $s1;
Print "<br>";
Print $g;
?>
<br>
But here the glory of Nepal has been permanently changed to Buddha.
</p></td></tr></table></body></html>
A PHP User - 29 Aug 2008
I don't get the point. What is the meaning of this? Could anyone post a practical example illustrating the meaning of returning by reference?
Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.
If you are reporting an error in the content, please tell me directly.
|