...

View Full Version : I don't understand this operator..

Kev0121
03-04-2009, 06:15 PM
As the title says, i really don't understand the = assignment operator and how it works i've been told that the = operator takes what's on the right and shoves it to the left but i really don't understand why it does that and how to use the = operator but I'm sure I'm about to find out hehe :D

Kevin

djm0219
03-04-2009, 06:25 PM
The = assigns the value on the right side to the variable on the left side of the =. \$a = 123 sets the variable \$a to the value 123.

webguy08
03-04-2009, 06:41 PM
The = operator just takes whatever is on the right hand side of the = and puts it into the left hand side variable. However, you must ensure that the value on the right hand side is the same as the value capable to be held on the left hand side. For example, if you were to have x = 1, you must ensure that x is of an integer type otherwise the 1 cannot be stored in it.

You would find that the = operator is very useful, and you will use it in pretty much every programming language code you write. You use to it to assign values to variables where you can later use those variables to perform tasks.

For example:
xz = 5*3
and
xy = 5*7
You could then write to work out xz+xy:
xz + xy rather than (5*3)+(5*7), which is not only quicker but can be useful for pieces of code which require the same value repeatedly.

I am being very basic about it and using very basic examples. You will find that with more complicated code the = method can even be used to make variables hold values from methods.

Kev0121
03-04-2009, 06:45 PM
Ah right i understand it now it just assigns something to something right?
Thanks

Kevin

webguy08
03-04-2009, 06:47 PM
Yep that's basically it :)

djm0219
03-04-2009, 07:31 PM
However, you must ensure that the value on the right hand side is the same as the value capable to be held on the left hand side. For example, if you were to have x = 1, you must ensure that x is of an integer type otherwise the 1 cannot be stored in it.

That's not true with PHP since it's a typeless language. You may do
\$x =1; followed immediately by
\$x = 'abc'; without creating an error.

Fou-Lu
03-04-2009, 07:40 PM
Good o'l datatype weak languages. Too bad we lose implicit constraints with it. Oh well.
None of these mention references either.

\$a = 10;
\$b = \$a;

print \$a . "\n"; // 10
print \$b . "\n"; // 10

\$a = 0;
print \$a . "\n"; // 0
print \$b . "\n"; // 10

PHP defaults to pass by value on everything except for objects in post-PHP5. Pre-PHP5, objects were also pass by value, so it was necessary to handle function return references as well (this is more of an argument handling for functions, but you get the point).

\$a = 10;
\$b = &\$a;

print \$a . "\n"; // 10
print \$b . "\n"; // 10

\$a = 0;
print \$a . "\n"; // 0
print \$b . "\n"; //0

So when its said that the value of the right is stuffed into the left, is an incorrect way of perceiving a variable. As you can see above, \$b is not stuffed with \$a, \$b is provided with a pointer that references the same memory location held by \$a. Any changes in \$a are reflected into \$b, though changes in \$b will not be reflected into \$a.

Understand?