操纵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