How do I sum the items in a php loop? - php

I've been learning PHP for a few days and I have an embarrassingly easy question. I want to sum the items in a 500 item loop. I want to figure out the total of 500 + 499 + 498, etc.
Here's my code:
for ($i=1; $i<=500; $i++)
{
// echo $i . "<br />";
$total = 0;
$total = $total + $i;
return $total;
}
echo $total . "<br />";
?>
Can't figure out what I'm doing wrong.

Pull out the initialization and the return statement from the loop:
$total = 0;
for ($i=1; $i<=500; $i++) {
echo $i . "";
$total = $total + $i;
}
echo $total . "";
return $total;

You can also do
echo array_sum(range(0,500)); // 125250
or do the entire calculation without a for body:
for( $total = $i = 0; $i <= 500; $total += $i++ );
echo $total; // 125250
and a couple of other approaches (Daniel's solution is particularly nice).
Both of the above are equivalent to what you are likely looking for
$total = 0;
for ($i=1; $i<=500; $i++) {
$total = $total + $i;
}
echo $total;
Like already pointed out elsewhere, when you do $total = 0; inside the for loop, you will overwrite the previous value for $total and putting return into it will end your script unless the loop is inside a function.

It looks like you simply need to move the $total = 0; line out of your loop... Otherwise it will be set to 0 on each iteration.
You also need to move that return $total; line outside, as #Webnet noted in a comment below.
Also note that you can do that calculation in constant time without iterating through all the numbers, by finding the sum of an arithmetic progression:
Sn = 1/2n(a1 + a2)
Sn = 250(1 + 500)
= 125250

Your code should be...
$total = 0;
for ($i=1; $i<=500; $i++) {
// echo $i . "<br />";
$total = $total + $i;
}
echo $total . "<br />";
return $total; will break the loop

Well, you're resetting $total to zero every time, and using return outside of a statement...
Anyways, the answer is (500*501)/2 = 250*501 = 125250 (basic maths)

You have two errors:
First, you set the $total to 0 each time the loop is repeated.
Then, you use return, what happens here is that it will cancel further execution, so the loop basically just was run once.
Try this.
$total = 0;
for ($i=1; $i<=500; $i++){
$total = $total + $i;
}
echo $total;

$total = 0;
for ($i=1; $i<=500; $i++) {
echo $i . "";
$total = $total + $i;
}
echo $total . "";
return $total;

Related

How I can get average number from array without using array_sum or any other function?

I had a task to write program that gives 20 numbers from 9 to 99 using empty array and rand function which I did...but the second step is for me to calculate and get the average number.
With functions like array_sum I can get what I want, but the idea is not to use any of array functions, just arithmetic operators
$arrNums = array();
$sum = 0;
$intTotalNum = 20;
for($i = 0; $i < $intTotalNum; $i++) {
$intRand = rand(9, 99);
$arrNums[] = $intRand;
$sum = $sum + $arrNums;
$average = $sum / count($arrNums);
}
var_dump($arrNums);
echo "<br>";
echo $average;
Code gives me an error "Unsupported operand types"
This is how you can achieve it without using an array functions:
$arrNums = array();
$sum = 0;
$intTotalNum = 20;
for($i = 0; $i < $intTotalNum; $i++)
{
$intRand = rand(9, 99);
array_push($arrNums,$intRand);
$sum = $sum+$intRand;
}
echo '<pre>';
print_r($arrNums);
echo "<br>";
echo $sum;
//echo $average;
Just add your $sum to current $intRand and print it outside the loop.
The reason why I have used array_push functions so that you can print all the array elements outside for loop and check and verify how many numbers generated. If you don't want to print an array then it is no need, you can comment or remove it.
Just put the $average out side the for loop
$arrNums = array();
$sum = 0;
$intTotalNum = 20;
for($i = 0; $i < $intTotalNum; $i++) {
$intRand = rand(9, 99);
$arrNums[] = $intRand;
$sum += $intRand;
}
$average = $sum / $intTotalNum;
var_dump($arrNums);
echo "<br>";
echo $average;
You are trying add a integer with an array that's why the error "Unsupported operand types" . you can get the average more easily like this
$sum = 0;
$intTotalNum = 20;
$arrNums = array();
for($i = 0; $i < $intTotalNum; $i++) {
$arrNums[] = rand(9, 99);
$sum = $sum + $arrNums[$i];
}
$average = $sum / $intTotalNum;
var_dump($arrNums);
echo "<br>";
echo $average;

Running for loop inside other for loop

Can anyone help me understand why a variable takes its initial value after incrementing the variable? below is code:
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
echo $k+3;
echo $l+3;
}
In this we have two for loops running one inside other. Here we run three times the outside for loop, inside this we are running other for loop again. The problem we are facing is that when inner for loop end we have incremented $k and $l both by 3 but it always take value 0 and 3 respectively.
we have incremented $k and $l both by 3
Nope, you only print the result of your values plus 3, but you do not set them anywhere in the loop:
Instead of
echo $k+3;
echo $l+3;
write
$k = $k + 3;
$l = $l + 3;
You should try removing the "echo" and incrementing the variable in each loop. Then print them out after.
Try this:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
$j = $j++;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
?>
Gives you:
9
12
TRY this.
$k += 3;
$l += 3;
echo $k;
echo $l;
Try This
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
First Increment the value and store it in the variable
$k = $k+3;
$l = $l+3;
Then You need to print using
echo $k.'<br>';
echo $l;
#Harinarayan First of all you need to read about echo() http://php.net/manual/en/function.echo.php
echo — Output one or more strings
echo does not manipulate expression as you did in your question like:
echo $k+3;
instead of using echo for the increment you should first increment the variable and then echo that variable like below:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k += 3;
$l += 3;
echo $k;
echo "<br>";
echo $l;
}

