Write a PHP program to sum 1 to 10 numbers using while loop PHP:

Here this the program 

//Program:

<?php
$sum = 0;
$n = 10;
$i=1;
while($i<=$n)
{
$sum += $i;
$i++;
}
echo $sum;
?>


In this program, we take three variables named $sum,$n, and $i. $sum variable store the sum of numbers and $n viable used to store that number to sum and same $i use as a counter that counts the number and at the start, you can initialize the value which you want to start the addition of number.

for example:
if we want to sum 1 to 10 then we initialise the value as,
$i=0;
$n=10;

when $i got equal to $n then while loop will exit and print the sum of 1 to 10.

$sum += $i;
Read this: know about += operator

if you need any help then you can mention in the comment section.

Post a Comment

Previous Post Next Post