PHP程序设计-实验1-简单PHP代码入门

计算1+3+5+7+……+99=?

<?php
  $result = 0;
for ($i = 1; $i <= 99; $i += 2) {
    
    
  $result += $i;
}

echo "1+3+5+7+...+99 = $result";
?>

输出结果:1+3+5+7+...+99 = 2500

已知$arr=(2,35,123,43,12345,321,231,6658,3257,12);将该数组从小到大排序,并输出排序结果count():检测数组元素数量

<?php
$arr = array(2,35,123,43,12345,321,231,6658,3257,12);
sort($arr);
for ($i=0; $i<count($arr); $i++) {
    
    
    echo $arr[$i]." ";
}
?>

输出结果:2 12 35 43 123 231 321 3257 6658 12345

计算并输出1+1/2+1/3+1/4+……+1/100=?

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

输出结果:5.1873775176396

猜你喜欢

转载自blog.csdn.net/weixin_44893902/article/details/129805566
今日推荐