1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[PHP] Whats the difference?

Discussion in 'HTML, Graphics & Programming' started by Conrad11, 6 Jul 2006.

  1. Conrad11

    Soldato

    Joined: 12 Jun 2005

    Posts: 5,362

    Hi there,

    What is the difference between these?

    Code:
    $str = 0;
    and

    Code:
    $str == 0;
    ...and where would i use them and why?

    Thanks.
     
  2. Inquisitor

    Capodecina

    Joined: 12 Apr 2004

    Posts: 11,788

    Location: Birmingham

    = is the assignment operator. It's used to assign a value to a variable, for example:
    Code:
    $bar = 1337;
    $foo = $bar;
    
    It returns the value that was assigned to the variable on the left, so the following code has the same effect:
    Code:
    $foo = ($bar = 1337);
    
    == is the comparison operator. It's used to compare two values, and returns true if they are equal, false otherwise, for example:
    Code:
    $boolean = $foo == $bar; // $boolean is set to true if $foo and $bar are equal
    
    if ($foo == $bar)
    {
        // do something
    }
    
     
  3. robmiller

    Capodecina

    Joined: 26 Dec 2003

    Posts: 16,522

    Location: London

    Inquisitor's second point looks useless, but is actually really useful when you want to initialise a load of variables to the same value:

    Code:
    $foo = $bar = $baz = $mung = null;