SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: = and ==

  1. #1
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    UK
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    = and ==

    will someone please explain the difference between the following:

    PHP Code:
    if($id=6){
    echo 
    $id;

    and

    PHP Code:
    if($id==6){
    echo 
    $id;

    thanks

    Azaar

  2. #2
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $id = 6;
    This will set the variable $id equal to 6.

    $id == 6;
    This is a comparison, stating that the value in $id is the same as, or logically equal to 6.

    use the first method to set variables, the second in if statements to compare values.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second one is used to compare and so is used correctly. You could read it as "if $id is equal to 6 then echo $id"

    The first is used incorrectly. It is to assign a value to something, for example:
    PHP Code:
    $var1 $avr2
    [edit] Quick fingers Steve!

    Sean
    Last edited by seanf; Jan 16, 2002 at 17:16.
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    UK
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if($id=6){
    echo $id;
    }

    if i was to use the above would it create a problem ? - or just bad practice ?

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It will cause a problem because it will always evaluate to true. You would need to use ==

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Azaar
    if($id=6){
    echo $id;
    }

    if i was to use the above would it create a problem ? - or just bad practice ?
    well, the "problem" it'll create is that it's always true in that case. it's no different than saying

    if (6)

    sometimes you want to use = for its behavior, such as

    if ($r = !@mysql_query('something'))

    in that case, mysql_query()'s return value in assigned to $r and then tested (which is the desired action).

    an example that you see all the time, using the same principle, is:

    while ($row = mysql_fetch_array($r))
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  7. #7
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh, everyone's posting at the same time today
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  8. #8
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    UK
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you all

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to throw in the terminology:

    = is the assignment operator. It assigns the right hand operand to the left hand operand. Eg ($a = 6) assigns the value of 6 to the variable $a.

    == is the equality operator. It compares the value of the two operands and evaluates to TRUE (1) if they are equal or FALSE (0) if not.

    There is also the === operator. But you can look that one up in the manual at some later stage. It is not used very often

  10. #10
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    UK
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok...so when using <= or => in an if statement do u have to use ==> or just => ??

  11. #11
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <= is the less than or equal to comparison operator
    >= is the greater than or equal to comparison operator

    <== and >== do not exist as operators.

    Comparison operators:
    http://www.php.net/manual/en/languag...comparison.php

    All operators:
    http://www.php.net/manual/en/language.operators.php


  12. #12
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only time you need to use more than one equal sign is when you need to compare two values, or need to compare type.
    PHP Code:
    // Here's a nice little list of each operator
    // Note this may not be a complete list.

    // store a value

    $var 5;

    // compare two values

    if($val == or $val != 7)
    {
      
    # stuff
    }

    // compare type

    if($val1 === $val2 or $val3 !== $val4)
    {
      
    # stuff
    }

    // compare less or greater

    if($val5 <= or $val6 >= 5
    {
      
    # stuff

    So for all other comparisons (ie !=, or <= etc.) you just use one equal sign.

    Understand?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


Bookmarks

Posting Permissions

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