SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help on this Php If statement:If either is true,the other is not false

    Hi guys,
    Hope you're all doing just fine . I'd be grateful if you you can help out with this:

    PHP Code:
    if ( ($country != 'Canada') || ($country != 'United States') ) {

    deny all,except these two countries(either one)
    exit;

    Currently the code has to deny one or the other(whatever returns false). If am from Canada,am denied because am not from the United States(and vice versa).

    I tried it with an array and xor but to no result. May be am too tired i can't see the obvious.

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,826
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this:
    PHP Code:
        <?php
        
    echo '<dl>';
        
    $continent = array('Canada''United States''Brazil''Mexico');
        foreach(
    $continent as $country):
            echo 
    '<dt><br />Country: ' .$country .'<br /></dt>'

            if ( (
    $country === 'Canada') OR ($country === 'United States') )
            { 
                echo 
    '<dd>';
                if (
    'Canada' === $country)
                {
                    echo 
    'Denied because from <b>Canada</b> and not from the <b>United States</b>';
                }else{
                    echo 
    'Denied because from <b>United States</b> and not from the <b>Canada</b>.';
                }
                echo 
    '<br />deny either Canada OR United States'
                echo 
    '</dd>';
            } 
            
            if ( (
    $country != 'Canada') AND ($country != 'United States') )
            { 
                echo 
    '<dd>';
                echo 
    'NEITHER Canada NOR United States';
                echo 
    '</dd>';
            } 
        endforeach;    
        echo 
    '</dl>';
    ?>



    .

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tikila View Post
    Hi guys,
    Hope you're all doing just fine . I'd be grateful if you you can help out with this:

    PHP Code:
    if ( ($country != 'Canada') || ($country != 'United States') ) {

    deny all,except these two countries(either one)
    exit;

    Currently the code has to deny one or the other(whatever returns false). If am from Canada,am denied because am not from the United States(and vice versa).

    I tried it with an array and xor but to no result. May be am too tired i can't see the obvious.
    Do the logic math.
    If your word is.. "Banana"
    IF...TRUE OR TRUE = TRUE.
    If your word is "Canada"
    IF...FALSE OR TRUE = TRUE.
    If your word is "United States"
    IF...TRUE OR FALSE = TRUE.

    You dont want OR. Assuming your test to equal 1 when the word

    1 1 1
    1 0 0
    0 1 0
    0 0 0 (Impossible Case)
    Is AND.
    PHP Code:
    if ( ($country != 'Canada') && ($country != 'United States') ) { 

  4. #4

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Indeed. Couldnt remember the law name off the top of my head, hence the logic table. :P


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
  •