$array = array("Jonathan","Sampson");

foreach($array as $value) {
  print $value;
}

or

$length = count($array);
for (i < $length; $i++) {
  print $array[$i];
}
Answer from Sampson on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ control-structures.for.php
PHP: for - Manual
The above code can be slow, because the array size is fetched on every iteration. Since the size never changes, the loop can be easily optimized by using an intermediate variable to store the size instead of repeatedly calling count():
๐ŸŒ
DEV Community
dev.to โ€บ chrisrhymes โ€บ the-php-for-loop-47ch
The PHP for loop - DEV Community
September 13, 2021 - You can loop through an existing array using a for loop if we wanted to, using $i as the array key. The for loop variable needs to be initially defined as zero as array keys begin at zero $i = 0;. We can then use phpโ€™s count() function to ...
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ php tutorial โ€บ php array length
PHP array length | How does array length Work in PHP with Examples
April 18, 2023 - So, in each iteration step, the value gets incremented by 1. Care should be taken when using for loop in count () method as PHP lacks in differentiating indexed array and associative array.
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Talkerscode
talkerscode.com โ€บ howto โ€บ php-array-length-for-loop.php
PHP Array Length For Loop - TalkersCode.com
Now, we increment count inside this for loop. As the length of array finishes the count increment to the length of array. And at end of this php, we just prints the value.
๐ŸŒ
Medium
medium.com โ€บ @teamcode20233 โ€บ getting-the-length-of-an-array-in-php-3f10f7dd3719
Getting the Length of an Array in PHP | by Teamcode | Medium
May 30, 2023 - To get the length of an array in ... do this is by using a for loop. In this loop, we initialize a counter variable to 0 and increment it by 1 for each iteration of the loop....
Find elsewhere
๐ŸŒ
Carmatec
carmatec.com โ€บ home โ€บ how to get the length of an array in php: complete guide
How to Get the Length of an Array in PHP: Complete Guide
September 28, 2024 - This manual approach demonstrates ... best practices to follow when working with arrays and their lengths in PHP: Always prefer count() or sizeof() for clarity: Using built-in ......
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_looping_for.asp
PHP for loop
Loops While Loop Do While Loop For Loop Foreach Loop Break Statement Continue Statement PHP Functions PHP Arrays
๐ŸŒ
Koladechris
koladechris.com โ€บ blog โ€บ how-to-loop-through-arrays-in-php
How to Loop Through Arrays in PHP
The traditional for loop helps you do a good job looping through arrays. The unique thing you need to do is to get the length of the array with the count() function, then echo out each element of the array by accessing each element with the iteration variable you set.
๐ŸŒ
DEV Community
dev.to โ€บ techyhunger โ€บ calculate-php-array-length-2elb
Array Length PHP: Calculate PHP Array Length - DEV Community
August 21, 2019 - If the first argument is empty, an empty array or not set, then count() function will return 0. And, If the variable is specified but its data type is not an array, then, count() function will return 1. To avoid this, you can use the PHP inbuilt ...
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ blog โ€บ software development โ€บ php array length: a complete guide to finding array length in php [with examples]
PHP Array Length: Easy Methods to Find Array Size in PHP
3 weeks ago - You typically initialize the loop counter at 0 and have it continue as long as the counter is less than the array's length. The length is retrieved once using count() before the loop starts for optimal performance.
๐ŸŒ
GUVI
guvi.in โ€บ blog โ€บ web development โ€บ php array length: a complete guide to finding array length in php [with examples]
PHP Array Length: A Complete Guide with Examples
October 10, 2025 - Learn how to find PHP Array Length using count(), sizeof(), and loops with examples. Master PHP array length calculation easily.
๐ŸŒ
C.S. Rhymes
csrhymes.com โ€บ 2021 โ€บ 09 โ€บ 13 โ€บ the-php-for-loop.html
The PHP for loop | C.S. Rhymes
September 13, 2021 - You can loop through an existing array using a for loop if we wanted to, using $i as the array key. The for loop variable needs to be initially defined as zero as array keys begin at zero $i = 0;. We can then use phpโ€™s count() function to ...
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ how to use php count() to get array length
How to Use PHP count() to Get Array Length - FlatCoding
June 22, 2025 - This works well for custom data collections and inventory lists. In this article, you learned how the PHP count function works and where it fits in your code. You also saw how to use it with arrays, multidimensional structures, and even user-defined classes. ... Use count() to know how many elements exist in an array or a Countable object. Use the optional mode if you need to count inner children inside sub-arrays. Store count results in a variable before loops to save performance time.
๐ŸŒ
FlatCoding
flatcoding.com โ€บ home โ€บ php for loop: run code using a counter with examples
PHP For Loop: Run Code Using a Counter with Examples - FlatCoding
July 1, 2025 - A for loop works well for indexed arrays. You use the array length to set limits and access each item by index.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-iterating-through-an-array-in-php
How to Iterate Over an Array in PHP? - GeeksforGeeks
July 23, 2025 - The PHP for loop is a basic way to iterate through an array. It is useful when you need to access array elements by their index. ... <?php $arr = [1, 2, 3, 4, 5]; $length = count($arr); for ($i = 0; $i < $length; $i++) { echo $arr[$i] .