• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / PHP / How to Create an Empty Array in php

How to Create an Empty Array in php

March 23, 2022 Leave a Comment

In php, there are three ways you can create an empty array. Creating an empty array can be useful if your data is dynamic.

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

When working with arrays and collections of data in php, it is useful to be able to create and use different data structures easily.

We can easily create an empty array in php.

There are three ways you can use to define an empty array in php.

The first is similar to defining an array in JavaScript, where an empty array is defined with two open square brackets.

$emptyArray = [];

The second way to define an empty array in php is with the array() function.

$emptyArray = array();

The third and final way to create an empty array in php is by casting “null” to type array.

$emptyArray = (array) null;

In our opinion, the easiest way to initialize an empty array with no elements is the first way.

How to Add Elements to an Empty Array in php

After initializing an empty array, adding elements is easy. We can add items to an array with the php array_push() function.

Below is a simple example in php of creating an empty array and adding three elements to the array.

$emptyArray = [];

print_r($emptyArray);

array_push($emptyArray,"bear","snake","horse");

print_r($emptyArray);

//Output:
Array
(
)
Array
(
    [0] => bear
    [1] => snake
    [2] => horse
)

Hopefully this article has been useful for you to learn how to create an empty array in your php programs.

Other Articles You'll Also Like:

  • 1.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 2.  Remove HTML Tags from String in php
  • 3.  Replace Underscore with Space in php String
  • 4.  php rtrim() Function – Remove Whitespace at End of String
  • 5.  php json_encode() Function – Get JSON Representation of Variables in php
  • 6.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 7.  How to Parse URLs with php parse_url() Function
  • 8.  php print_r() Function- Get Information about Variables in php
  • 9.  php Delete Files with php Unlink Function
  • 10.  PHP Variables Are Preceded by $

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy