使用php创建数字系列

i have a form which has two dates pickup and return date now i want to make charges based on number of days but i am stuck in loop.

i want to charge if days 1,2,3,4 than no charge for 5,6,7,8 than charge again for 9,10,11,12 than again no charge for 13,14,15,16 like this how to write code for this pattern,i have tried code but not wokring

So i can add charge alternatively.

if($rental_days%5==0 && $rental_days <10): 

           $charge = ($rental_days-1)*$result['rent'];
         elseif($rental_days%6==0):
          $charge = ($rental_days-2)*$result['rent'];
          elseif($rental_days%7==0):
         $charge = ($rental_days-3)*$result['rent'];

         elseif($rental_days%5==0 && $rental_days >8):  
             $charge = ($rental_days-3)*$result['rent']+$result['rent'];


        else:
         $charge = ($rental_days-4)*$result['rent']+$result['rent']; 

        endif;  

You're not looking at sets of four, but actually sets of 8... the first half of the 8 you charge, the 2nd half you don't, so you want to divide by 8 and see if the remainder is 4 or less...

if ( $rental_days % 8 <= 4 && $rental_days % 8 != 0 )

This will work no matter how many days we have (ex: what if it's been 300 days). You wouldn't want to use a loop because this could go on forever.

I will rephrase your question: you are looking for a function / algorithm that, given a number N, will return the following (making a little table here):

 N  return
 1     1
 2     2
 3     3
 4     4
 5     4
 6     4
 7     4
 8     4
 9     5
10     6

The following code demonstrates a simple way how you could do this - it only works when the same number of paid days (4) is followed by the same number of unpaid days (4):

<?php
for($n=1; $n<20; $n++)
  {
  $m = ($n-1)%4 + 1;               // returns 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4...
  $unpaid = intval(($n-1)/4)%2;    // returns 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,...
  $fourBlocks = intval(($n-1)/8);  // returns 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,...
  if ($unpaid == 1)
    {
    $daysCharged = $n - $m - 4 * $fourBlocks;
    }
  else
    {
    $daysCharged = $n - 4 * $fourBlocks;
    }
  echo $n . " days: " . $daysCharged . " days charged, ". ($n - $daysCharged) . " free
";
  }
?>

This produces the following output:

1 days: 1 days charged, 0 free
2 days: 2 days charged, 0 free
3 days: 3 days charged, 0 free
4 days: 4 days charged, 0 free
5 days: 4 days charged, 1 free
6 days: 4 days charged, 2 free
7 days: 4 days charged, 3 free
8 days: 4 days charged, 4 free
9 days: 5 days charged, 4 free
10 days: 6 days charged, 4 free
11 days: 7 days charged, 4 free
12 days: 8 days charged, 4 free
13 days: 8 days charged, 5 free
14 days: 8 days charged, 6 free
15 days: 8 days charged, 7 free
16 days: 8 days charged, 8 free
17 days: 9 days charged, 8 free
18 days: 10 days charged, 8 free
19 days: 11 days charged, 8 free

Note - in a more general case where you have N days charged followed by M days not charged, you can change the code as follows:

<?php
$N = 4;
$M = 4;
echo $N . " days charged followed by " . $M . " days free:
";
for($days=1; $days<20; $days++)
  {
  $numBlocks = intval(($days - 1) / ($N + $M)); // a "block" is a complete cycle of paid & unpaid days
  $remainder = $days - ($N + $M) * $numBlocks; // the "remainder" contains an incomplete cycle
  $unpaid = $remainder - $N;
  if ($unpaid < 0) $unpaid = 0;  // this now contains the number of unpaid days in the incomplete cycle
  $daysCharged = $numBlocks * $N + $remainder - $unpaid;  // easy to compute the number that must be charged
  echo $days . " days: " . $daysCharged . " days charged, ". ($days - $daysCharged) . " free
";
  }
?>

This is actually much more robust, and will give you the right answer regardless of the value of M and N (so you can change the charging scheme quite easily).

Here is solution for you, this is what you exactly want. Checkout this, hope it will work for you..

<?php
    $charge = $counter = 40;
    $switch = 0;
    for($i=1;$i<=31;$i++)
    {
        if($switch == 0)
        {
            echo $i." => Charge = ".$charge."</br>";
            $charge = $charge+$counter;
        }
        else if($switch == 1)
        {
            echo $i." => NoCharge = ".$charge."</br>";
        }
        if($i%4 == 0)
        {
            if($switch == 0)
            {
                $switch = 1;
                if($i%8 == 0)
                {
                    $charge = $charge+$counter;
                }
            }
            else if($switch == 1)
            {
                $switch = 0;
                if($i%8 == 0)
                {
                    $charge = $charge+$counter;
                }
            }
        }
    }
 ?>

Here is output

1 => Charge = 40
2 => Charge = 80  
3 => Charge = 120
4 => Charge = 160
5 => NoCharge = 200
6 => NoCharge = 200
7 => NoCharge = 200
8 => NoCharge = 200
9 => Charge = 240
10 => Charge = 280
11 => Charge = 320
12 => Charge = 360
13 => NoCharge = 400
14 => NoCharge = 400
15 => NoCharge = 400
16 => NoCharge = 400
17 => Charge = 440
18 => Charge = 480
19 => Charge = 520
20 => Charge = 560
21 => NoCharge = 600
22 => NoCharge = 600
23 => NoCharge = 600
24 => NoCharge = 600
25 => Charge = 640
26 => Charge = 680
27 => Charge = 720
28 => Charge = 760
29 => NoCharge = 800
30 => NoCharge = 800
31 => NoCharge = 800