Home Tutorial Php Phpbasics Tutorial PHP For Each Loop Function

 
 

Share on Google+Share on Google+
PHP For Each Loop Function
Posted on: February 15, 2010 at 12:00 AM
Advertisement
In this current tutorial we will study about for-each loop in PHP.In PHP associative array gives us more power to use arrays in more effective way, like we can associate any key with a value. To fetch values from associative array we need to use for each loop.

Foreach Loop in PHP

In PHP  associative array gives us more power to use arrays in more effective way, like we can associate any key with a value. To fetch values from associative array we need to use for each loop.

Using  for each loop we can assign values of one by one to a variable and that variable is used inside the loop for various purpose like displaying the values of array, doing mathematical calculation etc.

Nowadays for-each loop is being used by almost every language like Java, C# etc.

Following examples will help you to learn for each loop precisely:

Example:

<?php

$studentClass;

$studentClass["ram"]="5";

$studentClass["rahim"]="5";

$studentClass["mohan"]="4";

$studentClass["joseph"]="5";

$studentClass["salma"]="2";

foreach($studentClass as $key => $value){

echo "$key reads in class $value <br/>";}

?>

 

Output:

ram reads in class 5
rahim reads in class 5
mohan reads in class 4
joseph reads in class 5
salma reads in class 2

Advertisement

Related Tags for PHP For Each Loop Function:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com