PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




PHP For Each Statement


Tutorials »Php »

Topic

What is "foreach" statement?



Explanation

For Each structure is a loop structure used for arrays

Syntax1:
    foreach (array_expression as $value)
      statement
Syntax2:
    foreach (array_expression as $key => $value)
     statement

In Syntax1 the array is given by the "array expression" and each value of the elements is assigned to "$value". Syntax2 is also similar, but only difference is that current elements key is assigned to "$value".

Example
     <?php
     $arr["One"]= "1";
     $arr["Two"]= "2";
     $arr["Three"]= "3";
     foreach ($arr as $value => $key) 
     {
      echo "The value $value, has the key $key </br>";
     }
     ?>
Result:
    The value One, has the key 1
    The value Two, has the key 2
    The value Three, has the key 3 

In the above example the $value is preceded by "&",it means that the array is referenced, this is to make changes to the array elements, instead of having the copy of a array. Usually reference of a $value and the last array element remain even after the foreach loop,so to remove that we use unset().





A Note

Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial. Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.

Other Links

web hosting