I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.

I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)

Answer from alex on Stack Overflow
🌐
PHP
php.net › manual › en › function.count.php
PHP: count - Manual
Returns the number of elements in value. Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface, 1 would be returned, unless value was null, in which case 0 would be returned.
🌐
W3Schools
w3schools.com › php › func_array_count.asp
PHP count() Function
PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser · AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll · PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Syllabus PHP Study Plan PHP Certificate ... array() array_change_key_case() array_chunk() array_column() array_combine() array_count_values() array_diff() array_diff_assoc() array_diff_key() array_diff_uassoc() array_diff_ukey() array_fill() array_fill_keys() array_filter() array_flip() array_intersect() array_intersect_assoc() array_intersect_key() array_intersect
Discussions

PHP array: count or sizeof? - Stack Overflow
To find the number of elements in a PHP $array, which is faster/better/stronger? count($array) or sizeof($array) ? Edit Thanks to Andy Lester, I have refined my question to mean from a multilingual More on stackoverflow.com
🌐 stackoverflow.com
php - Get string length of each element in an array - Stack Overflow
I'm trying to set the max length of each post on a site, but strlen() doesn't work with arrays. So I need to break it down to check each post in the array. How could I adapt what I have to get thi... More on stackoverflow.com
🌐 stackoverflow.com
How to declare the length/size of an array in php, so users can input any size - Stack Overflow
I understand but think about this if we could declare the size say in this format $a=[12], but this means the index 0 of the array $a has a value 12 in it. But in c if we declare like $a=[12], it means it has the array $a can store 12 elements. They should avail this in php if possible, because ... More on stackoverflow.com
🌐 stackoverflow.com
php - Length array in array - Stack Overflow
However if the array you want to get the length is not always in this same position you could do something like this: More on stackoverflow.com
🌐 stackoverflow.com
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
How do I properly use the PHP array length in a for loop?
A for loop is a common use case for the php array length. 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.Code Example:PHP$colors = ["Red", "Green", "Blue"];$length = count($colors);for ($i = 0; $i < $length; $i++) {   echo $colors[$i] . "
";}
🌐
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
How does checking the PHP array length affect performance?
Checking the php array length using count() is an extremely fast operation. PHP internally keeps track of the element count for each array, so when you call count(), it simply retrieves this stored value. This is an O(1) operation, meaning its execution time is constant and does not depend on the size of the array. Therefore, it is not a performance bottleneck, even for very large arrays.
🌐
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
🌐
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 - Here are a few best practices to follow when working with arrays and their lengths in PHP: Always prefer count() or sizeof() for clarity: Using built-in functions is not only more efficient but also more readable to others who may read your code. Use COUNT_RECURSIVE for multi-dimensional arrays: If you’re dealing with nested arrays and need to count all elements, use the COUNT_RECURSIVE mode with count(). Check if the array exists: Before getting the length of an array, ensure that the variable is an array using is_array().
🌐
ReqBin
reqbin.com › code › php › iawpnvuz › php-array-length-example
How to get the length of an array in PHP?
In this PHP Array Length example, we obtain the array's length using the count() function.
🌐
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
August 14, 2025 - 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.
Find elsewhere
🌐
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 - Getting the length (size) of a flat array in PHP is straightforward using the count() function. A flat array is an array that contains a single level of elements (non-nested).
🌐
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 - PHP count returns the number of elements in an array or Countable object. Click here to see how it works with examples.
🌐
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 - In this example, the sizeof() function ... functions that are commonly used to get the length or size of an array, which are sizeof() and count()....
🌐
freeCodeCamp
freecodecamp.org › news › php-array-length-how-to-get-an-array-size
PHP Array Length Tutorial – How to Get an Array Size
August 27, 2021 - There are two common ways to get the size of an array. The most popular way is to use the PHP count() function. As the function name says, count() will return a count of the elements of an array.
🌐
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 - The value returned by the count() function will be the number of elements within the array, giving you its length. ... To showcase this behavior, let us create a simple array called “fruit” containing five elements, each being a different ...
🌐
Laracasts
laracasts.com › discuss › channels › laravel › how-to-get-array-count
how to get array count
December 16, 2016 - We cannot provide a description for this page right now
Top answer
1 of 3
1

What you need to do is first create a list of strings matching the length and then pick a random one from them. So loop through all of the strings, check the length and add to $match, then use array_rand() to pick one of the matching ones...

function getName($random_num)
{
    $names = array
    (
        'Juans',
        'Luisss',
        'Pedroaa',
        'testNames1',
        'testNames2',
        'testName3',
        'test', //This should not be return because im only generating a number random of 5-10 and this character has only 4 character length
        'tse', //This should not be return because im only generating a number random of 5-10 and this character has only 3 character length
        // and so on
    );
    $randomString = "";
    $match = [];
    foreach ( $names as $name )
    {
        $length_string = strlen($name);
        if ($random_num == $length_string)
        {
            $match[] = $name;
        }
    }
    // If non found return ''
    return !empty($match) ? $match[array_rand($match)] : '';
}
2 of 3
1

You can try the below code. You was adding the below line in the loop that was wrong.

$value = rand(5,10);

Now, This program will return the name that will be equal to the generated random number that is between 5 to 10. Also it will return the message if no name exist according to the random number. For example: 8

<?php
function getName($random_num)
{
    $names = array
    (
        'Juans',
        'Luisss',
        'Pedroaa',
        'testNames1',
        'testNames2',
        'testName3',
        'test', //This should not be return because im only generating a number random of 5-10 and this character has only 4 character length
        'tse', //This should not be return because im only generating a number random of 5-10 and this character has only 3 character length
        // and so on
    );
    $randomString = "";
    for ($i = 0; $i <count($names); $i++)
    {
        $length_string = strlen($names[$i]);
        if ($random_num == $length_string)
        {
            return "name:".$names[$i].$random_num;
        }
    }
}
$random_num = rand(5,10);
$returned_name = getName($random_num);
if ($returned_name == "")
{
    echo "No Name Exist having length: " . $random_num;
}
else
{
    echo $returned_name;
}
?>
🌐
sebhastian
sebhastian.com › php-array-length
PHP how to get an array's length (single and multi-dimensional arrays) | sebhastian
November 8, 2022 - To get an array’s length in PHP, you can use either the sizeof() or count() function.
🌐
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.