<?php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any buildings.");
}
else
{
$N = count($aDoor);
echo("Y开发者_Go百科ou selected $N door(s): ");
for($i=0; $i < $N; $i++)
{
echo($aDoor[$i] . " ");
}
}
?>
The script above will increment and then echo the extracted values of the variable array $aDoor, How can I modify to sum the values instead?
$total = 0
for($i=0; $i < $N; $i++)
{
$total += $aDoor[$i];
}
echo "Total: $total";
$sum = 0;
foreach($aDoor as $value){
$sum += $value;
}
echo 'Sum: ' . $sum;
You mean sum the first i elements of the $aDoor array?
something like this maybe:
$sum = 0;
for ( $i = 0; $i < $N; $i++ )
{
$sum += $aDoor[$i];
}
<?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): ");
}
?>
精彩评论