How to add 1 to 100 using PHP

WBOY
Release: 2023-03-13 21:20:02
Original
8498 people have browsed it

In PHP, you can use the for loop statement to implement the function of adding 1 to 100. The for loop can loop and execute the specified code block under specified conditions. The syntax is "for($i=1;$i< ;=100;$i ){$sum =$i;}”.

How to add 1 to 100 using PHP

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Use PHP to implement 1 Method of adding to 100

In PHP, you can use the for loop statement to achieve the accumulation of numbers. If you have determined the number of times the script will run in advance, you can use the for loop.

The syntax is as follows:

for (init counter; test counter; increment counter) {
  code to be executed;
}
Copy after login

What needs to be noted is:

  • init counter: initialize the value of the loop counter

  • test counter: Evaluated for each loop iteration. If the value is TRUE, continue looping. If its value is FALSE, the loop ends.

  • increment counter: increase the value of the loop counter

The example is as follows:

<?php //1-100利用for循环1-100累加
 $sum=0;//初始化sum值为0
for($i=1;$i<=100;$i++){//定义i,循环次数,一般求1-100的和,从1开始一直加到100
          $sum+=$i;//每次累加传给sum,然后i循环,直道100。
}
echo "1-100之间累加的和为".$sum;
?>
Copy after login

Output result:

How to add 1 to 100 using PHP

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of How to add 1 to 100 using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!