Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Quote:
|
It seems what might be happening that when a variable is passed by reference and variable is changed, the new value isn't available until the end of the statement. Why is this? Does this occur in other languages?
|
Not exactly. You touched a sensible subject in PHP.
In php4, at least, every variable transfered from a function to another is in fact COPIED.
So, when you do
PHP Code:
$var="my value"; $var=str_replace(" ",'_', $var);
It means that str_replace receive a copy of $var, works on it, and return that copy that overwrite the old $var.
Now, when you work by reference, like this:
PHP Code:
$var="my value"; $var=str_replace(" ",'_', &$var);
str_replace don't receive a copy of $var, but the memory adresse of the value of $var.
It means that when you are working with references passed calls, you can change the content of a variable, even when using incorrect syntax.
The example up there is valid, but this would have the same effect:
PHP Code:
$var="my value"; str_replace(" ",'_', &$var);
Because of the reference call is working on the content of the original variable.
I don't know for a lot of languages, but I've learned to use this in C (it's called a pointer, if I'm remembering right).
My guess is that it's a fairly common construction, and surely a lot of languages are providing this facility.
__________________
Only a biker knows why a dog sticks his head out the window.
Last edited by tripy; 11-10-2007 at 01:32 PM..
|