Practical Web Programming

Thursday, January 22, 2009

PHP: Using foreach to Loop Through Array

The foreach looping construct in one of the less used looping construnct in the programming language, and PHP is not an exception. This is because in PHP, the foreach works only with arrays and will spit an error if used with other data type.

Anyways, using the foreach loop construct is really easy. See example below.

$array = array("j", "o", "e", "l", "b", "a", "d", "i", "n", "a", "s");

foreach ($array as $char)
{
print $char."<br/>";
}


The above example will output below.

j
o
e
l
b
a
d
i
n
a
s

0 comments:

Recent Post