Skip to content
Advertisement

Unable to do sum and getting same value by using while loop php

I’m working on project where I’m preparing a large array set to create a function that will save the value into the database one by one.

So that I created a loop to generate the array as plain text format that I need

$i = 1;
while( $i <= 65 ) {
  $startVal = $this->summe(35501, 500);
  $endVal = $this->summe(36000, 500);
  $amount = $this->summe(3575, 50);
  echo "array(<br>";
    echo "'low'     => '".$startVal."',<br>";
    echo "'high'    => '".$endVal."',<br>";
    echo "'amount'  => '".$amount."',<br>";
    echo ''city'    => $city<br>';
  echo "),<br>";
  $i++;
}

But there is a small issue that whenever I run the code, every time loop return same value:

array(
'low' => '36001',
'high' => '36500',
'amount' => '3625',
'city' => $city
),
array(
'low' => '36001',
'high' => '36500',
'amount' => '3625',
'city' => $city
),
array(
'low' => '36001',
'high' => '36500',
'amount' => '3625',
'city' => $city
),
array(
'low' => '36001',
'high' => '36500',
'amount' => '3625',
'city' => $city
),

But I need to generate output like following format:

array(
'low' => '36001', 
'high' => '36500',
'amount' => '3625',
'city' => $city
),
array(
'low' => '36501', // Adding 500
'high' => '37000', // Adding 500
'amount' => '3675', // Adding 50
'city' => $city
),
array(
'low' => '37001', // Adding 500
'high' => '37500', // Adding 500
'amount' => '3725', // Adding 50
'city' => $city
),
array(
'low' => '37501', // Adding 500
'high' => '38000', // Adding 500
'amount' => '3775', // Adding 50
'city' => $city
),

Advertisement

Answer

You’re feeding the same hard-coded values to the summe function each time, so it’s unsurprising that you get the same results each time. (I assume that function just adds the two numbers together, although you didn’t show the code for it.)

If you want it to increment each time, then you need to keep a record of the value returned on the previous loop, and use that instead. Something like this:

$i = 1;
$startVal = 35501;
$endVal = 36000;
$amount = 3575;

while( $i <= 65 ) {
  $startVal = $this->summe($startVal, 500);
  $endVal = $this->summe($endVal, 500);
  $amount = $this->summe($amount, 50);
  echo "array(<br>";
    echo "'low'     => '".$startVal."',<br>";
    echo "'high'    => '".$endVal."',<br>";
    echo "'amount'  => '".$amount."',<br>";
    echo ''city'    => $city<br>';
  echo "),<br>";
  $i++;
}

Demo: http://sandbox.onlinephpfunctions.com/code/e8bcca803d8cac56b85d5ac7841d18250aca11e3

Also the summe function is redundant, you don’t need to have a wrapper just for doing a trivial addition. Without the unncessary overhead of a function call, you could write the same 3 lines as

$startVal += 500;
$endVal += 500;
$amount += 50;

instead.


P.S. As an aside, you appear to be trying to use PHP to generate string output similar to that produced by print_r() – or perhaps more like var_export(). It’s not clear why you’re doing that rather than just using one of those debugging functions directly, or (if debugging isn’t the objective) outputting to a recognised, standard, parseable format such as JSON or XML.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement