Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: I don't understand this operator..

1. ## I don't understand this operator..

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

Kevin

• 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.

• ## Users who have thanked djm0219 for this post:

Kev0121 (03-04-2009)

• 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.

• ## Users who have thanked webguy08 for this post:

Kev0121 (03-04-2009)

• Ah right i understand it now it just assigns something to something right?
Thanks

Kevin

• Yep that's basically it

• Originally Posted by webguy08
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
Code:
`\$x =1;`
followed immediately by
Code:
`\$x = 'abc';`
without creating an error.

• Good o'l datatype weak languages. Too bad we lose implicit constraints with it. Oh well.
None of these mention references either.
PHP Code:
``` \$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).

PHP Code:
``` \$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?

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•