How to create an empty array in PHP? explain with example

When an empty array and then start entering elements in it later. With the help of this, it can prevent different errors due to a faulty array. It helps to have the information of using bugged, rather than having the array. It saves time during debugging. Most of the time it may not have anything to add to the array at the point of creation.

Syntax to create an empty array:

$emptyArray = []; 
$emptyArray = array();
$emptyArray = (array) null;

While push an element to the array it can use $emptyArray[ ] = “first”. At this time, $emptyArray contains “first”, with this command and sending “first” to the array which is declared empty at starting.

In other words, the initialization of the new array is faster, use syntax var first = [ ] rather than using syntax var first = new Array(). The fact is being a constructor function the function Array() and the, [ ] is a part of the literal grammar of array. Both are complete and executed in completely different ways. Both are optimized and not bothered by the overhead of any of the calling functions.


    $emptyArray = (array) null;


array(0) {

PHP supports [ ] as an alternative, according to the respective compiler it was synonymous and most PHP developers use $array = [ ] because it makes it easier to go back and forth between JS and PHP.


    $firstempty = [];
    echo "Created First empty array <br>";

    $second = array( );
    echo "Created second empty array <br>";  

    $first = array( 1, 2);
    foreach( $first as $value ) {
      echo "Value is $value <br>";

     $first[0] = "one";
     $first[1] = "two";
     foreach( $first as $value ) {
       echo "Value is $value <br>";


Created First empty array 
Created second empty array
Value is 1 
Value is 2 
Value is one 

Related Posts

Associative Array in PHP?

An associative array is a simple and very important array of PHP. This array is also used the most, it is very easy to understand this array,…

How to work in functions PHP?

If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place…

For Loofs in PHP

In for-loop initialization, condition and increment happen on the same line. The loop keeps on repeating as long as the condition is true. When the for loop…

If Else Conditionals in Php?

If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This…

What are operators in PHP? PHP – Operator Types

Operators are used to developing data. In Programming Languages, Operators are taken from Mathematics and Operator Programmers work with Data. An operator is a chain of symbols…

String Functions in PHP?

When many characters are written together in sequence, they form a string. For example, India is a string. It is made up of i, n, d, i,…

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x