- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 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
- Related Articles
- Multiple index variables in PHP foreach loop
- How to run MongoDB query to update only a specific field value?
- How do you run your own code alongside Tkinter's event loop?
- How to run an infinite loop in Tkinter?
- How to run Python code on Google Colaboratory?
- Join every element of an array with a specific character using for loop in JavaScript
- PHP foreach Loop.
- PHP preg_split to split a string with specific values?
- MongoDB Query to search for records only in a specific hour?
- How to use JavaScript to set cookies for a specific page only?
- How can I break an outer loop with PHP?
- How to count values from a PHP array and show value only once in a foreach loop?
- PHP Predefined Variables
- PHP variable Variables
- JavaScript variables declare outside or inside loop?

Advertisements