Jump to content

Passing by reference


mostafatalebi

Recommended Posts

No, the PHP Compiler will optimise your code. The reference is merely a means of creating another variable for the same content; a reference. Its not a pointer and "they are not actual memory addresses" - http://www.php.net/manual/en/language.references.whatare.php

 

Passing by reference allows you to directly manipulate content within a function/method of a variable defined outside the scope of the function/method.

Edited by cpd
Link to comment
Share on other sites

It's used to allow a function the ability to modify a variable. One example is to return an error message from a function, such as how fsockopen does. Another example would be to directly modify an array, such as what sort and related functions do.

 

Passing a variable by reference won't save any memory generally speaking. PHP won't copy a variable's value until it needs (due to a write operation) so if your function never makes a write statement to the parameter then the variable never gets copied and no extra memory is used.

Link to comment
Share on other sites

I'm confused. It seems the php document is saying the same thing as I but in other words. I say it "reduces the memory consumption". Now read the below example of mine and correct me if I'm wrong:

 

EXAMPLE:

 

we have variable $x = "this is a string"; then  I do &$s = $x; I think it means that, "this is a string" has one location in the memory, then two variables with two different names can access it(passing by reference). While:

 

$x = "this is a string"; $s = $x; makes a copy of "this is a string" as variable $x content. Thus, logically, now it occupies two locations of the memory (regular variable assignment). Am I right?

Link to comment
Share on other sites

No, the PHP Compiler will optimise your code. The reference is merely a means of creating another variable for the same content; a reference. Its not a pointer and "they are not actual memory addresses" - http://www.php.net/manual/en/language.references.whatare.php

@mostafatalebi - Did you read this? Edited by Jessica
Link to comment
Share on other sites

@mostafatalebi - Did you read this?

I have. But did you pay attention to "no" by him? So he told that my first justification is not true. This added some confusion to me, therefore I ask my question again with some points added to get a complementary answer with regard to theirs.

Link to comment
Share on other sites

He said No as in they are not "used to reduce memory consumption". He backed that up by showing the manual which says "The reference is merely a means of creating another variable for the same content; a reference. Its not a pointer and "they are not actual memory addresses"

 

Your reply talked about memory addresses. Which it specifically says a reference IS NOT.

 

You said "correct me if I'm wrong:" - You're wrong.

Link to comment
Share on other sites

...is used to reduce memory consumption?

 

 

No.

 

Arguably I should have been clearer as to which question I was answering. 

 

Thanks for clarifying Jessica.

Edited by cpd
Link to comment
Share on other sites

Function/methods within PHP replicate variables passed to them via parameters therefore, you are manipulating a replica of the variable not the variable itself. When passing by reference, you are then manipulating the actual value itself even though its defined outside the scope of the method/function.

 

E.g.

function plusTwo($var) {
    $var+= 2;
}

$a = 1;
plusTwo($a);
echo $a; // Output is 1 still because $var is a replica of the value passed in.

/* ------------- */

function plusTwo(&$var) {
    $var+= 2;
}

$a = 1;
plusTwo($a);
echo $a; // Output is 3 because you passed the variable in by reference and the value was directly manipulated

With regards to assignments: 

$a = 2;
$b = 4;

$c =& $a;
$d = $b;

echo $a; // Output is 2
echo $b; // Output is 4

echo $c; // Output is 2
echo $d; // Output is 4

$c+= 1;
$d+= 2;

echo $a; // Output is 3 because $c directly manipulated the value it references to
echo $b; // Output is still 4

echo $c; // Output is 3
echo $d; // Output is 6 because $d is a replica of $b and therefore has a different value
Edited by cpd
Link to comment
Share on other sites

$x = "this is a string"; $s = $x; makes a copy of "this is a string" as variable $x content. Thus, logically, now it occupies two locations of the memory (regular variable assignment). Am I right?

$s = $x will make what is known as a copy-on-write reference to $x. In other words both $x and $s point to the same value, just like if you had use a reference. The difference is that if you ever do anything that would cause either of them to change, eg $s=strtolower($s); then PHP will copy them at that point.

 

So until you perform some sort of modification to either variable, they still only occupy a single instance of memory and there is no additional overhead copying values over. As such, using a reference merely to save memory/reduce overhead is unnecessary. Only use a reference if you have another reason for doing so.

Edited by kicken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.