Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 11-10-2007, 01:08 PM Pass by reference?
Extreme Talker

Posts: 238
Location: United States
Trades: 0
I came across something interesting today. Apparently the following code is not the same, as it gives different values of $array in the end:
PHP Code:
$array array_merge($arrayarray_splice($array$positioncount($array), $element)); 
PHP Code:
$temp array_splice($array$positioncount($array), $element);
$array array_merge($array$temp); 
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?
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
frost is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-10-2007, 01:29 PM Re: Pass by reference?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-10-2007, 05:13 PM Re: Pass by reference?
Extreme Talker

Posts: 238
Location: United States
Trades: 0
I know what you mean, but that's not exactly what I was referring to. I guess I should have mentioned this in my original post, but the array which is passed into array_splice() is always passed by reference.

I used a bad example because I called too many things $array.
PHP Code:
$result array_merge($arrayarray_splice($array$positioncount($array), $element)); 
PHP Code:
$temp array_splice($array$positioncount($array), $element);
$result array_merge($array$temp); 
The values of $result are different for both of the above examples. I've been thinking about it a little bit more, and yeah, since it is being passed by reference, it should be available immediately. I think what must be happening is that since the $array argument in the array_merge() call is being passed by reference, it's being copied for use in array_merge() before the array_splice() call is made.

Actually that makes perfect sense. I get it now.
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
frost is offline
Reply With Quote
View Public Profile
 
Old 11-10-2007, 06:46 PM Re: Pass by reference?
shivaji's Avatar
Ultra Talker

Posts: 321
Trades: 0
Maybe you can try this.

PHP Code:
$result array_merge($array, (array_splice($array$positioncount($array), $element))); 
I think you will get the same result like in second example.

Shivaji
__________________

Please login or register to view this content. Registration is FREE
- uncommon free scripts

Please login or register to view this content. Registration is FREE
- Städte, Sport, Party, Gourment, Apartments, Hotels
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Reply     « Reply to Pass by reference?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27437 seconds with 12 queries