PHP Array Declaration and Defination

You all have already got clear idea about PHP basic fundamental. Now it is time to start the journey with PHP advanced. In the advanced section we will discuss array, string, function, include, working with files etc.

What is Array?

An array is like a container that stores the several types of value. Like other programming languages, PHP gives the feature to save several types of value in a single array. It’s pretty good for a coder to deal with multiple types of values with an array.

If we compare the array with a locker then every portion of that locker will be considered as an index. Generally, array deals with index. In every index, different types of value are stored. The picture given below shows a clear idea about array index.

Array starts with index zero and every value are stored within several indexes.  Array values are separated by a comma(,) from each other. During defining associative array =>(array separator) is used.



This is the diagram for array understanding. Here stored values are 9, 11, 7, 25, 6, 20, 16, 12, 45. All the values are stored in an array with different indexes started from zero. Here 9 is a value and 0 is an index. In same way 1 is an index where 11 is a value. A single index can store a single value only. It can be string, float, double, bool.


Defining an Array in PHP

For defining an array in php it is needed to use keyword array. After that, it needs to write left and right parenthesis. Let’s define an array. Array can be declared by square brackets( [] ) also. But, this is not a good practice as well.

Here these two arrays are one dimensional and we can also use a multidimensional array for several purposes. For defining multidimensional array it needs to use the array as nested like below. The value of the array can be shown in the browser by using var_dum()  or print_r function.



Its possible to write an array with a key. Key is a name that is stored for accessing every single value. This key refers to every single value. For Writing an array with a key then it needs to follow the following process. If an array doesn’t carry any key then default indexes(0,1,2,3,4,5…) are used.

Here there are several keys like country, passion, email etc. for storing different values. This keys will be used for accessing, changing or printing values.

Every time a programmer needs to deal with an array. For this reason, it is necessary to have good command in array declaration, index. 

Share This on Social Media