
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
<!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
Advertisements