Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
PHP Foreach Loop
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. PHP Tutorials
  3. PHP Foreach Loop

PHP Tutorial

Basic PHP Fundamentals

  • PHP Tutorial Content
  • PHP Introduction
  • PHP History
  • Uses of PHP
  • PHP Features
  • What Is PHP
  • PHP Installation And Software Requirements
  • How To Install Wamp Server
  • How To Install Xampp Server
  • Php Syntax
  • PHP Variables
  • PHP Data Types
  • PHP Constants
  • PHP Echo And Print
  • PHP Operators
  • PHP Assignment Operators
  • PHP String And Logical Operators
  • Operators Categories
  • PHP Comments
  • PHP $ And $$ Variables
  • Super Global Variables

PHP Control Statement

  • PHP Control Structure
  • PHP If Statement
  • If Elseif Statement In PHP
  • Switch Statement In PHP
  • Break Statement In PHP
  • If Elseif Else Statement
  • Continue Statement In PHP

Php Loops

  • What Is Loop
  • For Loop In PHP
  • PHP Foreach Loop
  • PHP While Loop
  • Do While Loop

PHP Arrays

  • PHP Arrays
  • Indexed Arrays
  • Associative Array
  • Multidimensional Arrays
  • Array Functions
  • Array Constants
  • List of Array Functions
  • Array Functions First Exercise

Php String

  • String In PHP
  • PHP String Functions

PHP Functions

  • PHP Functions
  • PHP Parameterized Function
  • PHP Call By Value
  • Call By Reference In PHP
  • Default Argument Values Function In PHP
  • Variable Argument Function In PHP
  • Recursive Function In PHP
  • Math Function In PHP
  • Math Function Examples

PHP Form

  • Form Handling In PHP

PHP Include

  • Include File In PHP

PHP State Management

  • Cookies In PHP
  • Session In PHP

PHP File System

  • File Handling In PHP
  • Open Files In PHP
  • Read Files In PHP
  • Write Files In PHP
  • Delete Files In PHP
  • Append To Files In PHP

PHP Upload & Download

  • File Upload In PHP
  • Download File In PHP

PHP Mail System

  • Mail System In PHP
  • Mail With Attachment In PHP

Basic PHP Program

  • Php Programs
  • Sum Of Digits
  • PHP Even Odd Program
  • Prime Number Program
  • Table Of Number Program
  • Factorial Of A Number
  • Armstrong Number
  • Palindrome Number
  • Fibonacci Series

PHP Foreach Loop

Summary: The foreach statement is used to loop through arrays. For each pass, the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

The foreach statement is used to loop through arrays. For each pass, the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

foreach loop

Syntax

foreach (array as value) {
   code to be executed;
}

foreach Loop Explanation

  • foreach(…){…} : is the foreach PHP loop block code
  • $array_data: is the array variable to be looped through
  • $array_value: is the temporary variable that holds the current array item values.
  • block of code: is the piece of code that operates on the array values

Example 1:

<?php
$days= array("Sunday", "Monday", "Tuesday");
// Loop through colors array
foreach($days as $value){
    echo $value . "<br>";
}
?>

Output :

Sunday
Monday
Tuesday

Example 2:

<?php
$Mydata= array(
    "name" => "Admin",
    "email" => "admin@esaytolearning.com",
    "age" => 22
);
// Loop through Mydata array
foreach($Mydataas $key => $value){
    echo $key . " : " . $value . "<br>";
}
?>

Output :

name : Admin
email : admin@easytolearning.com
age : 22

PHP Nested For Loop

We can use for loop inside for loop in PHP, it is known as nested for loop.

Nested for loop is executed fully for one outer for loop. If outer for loop is to be executed 3 times and inner for loop for 3 times, inner for loop will be executed 9 times (3 times for 1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd outer loop).

Example 1:

<?php  
for($i=1;$i<=3;$i++){  
for($j=1;$j<=3;$j++){  
echo "$i   $j<br/>";  
}  
}  
?>  

Output :

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

PHP foreach Loop,foreach loop statement,PHP nested for loop

Series: PHP Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
PHP Foreach Loop foreach loop statement PHP Nested For Loop

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.