Types of constant arrays in PHP 7

PHPServer Side ProgrammingProgramming

We have the following types of constant arrays in PHP 7 −

  • Union of Constant Arrays
  • Equality of Constants Arrays
  • Identity of Constants Arrays
  • Inequality of Constants Arrays

Union Constant Arrays (+)

Union constant array uses the plus sign (+) to join two arrays. The joining of two arrays takes place at the index level. For example, we will take the two arrays, x, and y. Array x has four elements and array y has five elements. Now, we will union the x and y arrays using print_r($x+$y).

Example

Live Demo

<?php
   $x = array(11, 12, 13,14);
   $y = array('Rohan','Mohan','Thomas','John','Alex');
   define('rollno',$x);
   define('Stud_Name',$y);
   print_r("The union of the arrays are:");
   print_r($x+$y);
   define('College','rollno','Stud_Name');
   print("The constant array of rollno is at index 3 is:");
   print(rollno[3]);
   print("The constant array of Stud_name is at index 2 is:");
   print(Stud_Name[4]);
   print("The constant array of college is at index 4 is:");
   print(College[4]);
?>

Output

The output for the above program will be −

The union of the arrays are: Array
(
   [0] => 11
   [1] => 12
   [2] => 13
   [3] => 14
   [4] => Alex
)
The constant array of rollno is at index 3 is:14
The constant array of Stud_name is at index 2 is:Alex
The constant array of college is at index 4 is:Alex

Equality of Constant Arrays (==)

Equality of constant arrays takes the equal (==) operator to find the equality of the given arrays. The equal operator uses on the arrays at the index level as well as on the element values. Suppose we have two different arrays, x and y, with 4 and 5 elements. Then, we will check the equality between the x and y arrays using ($x==$y). If the given arrays are equal, then it will return true and if both the arrays are not equal, it will return false.

Example

Live Demo

<?php
   $x = array(11, 12, 13,14, 15);
   $y = array('Rohan','Mohan','Thomas','John','Alex');
   define('rollno',$x);
   define('Stud_Name',$y);
   print_r("The equality of the rollno arrays are:");
   var_dump('rollno'=='rollno'); //using equality    operator and result will be true
   print_r("The equality of the arrays are:");
   var_dump('rollno'=='Stud_Name'); //using equality operator and result will be true
?>

Output

Output for the above program will be −

The equality of the rollno arrays are:bool(true)
The equality of the arrays are:bool(false)

Identity of Constants Arrays (===)

Identity (===) operator is used to check whether the given arrays are identical or not. Suppose we have two constant arrays, x and y. If both the given arrays are using the same key and value pair that are the same types and are in the same order. Then the result will be true otherwise the result will be false.

Example

Live Demo

<?php
   $x = array('Rohan','Mohan','Thomas','John','Alex');
   define('Stud_Name',$x);
   print_r("The identity of the Stud_Name arrays are:");
   var_dump('Stud_Name'==='Stud_Name'); // Used identity (===) operator
?>

Output

Output for the above program will be −

The identity of the Stud_Name arrays are:bool(true)

Inequality of constants arrays (!=)

The inequality operator is used to check whether the given two arrays are equal or not. The inequality will take place at the arrays of the index level as well as on the values of the array elements.

Suppose we have two arrays, x and y. Array x has four elements and Array y has five elements, then we will check the inequality between x and y arrays. For example, if $x!=$y, the result will be true because the value of x and y do not match.

Example

Live Demo

<?php
   $x = array(11, 12, 13,14, 15);
   $y = array('Rohan','Mohan','Thomas','John','Alex');
   define('rollno',$x);
   define('Stud_Name',$y);
   print_r("the equality of the rollno arrays are:");
   var_dump('rollno'!='rollno');
   print_r("the equality of the arrays are:");
   var_dump('rollno'!='Stud_Name');
?>

Output

Output for the above program will be −

The equality of the rollno arrays are:bool(false)
The equality of the arrays are:bool(true)
raja
Published on 13-Mar-2021 07:19:25
Advertisements