The foreach statement, although it sounds like an extension of the for statement, is intended to be used while working with arrays and objects. The foreach statement gives an error with a regular variable.

Syntax for FOREACH statement

<?php
    // one way
    foreach (expression as $value)
        {
            // code goes here
        }
    // another way
    foreach (expression as $key => $value)
        {
            // code goes here
        }
?>

Example with a FOREACH statement

 

›› go to examples ››