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

A Complete Guide to Choosing the Right DevOps Trainer for Career 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

How to Evaluate Engineering Operations and Build a Practical Improvement Plan

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

Hiring a Team Collaboration Instructor: Key Steps to Follow

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

Beyond DevOps: How NoOps Is Revolutionizing IT Infrastructure Management

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 Cloud Security Lab Exercises for Trainers

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 Find a Trainer for DevOps Consulting Skills

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
0
Would love your thoughts, please comment.x
()
x