Monday, January 25, 2016

FOREACH LOOP PHP: Display Data from An Array Using Foreach Loop in PHP

An array is a special variable that holds not just a single value, but multiple values.

Let's say we have an array of names such as Reah, Riza, Mond and Kris. Usually, a variable can only hold one (1) value at a time. But with arrays, you can store multiple values at the same time.

You can access the values in array using two (2) ways. First is by accessing the value using the assigned index. By default, array index start with zero (0).

PHP Code

<?php
$name=["Reah","Riza","Mond","Kris"];
echo $name[0];
echo "<br>";
echo $name[1];
echo "<br>";
echo $name[2];
echo "<br>";
echo $name[3];
?>

And second is with the use of a foreach loop.
foreach ($name as $value)
{
echo $value;
echo "<br>";
}
?>

Output








FOREACH LOOP PHP: Display Data from An Array Using Foreach Loop in PHP

FOREACH LOOP PHP: Display Data from An Array Using Foreach Loop in PHP Rating: 4.5 Diposkan Oleh: Emoblazz

0 comments:

Post a Comment