Jump to content

Quick Questions


benjy355

Recommended Posts

For a little while now, I've seen the =& operator in some PHP codes, and honestly, I have no idea what the frick' it means.And I don't know what $var-> means either >_> (The ->)

Link to comment
Share on other sites

When you assign a variable using &, you are assigning it by reference instead of by value. When you do this:$var = 7;You have just copied the value "7" into the memory location identified by $var. Each variable corresponds to a specific address in memory the same way a domain name corresponds to an IP address, or a street address corresponds to a specific house.$var = "string";Now you have assigned the value "string" to the location in memory referenced by $var. $var1 = 14;$var2 = $var1;Now you have copied 14 into the memory location ID'd by $var1, but what does the second statement do? The second statement copies the value in the memory location ID'd by $var1, and stores the same value in the memory location ID'd by $var2. Even though there's all this terminology flying around, it's pretty simple. You have copied one value from one location to another. The value independently exists in both places, and you can change either variable without affecting the other one.So now we have this:$var1 = "string";$var2 =& $var1;This is called assigning by reference (when you use it with a function, it is passing by reference). Now, you have stored the value "string" inside $var1's memory location. But in the second statement, instead of copying the value to $var2's memory location, you have copied a reference to the value. Basically, you have stored the memory location of $var1 inside $var2, so that $var2 points to $var1. Both variables are now pointing to the same location in memory.$var1 = "string";$var2 =& $var1;$var2 = "test";echo $var1; //prints "test"This is often used with objects like arrays, instead of copying the entire array PHP will transparently only place a reference to the array to save on memory. However, the first time you make a change to the new array, it will copy everything first and then update the new copy. This is called copy-on-write, and is a more efficient use of memory that many high-level languages implement in their memory control systems.More on operators:http://www.php.net/manual/en/language.operators.phpThe -> operator (not actually an operator, I believe they call this a dereference) access an object's properties or methods. If you have a class in a variable called $var, and one of the properties of the class is called name, you refer to it like this:$var->name;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...