SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: = and ==

  1. #1
    SitePoint Guru johnjohn2's Avatar
    Join Date
    Apr 2004
    Location
    here
    Posts
    746
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    = and ==

    I was reviewing some code for a friend and fixed some error's where they had a check against a session variable. They had an if statement but instead of using:
    if ($_SESSION['location'] == 5)

    they had:
    if ($_SESSION['location'] = 5)

    I was wondering if he had his code reset the session variable to a value of 5 by his error. Or does php not evaluate a single = to anything?

  2. #2
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    The Caribbean
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are correct, the single = is for assigning variables. Two == is for comparison.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    California
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a pretty costly mistake, too. The condition $_SESSION['location'] = 5 would be true, so whatever is the if statement would then be executed.
    The Banana Stand - an Arrested Development fansite
    LC-3 Help - tutorials on the LC-3 educational assembly language

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The simple answer is to change the order. Write = like this: $var = 5;
    And == like this 5 == $var; now if you miss one off by mistake you will get an error.


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
  •