PHP 循环 1 到 10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40716464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:14:10  来源:igfitidea点击:

PHP Loop 1 to 10

phploopsfor-loop

提问by Jelles

I need to make a loop in php that does 1 + 2 + 3 + 4 .... + 10 = 55 but icant get it to work. I did this:

我需要在 php 中创建一个循环,执行 1 + 2 + 3 + 4 .... + 10 = 55 但不能让它工作。我这样做了:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php 
        for ($i = 1; $i <= 10; $i++){
            $sul = $i + $i + $i + $i + $i + $i + $i + $i + $i + $i;
            echo "$i + $i + $i + $i + $i + $i + $i + $i + $i + $i = $sul<br>";

        };

    ?>
</body>

Hope you can help me thanks :)

希望你能帮助我谢谢:)

回答by Karol Gasienica

This code should help you:

此代码应该可以帮助您:

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

You are incorrect using loop.

您使用循环不正确。

Explanation

解释

I think it will be easier to understand with following table:

我认为使用下表会更容易理解:

_____________________
|JUMP | $i   | $sum  |
|1    | 1    | 1     |
|2    | 2    | 3     |
|3    | 3    | 6     |
|4    | 4    | 10    |
|5    | 5    | 15    |
|6    | 6    | 21    |
|7    | 7    | 28    |
|8    | 8    | 36    |
|9    | 9    | 45    |
|10   | 10   | 55    |

More about foryou can read in PHP: for

更多关于for你可以在PHP 中阅读的内容:for

Update

更新

If you want your structure it can be as follows:

如果你想要你的结构,它可以如下:

<?php
$sum = 0;
$str = '';
for($i = 1; $i<=10; $i++) {
    $sum = $sum + $i;
    $str .= $i == 10 ? $i." = " : $i." + ";
}
echo $str.$sum;
?>

And it will output 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

它会输出 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

回答by Jonnix

Something like this perhaps?

也许是这样的?

$range = range(1, 10);
$sum = array_sum($range);

echo implode(' + ', $range) . ' = ' . $sum;

range() - http://php.net/range

范围() - http://php.net/range

array_sum() - http://php.net/array_sum

array_sum() - http://php.net/array_sum

implode() - http://php.net/implode

implode() - http://php.net/implode

回答by Murad Hasan

Just make a for loop from starting 1 to 10, like given below. You need to initialize the counter as 0 and while the loop executes you need to collect / sum them to the counter and finally outside the loop print/ echo the counter.

只需从开始 1 到 10 进行 for 循环,如下所示。您需要将计数器初始化为 0,并且在循环执行时您需要将它们收集/求和到计数器,最后在循环之外打印/回显计数器。

$count = 0;
$string = '';
for ($i = 1; $i <= 10; $i++){
    $count += $i;

    $string .= ($i == 10) ? $i : $i." + ";
}
echo $string." = ".$count; // 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

This is a very simple and straight forward way, you can do this using some array functions which is built-in in PHP.

这是一种非常简单直接的方法,您可以使用 PHP 内置的一些数组函数来完成此操作。

回答by Ronald

If you don't want to hardcode it, you can do this

如果你不想硬编码它,你可以这样做

<?php
$answer = 0;    
for ($i = 1; $i <= 10; $i++){
         $answer = $i + $answer;
         if ($i == 10) {
           echo $i." = ".$answer;
         }
         else {
           echo $i." + ";
         }       
    };
?>

Output is:

输出是:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

回答by Mauricio Irace

A for loop actually repeats a block of code n times, you syntax is right buy your semantic is wrong: initializes a counter in 0 and add i to the counter in each step as the other people say. Also, if is not compulsory to use a for, remember that the sum of first n natural numbers is n(n+1)/2, so no loop is actually needed

for 循环实际上将代码块重复 n 次,您的语法是正确的,但您的语义是错误的:在 0 中初始化一个计数器并在每个步骤中将 i 添加到其他人所说的计数器中。另外,如果不是强制使用for,记住前n个自然数的和是n(n+1)/2,所以实际上不需要循环

回答by d3p4n5hu

There you go:

你去吧:

$sum = 0;
for($i = 1; $i <= 10; $i++){
    if($i == 10){
        echo $i;
    } else {
        echo $i." + ";
    }
    $sum = $sum + $i;
}
echo " = ".$sum;

回答by user2693928

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
        $n = 10;
        $items = range(1, $n);
        $sum = array_sum($items);
        echo implode('+', $items) . ' = ' . $sum;
    ?>
</body>

This way is shorter. You generate the array from 1 to $n(10);

这种方式更短。您生成从 1 到 $n(10) 的数组;

Then you calculate the sum of the items.

然后你计算项目的总和。

And then join each element with '+' and add the sum.

然后用“+”连接每个元素并添加总和。