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!

You may ask, “How to create an empty array in PHP?” This is common in PHP codes, where you may want to store the values of a future computation into an array. You basically would declare the array as empty and store the computed values into it.

So, how can this be achieved?

PHP arrays with no value can be initialized in three ways:

  • $myArray = array();
  • $myArray = [];
  • $myArray = (array) null;

Among the three methods shown, the most common among programmers is the second, which uses [].
This is because the second method is marginally faster as it is only a constructor and is just simpler to use.

Code

Let’s check out some code that uses the techniques listed above.

<?php
  
$empty1 = (array) null;
$empty2 = [];
$empty3 = array();
var_dump($empty1,$empty2,$empty3);

//All above array are empty and will return empty
//pushing values into second type of array
$empty[]  ="traps" ;
$empty[]  ="tracks" ;
$empty[]  ="traces" ;
$empty[]  ="traps" ;
var_dump ($empty) ;
?>

Related Posts

Find Professionals Near Me: Complete Guide to Trusted Local Services

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

The Comprehensive Guide to AIOps Training: From Fundamentals to Architecture

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

Mastering the Evaluation of DevOps Trainer Testimonials

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

The Essential Guide to Verifying Professional Training Instructor Expertise

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

Comparing DevOps Trainer Pricing and Value for Professional Growth

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

Essential DevOps Trainer Review Checklist for Career Success

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
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x