How do I add together Random Integers from a for loop with PHP

I was messing around with PHP and I figured out how to list 5 random integers between 1 and 100 using a for loop:
for($row = 1; $row <= 5; $row++) {
echo rand(1,100) . "<br>";
}
I know that I can get the sum of random numbers by doing the following:
$sum = 0;
for($row = 1; $row <= 5; $row++) {
$sum += rand (1,100) . "<br>";
}
echo $sum;
But now I don't know how I can echo the rands to be seen as well.
I want to be able to somehow combine the first piece of code and the second to get the list of random integers and their sum.
If you're planning to reuse the data, store them in a variable.
You should be looking into array's here:
for($sum = 0, $row = 1; $row <= 5; $row++) {
$num = rand (1,100);
$nums[] = $num;
$sum += $num;
}
echo "the total sum is: '$sum' with the values of: '" . implode(', ', $nums) . "'.";
Or just loop through it:
foreach($nums as $value){
echo $value, '<br>';
}
This is what I ended up using, and it worked! Thanks!
<?php
for($row = 1; $row <= 5; $row++) {
$value = rand (1,100);
echo $value . "<br>";
$sum += $value;
}
echo $sum;
?>

how to print like 1,2,5,10,17,26,37,50,65

In a small project I would link to print 1,2,5,10,17,26,37,50,65.This number is increased by an odd number like 1,3,5,7,9,11,13.
I've been unable to find any way to print.
any suggest?
$counter = 0;
$maxCount = 1000;
$sum = 0;
while($counter <=$maxCount)
{
if ($counter % 2 != 0)
{
$sum += $counter;
}
echo $sum . "<br>";
$counter++;
}
You can do this using while() loop,
$start = $interva1 = 1;
$maxCount = 100;
while($start < $maxCount){
echo $start . " ";
$start += $interva1;
$interva1 += 2;
}
$start is the number you want to start the sequence with
$interval is the odd number to be added in each iteration of the loop
This can be done by using a for loop and increment by 2. If you start on an odd number as gap, the gap will always stay an odd number.
$sum = 1;
$maxGap = 1000;
for ($gap = 1; $gap <= $maxGap; $gap += 2) {
echo $sum . "<br />";
$sum += $gap;
}

how to sum values inside for loop

I try to get the summtion of the values inside a for loop but i couldn't do that
like this code:
for($i=1;$i<=($value);$i++){
if(isset($_POST['submit'])){
$total=$max[1]+$max[2]+......+$max[$i];
echo $total;
}
}
When i press submit it gave me that there's a mistake in line 2
that there is an unexpected '.' and i couldn't find any solution to solve this problem
i hope that my problem is clear for you
can any one help me please :)
if ( isset($_POST['submit']) ){
$total = 0;
for( $i = 1; $i <= $value; $i++ ){
$total += $max[$i];
}
echo $total;
}
if(isset($_POST['submit'])){
$total = 0;
for($i=1; $i <= $value; $i++){
$total += $max[$i];
}
echo $total;
}

Categories