PHP For Each Loop

Foreach loop works only on arrays and objects & it will issue an error if we try to use it on a variable with a different data type or an uninitialized variable. It is similar to For loop except we don't have to define an incrementer and increment it and there is also no condition to be evaluated. It will run for each item in the array and won't run at all if the array is empty.

It is to be noted that a For Loop and While Loop will continue till some condition is true, the ForEach loop will continue until it has gone through every item in the array.

Syntax

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

Example

<?php
$animal = array("cow", "horse", "elephant", "cat"); 
foreach ($animal as $value) {
   echo "$value";
}
?> 

This will produce following result

cow horse elephant cat


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.