Page 1 of 1

Newbie question on ELSE statement

Posted: Fri Oct 19, 2007 7:34 pm
by imimin
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Can someone please look at my code and tell me what I am doing wrong? I am fairly new at programming (a fascinating science!), but on this I am sure everything looks correct, but for some reason, it does not seem to work like it supposed to. First, let me give you an example of the code that I think should be working right, and then I will explain what is going on. Keep in mind I am new at this stuff and would appreciate any good advice I can get from you experts.

Here is a sample of the code I am dealing with:

Code: Select all

$exclude_gnd=1; if(strpos($methodName, '2nd') !== false and $exclude_gnd!==0){ if($twoDayZone != '-'){ $baseShippingCost = us_get_cost_for_lbs_and_zone('ups_commus48_2da', $evalWeight, $twoDayZone); $shipDiscount = $shippingDiscounts['second']; }/*elseif($twoDayZone = '-'){$baseShippingCost = 0;}*/ else {$baseShippingCost = 0;} }

My present problem is that the ELSE statements at the end of end of the sample does not work (i.e. else{$baseShippingCost = 0;}), it seems to be ignored if the expression evaluates to FALSE. Everything else seems to work correctly.

I would appreciate any help I can get on this !

THANK YOU!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Oct 19, 2007 8:44 pm
by gregwhitworth
can you post a test where you are trying to achieve your 'else' statement and put the following right after the else statement:

echo $baseShippingCost;

And post that - because then we can see what is happening - along with any errors.

Posted: Mon Oct 22, 2007 4:27 pm
by feyd
I'd recommend a more "proper" formatting of code. Example:

Code: Select all

if ( $foo )
{
  // foo stuff
}
elseif ( $bar )
{
  // bar stuff
}
else
{
  // other stuff
}

Posted: Mon Oct 22, 2007 6:30 pm
by RobertGonzalez
Here is your code in easier to read format:

Code: Select all

<?php
$exclude_gnd=1; 
if(strpos($methodName, '2nd') !== false and $exclude_gnd!==0){ 
  if($twoDayZone != '-'){ 
    $baseShippingCost = us_get_cost_for_lbs_and_zone('ups_commus48_2da', $evalWeight, $twoDayZone); 
    $shipDiscount = $shippingDiscounts['second']; 
  }/*elseif($twoDayZone = '-'){$baseShippingCost = 0;}*/ else {
    $baseShippingCost = 0;
  } 
} 
?>
Are you sure we are talking about the same conditional that is not evaluating properly?