Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:04:36+00:00 2026-06-15T11:04:36+00:00

Ive got the following snippet of code, which works and does what I need

  • 0

Ive got the following snippet of code, which works and does what I need it to do. The only problem is I think its a little ‘verbose’ and can do with some optimising. Can someone help me reduce the repetitiveness if at all possible. Most of the code is very similar…

Many thanks

<?php 

// condition = Less Than; More Than; Between
// noofweeks = this is the number of weeks chosen by the user

function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno,      $weekEndno, $basicprice, $supplementAmnt, $supplementType) {

if ($condition== "Between") {
// I need to get the start and end values as the data in this parameter should look like 1-17 
$betweenArray = explode('-',$conditionWeeks);
$startWeek = $betweenArray[0];
$endWeek = $betweenArray[1];
}


if(($condition == "Less Than") && ($noofweeks < $conditionWeeks) && ($supplementType ==    'Subtract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice -  $supplementAmnt; }

elseif(($condition == "Less Than") && ($noofweeks < $conditionWeeks) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice + $supplementAmnt; }

elseif(($condition == "More Than") && ($noofweeks > $conditionWeeks) && ($supplementType == 'Subtract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice - $supplementAmnt; }

elseif(($condition == "More Than") && ($noofweeks > $conditionWeeks) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice + $supplementAmnt; }    

elseif(($condition == "Between") && ($noofweeks >= $startWeek && $noofweeks <= $endWeek) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice + $supplementAmnt; }

elseif(($condition == "Between") && ($noofweeks >= $startWeek && $noofweeks <= $endWeek) && ($supplementType == 'Substract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) )  { return $basicprice - $supplementAmnt; }   

//if no conditions match, just return the unaltered basic price back

else { return $basicprice ;}

;} ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:04:38+00:00Added an answer on June 15, 2026 at 11:04 am

    That one list of ifs can be written as a nested if.

    The most important changes:

    1. ($weekno >= $weekStartno && $weekno <= $weekEndno) is part of all
      conditions, so it is in the outer if.
    2. The three varying conditions,
      that match with the given condition string are put in 1 if using or
      (||).
    3. The inner if is also singlified. Add results in an
      addition. Subtract in a subtraction.

    All comes down to repeating the conditions as less as possible, although you should keep in mind that the structure should still be clear. In some cases, it may be easier your way.

    <?php 
    
    // condition = Less Than; More Than; Between
    // noofweeks = this is the number of weeks chosen by the user
    
    function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno, $weekEndno, $basicprice, $supplementAmnt, $supplementType) {
      if ($condition == "Between") {
        // I need to get the start and end values as the data in this parameter should look like 1-17 
        $betweenArray = explode('-',$conditionWeeks);
        $startWeek = $betweenArray[0];
        $endWeek = $betweenArray[1];
      }
    
      if ($weekno >= $weekStartno && $weekno <= $weekEndno)
      {
        if (($condition == 'Less Than' && $noofweeks < $conditionWeeks) ||
            ($condition == 'More Than' && $noofweeks > $conditionWeeks) ||
            ($condition == 'Between' && $noofweeks >= $startWeek && $noofweeks <= $endWeek))
        {
          // You can use a 'switch' as well, instead of if...elseif.
          if ($supplementType == 'Subtract') {
            return $basicprice - $supplementAmnt;
          } elseif ($supplementType == 'Add' {
            return $basicprice + $supplementAmnt;
          }
        }
      }
      return $basicprice;
    } ?>
    

    In a different setup, I put the intermediate results in separate variables, which I think
    makes it even more readable. I added a little more comments here, explaining the decisions:

    function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno, $weekEndno, $basicprice, $supplementAmnt, $supplementType) {
    
      // Create two 'helper' booleans to make the if condition easier.
      $weekInRange = ($weekno >= $weekStartno && $weekno <= $weekEndno);
      $noOfWeeksInRange = 
          ($condition == 'Less Than' && $noofweeks < $conditionWeeks) ||
          ($condition == 'More Than' && $noofweeks > $conditionWeeks);
    
      // Alternatively, you can break the single line above up in multiple 
      // lines, which makes debugging easier:
      //$noOfWeeksInRange = ($condition == 'Less Than' && $noofweeks < $conditionWeeks);
      //$noOfWeeksInRange |= ($condition == 'More Than' && $noofweeks > $conditionWeeks);
    
      if ($condition == "Between") {
        // I need to get the start and end values as the data in this parameter should look like 1-17 
        $betweenArray = explode('-',$conditionWeeks);
        $startWeek = $betweenArray[0];
        $endWeek = $betweenArray[1];
        // Overwrite this variable with the condition that goes with 'Between'.
        // We're already in that if, so we don't need to check 'Condition' again..
        // You could use betweenArray[0] and [1] in the condition below, but using
        // the variables $startWeek and $endWeek does make it more readable.
        $noOfWeeksInRange = ($noofweeks >= $startWeek && $noofweeks <= $endWeek);
      }
    
      // And not this 'if' is even more readable.
      if ($weeksInRange && $noOfWeeksInRange)
      {
          // You can use a 'switch' as well, instead of if...elseif.
          if ($supplementType == 'Subtract') {
            return $basicprice - $supplementAmnt;
          } elseif ($supplementType == 'Add' {
            return $basicprice + $supplementAmnt;
          }
      }
      return $basicprice;
    } ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive got the following code which I hacked together from website examples. It works
I've got the following snippet of code and its all working, and bringing in
Ive got the following problem. I have a model called user which has a
All I've got is the following little snippet of code: <select size="1" name="EventHour<?php echo
I appear to have a peculiar problem. I've got the following code snippet and
Ive got a following problem. I made a XUL toolbar. Toolbar works correctly, it
I've got following code in my AsyncTask. The only thing I want the AsyncTask
I've got following problem: (c#) There is some class (IRC bot), which has method,
I'm programming C++ on Ubuntu, using QDBus and I've got the following code snippet:
I've got the following problem: My Flashlight app works fine on my Samsung Galaxy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.