封装 2018-11-23 12:02

求和:

<?php
header('Content-type:text/html;charset=utf-8');

// 求和
function get_sum($end_number=10){
	$sum=0;
	for ($i=1; $i <=$end_number ; $i++) { 
		// $sum=$sum+$i;
		$sum+=$i;
	}
	return $sum;
}

$end_number=5;
$sum=get_sum($end_number);
echo "从1加到{$end_number}={$sum}<br />";



<?php
header('Content-type:text/html;charset=utf-8');

// 求和
function get_sum($end_number=10){
	$sum=0;
	for ($i=1; $i <=$end_number ; $i++) { 
		// $sum=$sum+$i;
		$sum+=$i;
	}
	return $sum;
}

$end_number=5;
$sum=get_sum($end_number);
echo "从1加到{$end_number}={$sum}<br />";

$end_number=10;
$sum=get_sum($end_number);
echo "从1加到{$end_number}={$sum}<br />";

$end_number=36;
$sum=get_sum($end_number);
echo "从1加到{$end_number}={$sum}<br />";

$end_number=100;
$sum=get_sum($end_number);
echo "从1加到{$end_number}={$sum}<br />";