Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Manipulate For Loop to Run Code with Specific Variables in 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

 Live Demo

<!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
Updated on: 2020-10-13T07:05:02+05:30

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements