graham23s Posted October 25, 2008 Share Posted October 25, 2008 Hi Guys, i have writtena basic function to return shipping costs: <?php function calculate_weight_and_postage($customers_country, $sum_weight) { // the 2 arguments we are taking are the customers country and the sum weight of the order print $sum_weight; // UK if ($customers_country == "United Kingdom") { // sub if if ($sum_weight > 0.00 || $sum_weight < 0.10) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.10 || $sum_weight < 0.20) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.20 || $sum_weight < 0.30) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.30 || $sum_weight < 0.40) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.40 || $sum_weight < 0.50) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.50 || $sum_weight < 0.60) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.60 || $sum_weight < 0.70) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.70 || $sum_weight < 0.80) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.80 || $sum_weight < 0.90) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 0.90 || $sum_weight < 1.00) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 1.00 || $sum_weight < 1.10) { $weightPostage = array("2.95" => "Standard - £2.95 incl VAT"); // return us the weight and postage to use return $weightPostage; } else { $weightPostage = array("3.95" => "Standard - £3.95 incl VAT"); return $weightPostage; } return $weightPostage; } // UK } ?> the weight sum is currently 1.07 so should return the 2.95 price but it seems to be only returning 1.50, just wondering if my logic is correct here thanks guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/130072-elseif-in-function/ Share on other sites More sharing options...
wildteen88 Posted October 25, 2008 Share Posted October 25, 2008 Wouldn't you be better of merging all the if/elseif's for the $sum_weight being 0.00 to 1.00 into just one? Seems pointless having 8 or so conditions that return the same result. Also rather than use || (or) use && (and) <?php function calculate_weight_and_postage($customers_country, $sum_weight) { // the 2 arguments we are taking are the customers country and the sum weight of the order print $sum_weight; // UK if ($customers_country == "United Kingdom") { // sub if if ($sum_weight > 0.00 && $sum_weight < 1.00) { $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); // return us the weight and postage to use return $weightPostage; } elseif ($sum_weight > 1.00 && $sum_weight < 1.10) { $weightPostage = array("2.95" => "Standard - £2.95 incl VAT"); // return us the weight and postage to use return $weightPostage; } else { $weightPostage = array("3.95" => "Standard - £3.95 incl VAT"); return $weightPostage; } return $weightPostage; } // UK } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130072-elseif-in-function/#findComment-674430 Share on other sites More sharing options...
kenrbnsn Posted October 25, 2008 Share Posted October 25, 2008 Also, it's better to have one return statement in a function. <?php function calculate_weight_and_postage($customers_country, $sum_weight) { // the 2 arguments we are taking are the customers country and the sum weight of the order print $sum_weight; $weightPostage = array(); // UK if ($customers_country == "United Kingdom") { // sub if if ($sum_weight > 0.00 && $sum_weight < 1.00) $weightPostage = array("1.50" => "Standard - £1.50 incl VAT"); elseif ($sum_weight > 1.00 || $sum_weight < 1.10) $weightPostage = array("2.95" => "Standard - £2.95 incl VAT"); else $weightPostage = array("3.95" => "Standard - £3.95 incl VAT"); } // UK return $weightPostage; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/130072-elseif-in-function/#findComment-674433 Share on other sites More sharing options...
graham23s Posted October 25, 2008 Author Share Posted October 25, 2008 ah brill! thanks a lot guys Graham Quote Link to comment https://forums.phpfreaks.com/topic/130072-elseif-in-function/#findComment-674434 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.