Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/130072-elseif-in-function/
Share on other sites

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
}

?>

Link to comment
https://forums.phpfreaks.com/topic/130072-elseif-in-function/#findComment-674430
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/130072-elseif-in-function/#findComment-674433
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.