This is a discussion on Newbie Question. Problems with IF statement - I think I'm goingmad! within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi and HELP! I'm new to PHP/Perl and having a small problem. The following statement doesn't return ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 parsed, 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 Benson wrote:
> 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 parsed, I still get the result as if country=GB. 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 |
|
|||
|
Simon Benson wrote:
> > I'm new to PHP/Perl and having a small problem. > <snip> > > if($INPUT::country = "GB") { 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. |
|
|||
|
Simon Benson <simonb22@hotmail.com> wrote in
news:tQNMd.353$hQ1.282@newsfe1-win.ntli.net: > 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)); http://ca.php.net/manual/en/language...assignment.php http://ca.php.net/manual/en/language...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 Patton wrote:
> Simon Benson <simonb22@hotmail.com> wrote in > news:tQNMd.353$hQ1.282@newsfe1-win.ntli.net: > > >>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)); > > > http://ca.php.net/manual/en/language...assignment.php > http://ca.php.net/manual/en/language...comparison.php > (Hint - what's the difference between "=" and "=="?) > Thanks guys, despite now being 'at home' I'm going to give it a try now. Then I might get some sleep tonight! |