PHP 7 中的常量数组类型

phpserver side programmingprogramming更新于 2025/9/18 5:07:17

PHP 7 中有以下类型的常量数组 −

  • 常量数组的并集
  • 常量数组的相等性
  • 常量数组的同一性
  • 常量数组的不等性

常量数组的并集 (+)

常量数组的并集使用加号 (+) 连接两个数组。两个数组的连接在索引级别进行。例如,我们取两个数组 x 和 y。数组 x 有四个元素,数组 y 有五个元素。现在,我们将使用 print_r($x+$y) 将 x 和 y 数组并集。

示例

<?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]);
?>

输出

上述程序的输出将是 −

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

常量数组相等性 (==)

常量数组相等性使用相等运算符 (==) 来判断给定数组是否相等。相等运算符不仅作用于数组的索引级别,也作用于元素值。假设我们有两个不同的数组 x 和 y,分别包含 4 个元素和 5 个元素。然后,我们将使用 ($x==$y) 来检查 x 和 y 数组是否相等。如果给定数组相等,则返回 true;如果两个数组不相等,则返回 false。

示例

<?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
?>

输出

上述程序的输出为 −

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

常量数组的恒等式 (===)

恒等式 (===) 运算符用于检查给定数组是否相同。假设我们有两个常量数组 x 和 y。如果两个给定数组使用相同的键值对,且键值对的类型和顺序相同,则结果为 true,否则为 false。

示例

<?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
?>

输出

上述程序的输出为 −

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

常量数组的不等式 (!=)

不等式运算符用于检查给定的两个数组是否相等。不等式不仅在索引级别的数组上进行,也在数组元素的值上进行。

假设我们有两个数组 x 和 y。数组 x 有四个元素,数组 y 有五个元素,那么我们将检查 x 和 y 数组之间的不等式。例如,如果 $x!=$y,则结果为 true,因为 x 和 y 的值不匹配。

示例

<?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');
?>

输出

上述程序的输出为 −

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

相关文章