AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP foreach


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP foreach

What is foreach in PHP?
The Foreach build in PHP provides an easy way to repeat over the array, foreach only works on arrays and objects and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

Syntax1:

foreach (iterable_expression as $value)

Syntax2:

foreach (iterable_expression as $key => $value)

 

Customize object iteration.

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

Example (this is HTML - PHP editor, change text on this window)

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
foreach ($arr as $key => $value) {

echo "<br /> {$key} => {$value} ";
print_r($arr);
}
?>

 

Related subjects:
What is an array Convert array to string comma Array to comma separated Associative array String contains

What is foreach in PHP?
How do you iterate through an array in PHP?
What is the difference between for and foreach in PHP?
Which loop would you prefer to go through an array in PHP briefly describe?

Tags: php foreach: key value, break, index, continue, multidimensional array, loop, string, end foreach, associative array, last item

 

PHP foreach with indexed arrays

To iterate over all elements of an indexed array, you use the following syntax:

foreach ($array_name as $element) { // process element here }

Example (this is HTML - PHP editor, change text on this window)

<?php
$my_colors = ['white', 'yellow', 'black', 'green'];

foreach ($my_colors as $color) {
echo $color . '<br />';
}
?>

 

PHP foreach with an associative array

To iterate over elements of an associative array, you use the following syntax:

foreach ($array_name as $key => $value) {
//process element here;
}

Example (this is HTML - PHP editor, change text on this window)

<?php

$country_capitals = [
'Austria' => 'Vienna',
'Spain' => 'Madrid',
'Italy' => 'Rome',
'Framce' => 'Paris',
'United States' => 'Washington D.C.'
];

foreach ($country_capitals as $country => $capital) {
echo "The capital city of {$country} is $capital" . '<br />';
}
?>

 



php foreach: key value, break, index, continue, multidimensional array, loop, string, end foreach, associative array, last item
PHP foreach - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...