Forum Moderators: coopster

Message Too Old, No Replies

Clarity please

I cant find this in the manual

         

Matthew1980

9:49 pm on Oct 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I am pondering something I found in the dark recesses of the 'tinternet. I haven't yet come across this: =& And I would like to know it's purpose, I looked in the manual, and cant find a reference to it, though if I dissect it: += means to add so:-

$var = 3;
$var += 5;
echo $var; <-- would output 8 as 3 has been added to 5

So I guess as =& is a bitwise operator?

Also this funtion:-

function &newName()

I haven't come across this before, can anyone shed some light on this?

Cheers,
MRb

willybfriendly

10:16 pm on Oct 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe the ampersand is used to "pass by reference'. Not something I have had opportunity to use though.

$varX = "foo";
$varY =& $varX;

echo $varY; //prints "foo"

//change $varX

$varX = "bar";

echo $varY; //prints "bar"

I "think" it works in reverse to. In essence you are setting up an alias for the same data.

Anyango

5:24 am on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep & before the variable name on the right, makes the left side a pointer to the right side variable. & is used to reference an existing variable. So then essentialy

$a=3;
$b=&$a;

means that $b is a pointer/reference to/of $a, just an alias.

Sorry if i misunderstood the question or wrote what you already knew :)

Anyango

5:43 am on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And at times its very helpful, specialy in other languages like c++. Lets say you have an array of size 1 thousand, and you want to run any function on it. Normaly passing that array to your function will send it by value. Which means that function will create a local copy and run operations on that and return it, then you assign this returned array back to your original array. that means double the memory was used. Like this

<?php
$testArray=array(1,259,639,3,26,216,9576,1346);

$testArray=normalCall($testArray);
print_r($testArray);

function normalCall($anyArray)
{
sort($anyArray); // just for example
return $anyArray;
}
?>




Notice you had to assign the returned array back to your main.

If you were to pass that array as a reference, your function will operate on the original array you have and will not create any copy, saving good amount of memory. Also it can used to have consistency in data when you run multiple functions on that. So that all the data is picked up from the main array

like


<?php
$testArray=array(1,259,639,3,26,216,9576,1346);

referenceCall($testArray); // notice you dont have to assign it back
print_r($testArray);

function referenceCall(&$anyArray)
{
sort($anyArray); // just for example
return $anyArray;
}
?>



Again, sorry if this is not what you were talking about and you know it all already. maybe then it can help someone else sometime :)

Matthew1980

7:09 am on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Well, thanks for the tips there! It's quite handy to know as this exists; I'm not too sure about usage yet, but there is always something that can crop up.

From the examples given, I wonder if this action takes up a lot of memory, because if its a 'pointer' to something, a reference to something that pre exists, then surely a good reason for use would be < memory being used.. At least that is how I would think as it would function...

I think next time a write a class (which is always inevitable) I shall hazard a try.

Cheers,
MRb

Anyango

7:25 am on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Example might be poor but this concept is a very very important concept when you deal with big memory eating software, specially in the application development world. It's also and usefull synchronization is needed amongst different functions operating on same data

Anyango

7:28 am on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And also a pointer wont take alot of memory, cause it does not hold the "value" of that variable. It only holds the memory address where that variable is located / starts from (in case of an array or lists). So it only contains value of a memory address.

Matthew1980

9:16 am on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




And also a pointer wont take alot of memory, cause it does not hold the "value" of that variable. It only holds the memory address where that variable is located / starts from (in case of an array or lists). So it only contains value of a memory address.


Exactly my point. I think I shall experiment with my next project.

Cheers,
MRb

penders

7:14 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So I guess as =& is a bitwise operator?


As mentioned, this is the assign by reference [uk3.php.net] operator.

However,
&=
is a 'Bitwise And' assignment operator:
$a &= $b ...same as... $a = $a & $b

...of the same form as += or -=

function &newName()


The & in this case indicates that the function returns a reference [uk3.php.net].

...means that $b is a pointer/reference to/of $a


Just to clarify, references in PHP are not the same as pointers in other programming languages. References do not hold memory addresses. References in PHP are essentially a way of binding one variable to another.
What References Are [uk3.php.net]
What References Are Not [uk3.php.net]

Anyango

7:40 pm on Oct 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Just to clarify, references in PHP are not the same as pointers in other programming languages. References do not hold memory addresses.


This is correct, my fault i mentioned interchanged

Matthew1980

6:33 pm on Oct 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Thank's Penders for that information, I have now got a new project that needs doing, I have a month to do it, so I am going to throw caution to the wind, and try something new, REGEX! I have been putting this off for ages, but now I shall try to see if I can make this penny drop; also these new operator's I like this idea of passing things by reference, especially as it doesn't take up much memory - I shall scour the links that have been provided. Fortunately my new project is intranet and not internet...

Thanks once again.

Cheers,
MRb

penders

8:58 pm on Oct 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...I like this idea of passing things by reference, especially as it doesn't take up much memory


Generally, you should only pass things by reference if you need to pass things by reference. ie. If you need to change the original variable in the calling scope, not just to save memory. Anyango's function is one such example - sorting an array (although strictly speaking you don't need to return this array if you are already 'returning' it via the pass by reference parameter. The function could instead return success or failure perhaps?)

Class instances are automatically passed by reference and non-pass by reference variables only use double the memory when you change that variable within the function - because PHP implements 'copy on write'. You can pass a massive 'lookup-only' array to a function and it doesn't use any more memory than if you passed it by reference (providing you don't change it).