Newbie question on ELSE statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imimin
Forum Commoner
Posts: 38
Joined: Thu Oct 18, 2007 5:44 pm

Newbie question on ELSE statement

Post 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]
User avatar
gregwhitworth
Forum Commoner
Posts: 53
Joined: Tue Oct 09, 2007 1:00 am
Location: Wasilla, Alaska

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
Post Reply