Computer >> Computer tutorials >  >> Programming >> PHP

Manipulate for loop to run code with specific variables only PHP?


Let’s say the following is our array −

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

And we want the output like this with only selected values

45
67
89
68
98
57
77
60

Above, we multiplied marks less than 40 with 2, rest kept the entire array same.

Example

The PHP code is as follows

<!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>

Output

This will produce the following output

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