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

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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.

Example:

<?php
  
    $emptyArray = (array) null;
  
    var_dump($emptyArray);
?>

Output:

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.

<?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>";
     }
?>

Output:

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

Related Posts

PHP – strip_tags() Replace with Spaces Example

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Get Duplicate Values from Array in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

PHP explode() Function Example Tutorial

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Convert Array to String in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How Can I Convert a String in PHP Into an Array?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to Use the array_merge() Function in PHP?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x