Pon Z. Pon Z. - 4 months ago 6
PHP Question

Please help me using for loop in PHP

I want to use a loop to iterate through my array, calling my function to print out all of these messages. I have to somehow keep track of what number person im on. -- im using PHP

<?php
$name_array = array('pon', 'zi', 'pol', 'et');

function name_person($name, $number) {
echo $name . ' is person #' . $number . ' ';
}

for ($i = 0; $i <= 4; $i++) {
$counter = 1;
while ($counter <=4) {
name_person( $name_array[$i], $counter);
$counter++;
}
}


my output should be:

pon is person #1, zi is person #2, pol is person #3, et is person #4


Can you please help me?

Answer

answer is

  <?php
$name_array = array('pon', 'zi', 'pol', 'et');
function name_person($name, $number) {
    echo $name . ' is person #' . $number . ' ';
}
    $counter = 0;

    while ($counter <=3) {
        name_person( $name_array[$counter], $counter+1);
        $counter++;

    }

?>