• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

PHP help

Hale88

New Member
Joined
Sep 4, 2009
Messages
153 (0.03/day)
Location
Somewhere in FL
I am learning While...loops... and I have got everything so far except for one.

How can I do SUM in while...loops statement?

Ex: I have a number call: $number = 1, I know how to list from 1-10

but I don't know how to SUM them all together to get a Total of 55 (1+2+3...+10 = 55)?

help me please!
thank you
hale
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Code:
$number = 1;
$total = 0;
while ($number <= 10)
{
  $total += $number;
  $number++;
}
Not tested but should work. FYI, a for loop is better suited to this than a while loop.
 
Last edited:

Hale88

New Member
Joined
Sep 4, 2009
Messages
153 (0.03/day)
Location
Somewhere in FL
Hey I got it work:

print "Use While loop to calculate the sum: 1-10:";
print "<br/>";
$number = 1;
$total = 0;

for ($number =1; $number <= 10; $number++)
{
print "$number <br/>" ;

$total += $number;
}
Print "The Total is :$total";


?>

thank for helping
 
Last edited:
Top