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

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 ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-04-2005
Simon Benson
 
Posts: n/a
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 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
Reply With Quote
  #2 (permalink)  
Old 02-04-2005
Oli Filth
 
Posts: n/a
Default Re: Newbie Question. Problems with IF statement - I think I'm goingmad!

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
Reply With Quote
  #3 (permalink)  
Old 02-04-2005
Colin McKinnon
 
Posts: n/a
Default Re: Newbie Question. Problems with IF statement - I think I'm going mad!

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.
Reply With Quote
  #4 (permalink)  
Old 02-04-2005
Dave Patton
 
Posts: n/a
Default Re: Newbie Question. Problems with IF statement - I think I'm going mad!

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/
Reply With Quote
  #5 (permalink)  
Old 02-04-2005
Simon Benson
 
Posts: n/a
Default Re: Newbie Question. Problems with IF statement - I think I'm goingmad!

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!
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:09 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0