仅对特定变量运行代码的 for 循环 PHP?
不妨假设以下内容为我们的数组 −
$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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP