Web Programming TutorialsLearn about Constants Array in PHP7

Learn about Constants Array in PHP7

Learn-about-Array-of-Constants-in-PHP7

In the last chapter, we discussed two new PHP 7 operators – null coalescing and spaceship operators with examples. In this chapter, we are going to discuss about array of constants, which is again one of the many new features added to PHP 7.

Constants Array
Before PHP 7, we were using the const keyword to define an array of constants. PHP 7 has a new feature allowing us to define an array of constants using the ‘define ()’ function as shown below.
define (‘Canada’, [‘Calgary’,’Montreal’,’Ottawa’,’Toronto’,’Vancouver’]);
Here, the constants array name is Canada and city names such as ‘Calgary’, ‘Montreal’, ‘Ottawa’, ‘Toronto’, and ‘Vancouver’ are the constants. Like any other array, each constant has an index starting from 0 to number of elements minus one. Therefore, element at zero index will be ‘Calgary’ and at index as 1 is ‘Montreal’ and so on.

We are going to learn about constants array in detail with the following example.
Capture
Explanation of PHP 7 code
We have used the ‘define ()’ function to declare an array with the name as Canada which has 5 constant values that cannot be modified or deleted.

Output
In the above program, we are printing the value of array at 4th index which has a value ‘Vancouver’.
Output

Union of Constant Arrays
We use the + operator for joining two arrays. The joining takes place at the index level of the arrays. E.g. ‘a’ and ‘b’ are two arrays. Array ‘a’ has 4 elements and array ‘b’ has 6 elements. Then the combination of these two arrays i.e. $a + $b will be equal to first 4 elements of array ‘a’ and then 5th and 6th element of the array ‘b’. This is demonstrated below.
Capture1

Explanation of PHP 7 Code
Here, we first declared two arrays – array ‘a’ and array ‘b’, then we used the define function to create two constants array ‘Canada’ and ‘India’, respectively. Constant array ‘Canada’ has 4 elements namely Canada [0] = Montreal, Canada [1] = Ottawa, Canada [2] = Toronto, Canada [3] = Vancouver. Similarly constant array ‘India’ has 6 elements namely India [0] = New Delhi, India [1] = Mumbai, India [2] = Kolkata, India [3] = Chennai, India [4] = Jaipur, India [5] = Pune. Now, using define () function feature, we are defining a constants array ‘World’ using + operator which will combine the ‘Canada’ and ‘India’ arrays. After union, World array will have total of 6 elements namely World [0] = Montreal, World [1] = Ottawa, World [2] = Toronto, World [3] = Vancouver, World [4] = Jaipur and World [5] = Pune.

Output: After the union of two constants arrays, the resulted array World has total 6 elements and in the program we are printing elements of Canada [3], India [4] and World [5]. Below is the output.
Output1

Equality of Constants Arrays
We use the == operator to check the equality of two arrays. The equality takes place at the index as well as element value level between the arrays. E.g. ‘a’ and ‘b’ are two arrays. Array ‘a’ has 4 elements and array ‘b’ has 6 elements. Then, the equality check between these two arrays i.e. ‘$a == $b‘ will return bool (false) as the array ‘a’ and the array ‘b’ index and corresponding values do not match. This is demonstrated below.
Capture2

Explanation of PHP 7 Code
Here, we used the define function to declare two constants arrays ‘Canada’ and ‘India’ that have 4 and 6 elements, respectively. Therefore, when equality of ‘Canada’ array is checked against ‘India’ array, the equality statement will return bool (false). However, when we compare ‘Canada’ array against ‘Canada’ array, the equality statement will return bool (true) as both arrays are identical.

Output
Below output shows bool (false) when we checked equality of ‘Canada’ array against ‘India’ array and bool (true) when we checked equality of ‘Canada’ array against ‘Canada’ array.
Output2

Identity of Constants Arrays
We use the === operator to check if the two arrays are identical. If ‘a’ and ‘b’ are two constant arrays that have the same key and value pairs that are in the same order and are of the same types. Then a === b will return true, otherwise false. This is demonstrated below:
Capture3

Explanation of PHP 7 Code
Here, we used the PHP 7 define function to declare one constants array ‘Canada’ which has 4 elements. When we check for identity of ‘Canada’ array against ‘Canada’ array, the identity statement will return bool (true). However, when we compare ‘Canada’ array against any other different array, the identity statement will return bool (false), as those arrays will be non-identical.

Output: Below output shows bool (true) when we checked identity of ‘Canada’ array against ‘Canada’ array.
Output3

Inequality of Constants Arrays
We use != operator to check if the two arrays are equal. The inequality takes place at the index level as well as the value of the elements of the arrays. E.g. ‘a’ and ‘b’ are two arrays. Array ‘a’ has 4 elements and array ‘b’ has 6 elements, then the Inequality check between these two arrays i.e. ‘$a != $b‘ will return bool (true) as the array ‘a’ and the array ‘b’ index values do not match. This is demonstrated below.
Capture4

Explanation of PHP 7 Code
Here, we used PHP 7 define function to declare two constants arrays namely ‘Canada’ and ‘India’ which have 4 and 6 elements, respectively. Therefore, when inequality of ‘Canada’ array is checked against ‘India’ array, the inequality statement will return bool (true). However, when we compare ‘Canada’ array against ‘Canada’ array, the inequality statement will return bool (false), as both arrays are identical.

Output
The below output shows bool (true) when we checked inequality of ‘Canada’ array against ‘India’ array and bool (false) when we checked Inequality of ‘Canada’ array against ‘Canada’ array.
Output4
Click Source Code For this article

Conclusion
In this chapter, we explored a new feature of PHP 7 to define constants array using define () function without using the const keyword like in PHP 5.x. After that we conducted various operations on these constants arrays such as union, equality, identity and inequality between two constants arrays. In the next chapter, we are going to discuss about Anonymous Classes, another new feature of PHP7.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -