Professional Web Applications Themes

Newbie Question. Problems with IF statement - I think I'm goingmad! - PHP Development

Hi and HELP! I'm new to PHP/Perl and having a small problem. The following statement doesn't return the results I'm expecting, and I'm now starting to think I'm going mad... (or I've done something really stupid and just can't see it!) if($INPUT::country = "GB") { $ship_cost = sprintf("%5.2f", $shipping); } else { $ship_cost = $overseas_shipping; } $total = sprintf("%5.2f", ($PRICES{$INPUT::qty}+$ship_cost)); When country=GB then all appears OK, but if country not GB then the else statement isn't pd, I still get the result as if country=GB. Actually need to add an 'elseif' but if I do that the whole script falls ...

  1. #1

    Default Newbie Question. Problems with IF statement - I think I'm goingmad!

    Hi and HELP!

    I'm new to PHP/Perl and having a small problem.

    The following statement doesn't return the results I'm expecting, and
    I'm now starting to think I'm going mad... (or I've done something
    really stupid and just can't see it!)

    if($INPUT::country = "GB") {
    $ship_cost = sprintf("%5.2f", $shipping);
    } else {
    $ship_cost = $overseas_shipping;
    }
    $total = sprintf("%5.2f", ($PRICES{$INPUT::qty}+$ship_cost));

    When country=GB then all appears OK, but if country not GB then the else
    statement isn't pd, I still get the result as if country=GB.

    Actually need to add an 'elseif' but if I do that the whole script falls
    over...
    The total script should read...

    if($INPUT::country = "GB") {
    $ship_cost = sprintf("%5.2f", $shipping);
    }
    elseif($INPUT::country = "US")
    {
    $ship_cost = sprintf("%5.2f", $shippingUSA);
    }
    else {
    $ship_cost = $overseas_shipping;
    }
    $total = sprintf("%5.2f", ($PRICES{$INPUT::qty}+$ship_cost));


    FYI, variables shipping=4, shippingUSA=5, overseas_shipping=8
    At present $PRICES{$INPUT::qty} = 14.99 - there are currently no other
    values.

    Any help appreciated.

    --

    Thanks in advance
    Simon
    Simon Guest

  2. #2

    Default Re: Newbie Question. Problems with IF statement - I think I'm goingmad!

    Simon Benson wrote: 

    Ah, the eternal dilemma of = and ==. If you want to check whether two
    things are equal, you use ==, so it would be:

    if ($INPUT::country == "GB")
    ...

    In your original code, the single equals sign means that the value "GB"
    is assigned to $INPUT::country, and then that value is tested for a
    truth value. Since "GB" is not an empty string, or null, or false, the
    if statement passes, so you will always run that section of code and not
    the else { }.


    --
    Oli
    Oli Guest

  3. #3

    Default Re: Newbie Question. Problems with IF statement - I think I'm going mad!

    Simon Benson wrote:
     
    <snip> 

    And you're using member variables in un-instantiated classes! I've been
    using PHP for years and have no idea how that would behave.

    ....which would make feel really bad if it weren't for the fact you've a
    really basic error in using an assignmanet operator instead of a comparison
    operator - try
    if($INPUT::country == "GB") { // two '=' signs

    HTH

    C.
    Colin Guest

  4. #4

    Default Re: Newbie Question. Problems with IF statement - I think I'm going mad!

    Simon Benson <com> wrote in
    news:tQNMd.353$ntli.net:
     

    http://ca.php.net/manual/en/language.operators.assignment.php
    http://ca.php.net/manual/en/language.operators.comparison.php
    (Hint - what's the difference between "=" and "=="?)

    --
    Dave Patton
    Canadian Coordinator, Degree Confluence Project
    http://www.confluence.org/
    My website: http://members.shaw.ca/davepatton/
    Dave Guest

  5. #5

    Default Re: Newbie Question. Problems with IF statement - I think I'm goingmad!

    Dave Patton wrote: 
    >
    >
    > http://ca.php.net/manual/en/language.operators.assignment.php
    > http://ca.php.net/manual/en/language.operators.comparison.php
    > (Hint - what's the difference between "=" and "=="?)
    >[/ref]
    Thanks guys, despite now being 'at home' I'm going to give it a try now.
    Then I might get some sleep tonight!
    Simon Guest

Similar Threads

  1. Newbie question: problems with CMYK color selection
    By Bob in forum Adobe Indesign Macintosh
    Replies: 4
    Last Post: November 14th, 01:28 PM
  2. Need help with an SQL statement - Newbie
    By alanenglefield@gmail.com in forum MySQL
    Replies: 0
    Last Post: April 21st, 03:09 PM
  3. If then statement / mysql query: NEWBIE
    By phatnugs420@comcast.net in forum PHP Development
    Replies: 1
    Last Post: August 11th, 10:18 AM
  4. IF Statement Problems
    By AJWP webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 5
    Last Post: February 3rd, 04:03 PM
  5. Help with statement and Schema - Newbie
    By Les in forum IBM DB2
    Replies: 3
    Last Post: October 21st, 04:48 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139