$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
🌐
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 ...
People also ask

What is the primary function to get the PHP array length?
The primary and most recommended function to get the php array length is count(). This built-in function is specifically designed to count all the elements in an array or the properties in an object. It is straightforward, efficient, and universally understood by PHP developers. When you need to determine the number of items in any array, count() should be your first choice.  Code Example:PHP$fruits = ["Apple", "Banana", "Cherry"];$length = count($fruits);// $length is now 3
🌐
upgrad.com
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
What is the easiest way to find the length of an array in PHP?
The easiest method is using the count() function, which returns the total number of elements in both indexed and associative arrays.
🌐
guvi.in
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
Why is checking the array length important before using a loop?
Checking the php array length before initiating a loop is a crucial defensive programming practice. If you attempt to loop through an empty or uninitialized array, your code might behave unexpectedly or throw errors, especially if you are trying to access elements within the loop. A simple check like if (count($array) &gt; 0) ensures that your loop only runs when there is data to process, making your application more robust and error-free.
🌐
upgrad.com
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
🌐
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....
🌐
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 - ... The above code displays the output as ‘7’ instead of the value ‘6’. To perform iteration in array elements, we can use for-loop for iteration. The values should loop continues to execute.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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 ......
Find elsewhere
🌐
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.
🌐
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 - You can use another built-in PHP function to check if the value is countable. For example: $var = null; echo is_countable($var) ? count($var) : 0; If the value of $var is countable, it assigns its count; otherwise, it assigns zero.
🌐
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 ...
🌐
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.
🌐
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.
🌐
Pi My Life Up
pimylifeup.com › home › how to get the length of an array in php
How to get the length of an Array in PHP - Pi My Life Up
October 1, 2022 - To get the length of a multi-dimensional array in PHP you need to use the “COUNT_RECURSIVE” option with the count() function. When “COUNT_RECURSIVE” has been set, the function will loop through the entirety of the array, counting each ...
🌐
PHP
php.net › manual › en › function.sizeof.php
PHP: sizeof - Manual
} IS QUICKER THEN-> for($i = 0; $i < sizeof($huge_array);$i++) { code... } up · down · 21 umopdn [at] msn [dawt] com ¶ · 17 years ago · a) Always try and use PHP's internal routines to iterate through objects of various types (arrays in most examples below). Instead of interpreting your code to loop through them, they use their own internal routines which are much faster.
🌐
Honar Systems
honarsystems.com › home › php array length (4 methods)
PHP Array Length (4 Methods) | Honar Systems
August 21, 2025 - PHP array length, count, size, and others are expressions to find how many items are in the array. Sometimes size is different from count.
🌐
Devhandbook
devhandbook.com › php › loops
Loops | Dev Handbook
In PHP, we have while, do-while, for, and foreach loop. while loops through a block of code as long as the condition is true. ... do...while loops through a block of code once, and repeats the loop as long as the condition is true. ... We can assign a variable to the array length to improve the performance, especially if an array has many items.
🌐
Scaler
scaler.com › home › topics › how to find the length of an array in php?
How to Find the Length of an Array in PHP? - Scaler Topics
April 12, 2024 - It's a crucial metric to assess the array's size and capacity. The count() function is commonly used to determine the length of an array. This function iterates through the array and counts the elements, regardless of their data types. The length of an array is significant for tasks like looping ...
🌐
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