- Array Length PHP Calculate PHP Array Length
- count(): PHP Array Length For Loop
- array_or_countable
- mode
- sizeof()— PHP Array Length?
- How to Check Whether an Array Is Empty in PHP
- Applying the empty() Function
- Applying the count() Function
- Applying the sizeof() Function
- How to Check if an Array is Empty in PHP
- Using Empty()
- Using Count()
- Using sizeof()
- Conclusion
- Is your PHP array empty? A few ways to make sure of it.
- The empty() function
- The count() function
- The sizeof() function
- The not ( ! ) operator
Array Length PHP Calculate PHP Array Length
How to calculate the PHP array length? If you want to count the total number of elements or values in an array, you can easily do it by using PHP inbuilt functions called count() or sizeof(). You can find a detailed explanation with the example below that how you can calculate PHP array length.
count(): PHP Array Length For Loop
count(var array_or_countable, mode)
If you don’t know what is an array then please see the detailed explanation of how arrays are created and used in PHP.
array_or_countable
The first argument is an array variable that is required to calculate the PHP array length. 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 function isset() and check whether an array variable is set or not.
mode
The second argument is optional and can take two values either COUNT_NORMAL or COUNT_RECURSIVE. You can pass the value 0 and 1 instead of COUNT_NORMAL and COUNT_RECURSIVE that will make the same effect. The default value of the second argument is COUNT_NORMAL if no value passed as the second argument. Example #1 count() example
$food = array('fruits' => array('orange', 'banana', 'apple'), 'veggie' => array('carrot', 'collard', 'pea')); // recursive count echo count($food, COUNT_RECURSIVE); // output 8 // normal count echo count($food); or count($food, COUNT_NORMAL ); // output 2
In the above code block, We have created a multidimensional array. When we use count function in the RECURSIVE mode as a second argument, Output is 8. And, when we use count function in the NORMAL mode as a second argument or default mode, Output is 2.
sizeof()— PHP Array Length?
sizeof() is an alias of PHP count() function and accepts the same arguments as count(). If We replace the count() with sizeof() in the above example, the results of both functions will be the same for both functions calls. Many programmers who come from C language background expect sizeof() to return the amount of memory allocated. But, sizeof() — as described above — is an alias for count() in PHP. It is advised to always use count() instead of sizeof(). Because there is no guarantee for the alias function if they will exist or not in a future upgrade. So, you should always consider to maintain code standard as well as avoid to break the application due to the upgraded version.
How to Check Whether an Array Is Empty in PHP
Sometimes a software crash or other unexpected circumstances can occur because of an empty array. Hence, it is crucial to detect empty arrays beforehand and avoid them. This tutorial represents how to inspect whether a particular array is empty or not in PHP.
Let’s check out several useful means that will help you meet that goal.
Applying the empty() Function
The first method is applying the empty() function as shown in the example below:
$phone = []; echo empty($phone) ? "Array is empty." : "Array is not empty."; ?>
The output of this code will indicate that the array is empty.
Applying the count() Function
The next function to use for detecting an empty array is the count() function. This function differs from the one above: it is aimed at counting the elements inside an array.
This function returns 0 once the array is empty. Otherwise, the number of elements will be returned. In the case below, the result is 0. Thus, the given array is empty:
Now, let’s see another example where the number of the elements is returned:
$phone = ["00000", "11111", "22222", "33333"]; echo count($phone); ?>
The output of this example is 4. It means that the array includes 4 elements.
Applying the sizeof() Function
The third method is using the sizeof() function. Globally, it is used for checking the array size. When its size is 0, then the array is considered empty. Otherwise, it’s not empty.
Here is an example of using the sizeof() function:
// Declare an empty array $empty_array = []; // Use array index to check // array is empty or not if (sizeof($empty_array) == 0) < echo "Empty Array"; > else < echo "Non-Empty Array"; > ?>
In the output, it will be indicated that the given array is empty.
How to Check if an Array is Empty in PHP
In many cases, it is useful to know if the array you’re working with is empty or not.
This can be to prevent errors, ensure data integrity, or just to know if you should do something or not.
In this post, we’ll learn how you can check if an array is empty or not in PHP.
Using Empty()
The best way to check if an array is empty is to use the empty() function.
This function takes in your array and returns a boolean value, true if the array is empty, and false if it is not.
Let’s start out with our example array:
Now let’s use the empty() function to check if the array is empty:
As expected, the array is empty, so the empty() function returned true .
Using Count()
Another way you can check if an array is empty is to use the count() function.
This function will return to you the number of elements in the array, so if the array is empty, it will return 0 .
Because this returns to you the number of elements in the array, you can use this information for anything else you need in your program.
Using sizeof()
The last way you can check if an array is empty is to use the sizeof() function.
This function will also return to you the number of elements in the array, so if the array is empty, it will return 0 .
Conclusion
In this post, we learned several ways to check if an array is empty in PHP.
You can either use the empty() function, the count() function, or the sizeof() function.
Thanks for reading and happy coding!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!
Give feedback on this page , tweet at us, or join our Discord !
Is your PHP array empty? A few ways to make sure of it.
Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting web applications!
To ensure that a PHP array is empty, you can use any of the following methods:
- Use the empty() function to check if the array is empty and return a boolean value.
- Use the count() (or sizeof() ) function to count the number of elements in the array and check if it is equal to zero.
- Use the not operator ( ! ). If the array does hold any value, the not operator will return true .
The empty() function
The empty() function determines if a value is empty or not. It simply returns true if it’s empty, or false if it’s not.
This is my favorite way to check if an array is empty in PHP.
$foo = []; // trueif (empty($foo)) //> $bar = ['Foo', 'Bar', 'Baz']; // falseif (empty($bar)) //>
Learn more about the empty() function.
The count() function
The count() function counts the number of entries in an array and returns it as an integer.
You can even use it with Countable objects.
For multidimensional arrays, there’s a second parameter for which you can use the COUNT_RECURSIVE constant to recursively count the numbers of items.
$array = [ 'Foo' => [ 'Bar' => ['Baz'], ],]; $count = count($array, COUNT_RECURSIVE); // If $count is greater than zero, then your array is not empty.if ($count > 0) //>
Learn more about the count() function.
The sizeof() function
Learn more about the sizeof() function.
The not ( ! ) operator
This one is simple. You are probably used to the not ( ! ) operator. I didn’t know it could check for empty arrays, but here I am, after 15 years of PHP learning yet another basic thing.
$foo = []; if (! $foo) echo '$foo is empty.';>
Article written by Benjamin Crozat
I built Smousss, an AI-powered assistant for Laravel developers.