PHP Program to use break in a foreach loop with an associative array

bookmark

<?php
    //array definition
    $members = array("joe" => "12", "liz" => "13", "sam" => "14", "ben" => "15");
    
    //variables to count
    $count = 0;
    
    //number f display or loop
    $end = 2;
    foreach ($members as $key => $value) {
        $count++; // increment the counter
        //display
        printf("%s is  %d years old<br>", $key, $value);
        //stop when $count equals $end
        if ($count == $end) break;
    }
?>


Output

joe is 12 years old
liz is 13 years old