In this tutorial, you shall learn how to create an empty array in PHP using array() function, with the help of example programs.
PHP – Create empty array
To create an empty array in PHP, we can use array()
function or square bracket notation.
Call array()
function and pass no arguments to it. The function returns an empty array.
Or given empty square brackets with no elements mentioned between them. The expression represents an empty array
array()
//or
[]
Examples
1. Create empty array using array()
In this example, we create an empty array using array()
function and assign it to arr
.
To verify if array is empty, we shall print the length of array. An empty array must have a length of zero.
PHP Program
<?php
$arr = array();
print_r("array length : " . count($arr));
?>
Output
2. Create empty array using square brackets notation
In this example, we create an empty array using square brackets notation and assign it to arr
.
PHP Program
<?php
$arr = [];
print_r("array length : " . count($arr));
?>
Output
Conclusion
In this PHP Tutorial, we learned how to create an empty array using array()
function or square bracket notation.