SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: not (!)

  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    not (!)

    Code:
    code1
    
    <?php 
    $myVar=1;
    if (!$myVar==0)
    {
    echo $myVar; 
    }
    ?>
    //if $myVar is not 0, then print $myVar.
    The code above print 1. It works fine.

    Code:
    code2
    
    <?php
    $myVar=1;
    if (!$myVar==2)
    {
    echo $myVar; 
    }
    ?>
    
    //if $myVar is not 2, then print $myVar.
    I am expecting that the code above print 1.
    But it doesn't print 1.

    What's wrong in the code2?
    How can I correctly write for the meaning of "if $myVar is not 2, then print $myVar."?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,891
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    if (!$myVar==2) is not "if $myVar does not equal 2", it is "if !$myVar equals 2", that is to say "if the boolean negation of $myVar equals 2".
    Since $myVar is 1, which has a boolean value of true, the boolean negation of $myVar is false. Since false doesn't equal 2 the condition in the if in your second snippet doesn't validate to true.

    What you seem to want is

    if ($myVar != 2)

    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could throw some parentheses around it though.
    PHP Code:
    <?php
    $var 
    1;

    var_dump(! ($var == 2) ); #true
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you, ScallioXTX.

    Thank you, AnthonySterling.

  5. #5
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    Code:
    if (!$myVar==2)
    I think the problem is your "2" is too big
    aaron-fisher.com - PHP articles and more

  6. #6
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    Thank you, ScallioXTX.

    Thank you, AnthonySterling.
    This will also be useful.

    http://en.wikipedia.org/wiki/Boolean_algebra_(logic)
    Last edited by ScallioXTX; May 26, 2011 at 08:03. Reason: fixed url


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
  •