Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End If statement not working?

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #31004
    scottgm
    Member

    Hello PHP Wizards, i wonder if anyone can help, Ive got an associative array set up with a bunch of items, the idea is to compare the array items with items from a form using $_POST.

    So far ive got the items displaying from the form as follows

    
    while ($client_prices = each($_POST)) {			/* sets the name of the array to $client_prices for easy identification */
    echo $client_prices; /* Displays the keys withing the $client_prices array */
    echo ' - ';
    echo $client_prices; /* Displays the values within the $client_prices array */
    echo '
    ';
    }
    ?>

    and for the sake of it ill display the array ive set up on the server side using similar code :

    
    while ($server_prices = each($coffee_prices)) {			/* sets the name of the array to $server_prices for easy identification */
    echo $server_prices; /* Displays the keys withing the $server_prices array */
    echo ' - ';
    echo $server_prices; /* Displays the values within the $server_prices array */
    echo '
    ';
    }
    ?>

    now, what i want to do is make sure that the keys (name of items) and the values (cost of items) from the client side are the same as the keys and values ive set on the server. so i created an if statement :

    
    if(
    $client_prices === $server_prices && $client_prices === $server_prices
    )
    echo 'Prices Match';
    else
    echo '

    Oops! Somthing Has Gone, Please Call Us To Place Your Order

    ';

    ?>

    so i thought i had it all working when as it came back with ‘prices match’, i thought all is good. however when i purposely change the value of one of the items in my server side array so it will be different from the incoming client value, it still says ‘prices match’ when they clearly don;t?

    Anyone help would be appreciated :)

    p.s. im fairly new to php, and this is for my learning, not a real client.

    #69675
    Rob MacKay
    Participant

    Need some { } in there :)


    if( $client_prices === $server_prices && $client_prices === $server_prices ) {
    echo 'Prices Match';
    } else {
    echo '

    Oops! Somthing Has Gone, Please Call Us To Place Your Order

    ';
    }

    Or some :’s


    if( $client_prices === $server_prices && $client_prices === $server_prices ):
    echo 'Prices Match';
    else:
    echo '

    Oops! Somthing Has Gone, Please Call Us To Place Your Order

    ';
    endif;
    #69493
    jimmymac87
    Member

    also instead of === use == to check out more operators go here http://www.w3schools.com/PHP/php_operators.asp

    #69495
    TT_Mark
    Member

    Not strictly true @Robskiwarrior The if/else statement @scottgm has written is perfectly valid.

    You only need {} if the if statement does more than one line

    #69496
    Rob MacKay
    Participant

    Yes, but it is terrible practice lol

    #69498
    TT_Mark
    Member

    Think that is more personal preference! :-)

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.