• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  Remove Specific Character from String Variable in php
  • 2.  php getimagesize – Get Height, Width and Image File Type
  • 3.  php Convert Radians to Degrees with php rad2deg() Function
  • 4.  Using php to Copy Files Easily from One Directory to Another
  • 5.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 6.  php array_diff() – Find Elements of Array Not Present in Other Arrays
  • 7.  Using strtoupper() in php to Convert String to Uppercase
  • 8.  Truncate Strings in php Using substr()
  • 9.  php preg_match_all – Get All Matches of Pattern in String
  • 10.  php tan – Find Tangent of Number in Radians Using php tan() Function

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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