Declare empty array in php

How to initialize empty array in PHP

An array is a data structure where you can store the same or different types of data in a single variable.

In PHP, you can declare empty array using square brackets ([]) or using array() function.

Using square brackets to declare empty array in PHP

The below syntax can declare empty array in PhP. We can also adding the elements to this array using square brackets.

Example

Output

An empty array in PHP is:

array(0) {
}

Code Explanation

In the above program, we have declared empty array using [] and printed it using var_dump.

You can add elements in array with the help of square brackets as below:

An empty array in PHP is:
array(0) {
}
Elements of array are:
array(4) {
[0]=>
string(5) “Arpit”
[1]=>
string(4) “John”
[2]=>
string(4) “Mary”
[3]=>
string(5) “Ankur”
}

Here, we have added elements to empty array using square brackets([]).

Using array() to declare empty array in PHP

The below syntax can also declare empty array in PHP.

An array function has N parameters, where N is the number of elements the array stores. $index1, $index2, …, $index is an index of array elements and can be an integer or string. $value1, $value2, …, $valueN is the value of array elements.

Example

Output

An empty array in PHP:

array(0) {
}

Code Explanation

The above program shows how we can use the array() function to initialize an empty array in PHP.

We can use array() function to add elements into the array in PHP.
Here is an example:

Output

An empty array in PHP:

array(0) {
}
An array with three values:
array(3) {
[0]=>
string(5) “Arpit”
[1]=>
string(3) “Jai”
[2]=>
string(7) “Adithya”
}

From the above, we have learned how to declare empty array using square brackets([]) and an in-built PHP function array().

That’s all about how to declare empty array in PHP.


import_contacts

You may also like:

Related Posts

  • Python check for null
    17 November

    How to check for null in Python

    Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

  • Split String with multiple delimiters
    17 November

    Split String With Multiple Delimiters In Python

    Table of ContentsSplitting a string in PythonWays to split a string in PythonUsing the split() and replace() functionsUsing the regular expressions In this post, we will see how to split String with multiple delimiters in Python. Splitting a string in Python When we talk about splitting a string, it means creating a collection of sub-strings […]

  • Funny Kahoot names
    17 November

    600+ Funny, Dirty, Inappropriate Names for Kahoot

    Table of ContentsFunny Names for KahootKahoot: A Brief OverviewHow to Change your Name on Kahoot?Funny Names for Kahoot: Overall EditionFunny Kahoot Names: Girls EditionFunny Names For Kahoot: Boys EditionFunny Names for Kahoot: Unique EditionFunny Troll Names for KahootFunny Inappropriate Names: Dirty Kahoot namesGood Kahoot NamesFunny Inappropriate Names: Set 2Frequently Asked Questions About Funny Names for […]

  • Funny steam names
    17 November

    600+ Funny Steam Names to Use in 2021

    Table of ContentsFunny Steam Names: Let’s Enhance your Steam Profile by Giving it a New NameAssigning Funny Steam Names:Step-By-Step Guide200+ Funny Steam Names70+ Cool Steam Names90 Good Steam Names50 Clever Steam Names30 Funny Steam Names For Boys35 Funny Steam Names For Girls25 Creative Cool Steam Names100+ Other Funny Steam Names to Consider in 2021Deleting Previous […]

  • Sort dictionary by value in Python
    17 November

    Sort dictionary by value in python

    Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

  • Get class name in Python
    17 November

    Get class name in Python

    Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.