- 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
The internal working of the ‘foreach’ loop in PHP
The ‘foreach’ loop in PHP helps in accessing key value pairs within an array. The ‘foreach’ loop works with arrays only, with the advantage that a loop counter wouldn’t need to be initialized. In addition to this, no condition needs to be set that would be needed to exit out of the loop. The ‘foreach’ loop implicitly does this too.
Example
<?php $my_arr = array("Joe", "Hannah", "Paul", "Sanna"); foreach($my_arr as $entry) { echo "$entry<br/>"; } ?>
Output
Joe Hannah Paul Sanna
An array of string values is defined and the foreach loop is used to access every element inside the array by giving a variable name. The ‘echo’ is used to display every element of the array on the console.
- Related Articles
- PHP foreach Loop.
- Multiple index variables in PHP foreach loop
- Stripping last comma from a foreach loop in PHP?
- PHP Casting Variable as Object type in foreach Loop
- Internal working of the list in Python
- foreach Loop in C#
- How does the Java “foreach” loop work?
- Internal working of Python
- How to use the foreach loop parallelly in PowerShell?
- Internal working of Set in Python
- Internal Working of HashMap in Java
- Using foreach loop in arrays in C#
- Internal working of Set/HashSet in Java
- Iterating C# StringBuilder in a foreach loop
- Implement MongoDB toLowerCase() in a forEach loop to update the name of students?

Advertisements