Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,303
    Thanks
    4
    Thanked 204 Times in 201 Posts
    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.
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    Kev0121 (03-04-2009)

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    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)

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Ah right i understand it now it just assigns something to something right?
    Thanks

    Kevin

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Yep that's basically it

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,303
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Quote Originally Posted by webguy08 View Post
    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.
    Dave .... HostMonster for all of your hosting needs

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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