เรามีประเภทของอาร์เรย์ค่าคงที่ใน PHP 7 ดังนี้ -
- Union of Constant Arrays
- Equality of Constants Arrays
- Identity of Constants Arrays
- Inequality of Constants Arrays
Union Constant Arrays (+)
Union constant array ใช้เครื่องหมายบวก (+) เพื่อรวมอาร์เรย์สองตัว การรวมเกิดขึ้นที่ระดับดัชนี (index level) ตัวอย่าง เราจะใช้อาร์เรย์สองตัว x และ y อาร์เรย์ x มี 4 องค์ประกอบ และอาร์เรย์ y มี 5 องค์ประกอบ ตอนนี้เราจะ union x และ y โดยใช้ print_r($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
Equality of Constant Arrays (==)
Equality of constant arrays ใช้ตัวดำเนินการเท่ากัน (==) เพื่อหาความเท่ากันของอาร์เรย์ที่กำหนด ตัวดำเนินการเท่ากันใช้กับอาร์เรย์ที่ระดับดัชนีและค่าองค์ประกอบ สมมติเรามีอาร์เรย์สองตัว 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'); // ใช้ตัวดำเนินการเท่ากันและผลลัพธ์จะเป็น true
print_r("The equality of the arrays are:");
var_dump('rollno'=='Stud_Name'); // ใช้ตัวดำเนินการเท่ากันและผลลัพธ์จะเป็น true ?>
ผลลัพธ์
ผลลัพธ์สำหรับโปรแกรมด้านบนจะเป็น -
The equality of the rollno arrays are:bool(true) The equality of the arrays are:bool(false)
Identity of Constants Arrays (===)
ตัวดำเนินการ Identity (===) ใช้ตรวจสอบว่าอาร์เรย์ที่กำหนดเหมือนกันหรือไม่ สมมติเรามีอาร์เรย์ค่าคงที่สองตัว 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'); // ใช้ตัวดำเนินการ Identity (===) ?>
ผลลัพธ์
ผลลัพธ์สำหรับโปรแกรมด้านบนจะเป็น -
The identity of the Stud_Name arrays are:bool(true)
Inequality of constants arrays (!=)
ตัวดำเนินการไม่เท่ากันใช้ตรวจสอบว่าอาร์เรย์สองตัวที่กำหนดเท่ากันหรือไม่ ความไม่เท่ากันจะเกิดขึ้นที่ระดับดัชนีของอาร์เรย์และค่าขององค์ประกอบอาร์เรย์
สมมติเรามีอาร์เรย์สองตัว x และ y อาร์เรย์ x มี 4 องค์ประกอบ และอาร์เรย์ y มี 5 องค์ประกอบ จากนั้นเราจะตรวจสอบความไม่เท่ากันระหว่าง 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)