如何在 PHP 中操作 for 循环来运行仅包含特定变量的代码?

phpserver side programmingprogramming更新于 2025/7/17 5:07:17

假设我们的数组减法如下

$marks=[45,67,89,34,98,57,77,30];

我们希望输出结果像这样,只包含选定的值

45
67
89
68
98
57
77
60

上面,我们将小于 40 的分数乘以 2,其余部分保持不变。

示例

PHP 代码如下

<!DOCTYPE html>
<html>
<body>
<?php
   $marks=[45,67,89,34,98,57,77,30];
   echo "The Marks are as follows","<br>";
   foreach($marks as $tempMarks){
      if($tempMarks < 40){
         $tempMarks=$tempMarks*2;
      }
      echo $tempMarks,"<br>";
   }
?>
</body>
</html>

输出

这将产生以下输出 −

The Marks are as follows
45
67
89
68
98
57
77
60

相关文章