如何修改此脚本以添加而不是递增

时间:2010-02-10 16:35:17

标签: php

<?php
  $aDoor = $_POST['formDoor'];
  if(empty($aDoor)) 
  {
    echo("You didn't select any buildings.");
  } 
  else 
  {
    $N = count($aDoor);

    echo("You selected $N door(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($aDoor[$i] . " ");
    }
  }
?>

上面的脚本将递增,然后回显变量数组$ aDoor的提取值,如何修改以对值进行求和?

4 个答案:

答案 0 :(得分:1)

$total = 0
for($i=0; $i < $N; $i++)
{
  $total += $aDoor[$i];
}
echo "Total: $total";

答案 1 :(得分:0)

你的意思是$ aDoor数组的前i个元素的总和?

这样的事情可能是:

$sum = 0;
for ( $i = 0; $i < $N; $i++ )
{
  $sum += $aDoor[$i];
}

答案 2 :(得分:0)

$sum = 0;
foreach($aDoor as $value){
    $sum += $value;
}
echo 'Sum: ' . $sum;

答案 3 :(得分:0)

<?php
  $aDoor = $_POST['formDoor'];
  if(empty($aDoor)) 
  {
    echo("You didn't select any buildings.");
  } 
  else 
  {
    $N = count($aDoor);

    //echo("You selected $N door(s): ");
    var $iDoors = 0;
    for($i=0; $i < $N; $i++)
    {
      //echo($aDoor[$i] . " ");
      $iDoors += $aDoor[$i];
    }
    echo("You selected $iDoors door(s): ");
  }
?>