72 of 110 menu

PHP Array Loop

Looping through an array in PHP is done using the foreach command.

The syntax is as follows: the name of the array being iterated, then the keyword as then the name of the variable into which the array elements will be placed sequentially, in our case its name is $elem, but we can give it any name.

Let there be an array:

<?php $arr = [1, 2, 3, 4, 5]; ?>

Let's output all its elements to the screen as an example:

<?php $arr = [1, 2, 3, 4, 5]; foreach ($arr as $elem) { echo $elem; } ?>

Code execution result:

12345

See Also

English
БеларускаяEspañolРусский
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline