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
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts

    check if two variables are different

    hi
    i need to check if two variables are diffrent.
    hopefully in the same way an if statment can check if two variables are the same with

    if ($var1 == $var2);

    can i just put an ! in front of one or something.

    thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I assume you mean the value of the variables?
    PHP Code:
    if ($var1 != $var2// Or the less used: $var1 <> $var2 
    Techincally you should probably be able to wrap the whole works with a !.
    PHP Code:
    if (!($var1 == $var2)) 
    That should also work, but doesn't really make a lot of sense to me.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    alcool9999 (08-28-2009)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    This would show if variables are different without type check:
    PHP Code:
    if ($var1 != $var2
    I mean integer 5 and string "5" would be considered as equal.

    This would show if variables are different with type check:
    PHP Code:
    if ($var1 !== $var2
    In this case integer 5 and string "5" would be considered as not equal.

  • #4
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    if($var1 != $var2) {

    You only use a singe = rather with the ! than ==.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by the-dream View Post
    if($var1 != $var2) {

    You only use a singe = rather with the ! than ==.
    No thats not correct. != is typeless, !== is typed, just like == is typeless and === is typed.
    So 5 == '5' is true, but 5 === '5' is not.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    tailender1 (08-29-2009)

  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    10
    Thanked 0 Times in 0 Posts
    thanks
    it works.

  • #7
    New Coder
    Join Date
    Aug 2009
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts
    i have been confused by this concept for long time.. thanks any way


  •  

    Posting Permissions

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