In this lesson, you will learn about the Array Operations in PHP, and their usages, along with examples to better understand the topic.
The Array operators in PHP are used to compare arrays content. The table below illustrates the Array Operations and simple examples for each operation.
Operators Symbol | Array Operators’ Names | Example |
---|---|---|
+ | Union | $a + $b Union of $a and $b, result will be both $a and $b |
= | Equality | $a == $b The same key exists if $a and $b and value pairs, result will be true as bool(true) |
!ERROR! A4 -> Formula Error: An unexpected error occurred | Identity | $a === $b The same key exists if $a and $b and value pairs are of the same types also in the same order, the result will be bool(true) |
!= | Inequality | $a != $b if $a is not equal to $b , it will return true |
<> | Inequality | $a <> $b if $a is not equal to $b then it will return true |
!== | Non-identity | $a !== $b if $a is not identical to $b then it will return true |
Example
<?PHP echo "PHP Array Operator Example"; echo "<br>"; echo " ---------------------------------------- "; echo "<br>"; $array_one = array( "one" => "PHP", "two" => "C" ); $array_two = array( "three" => "JAVA", "four" => "COBOL" ); //union of array one and two will display two arrays together echo "Array one =", "<br>"; print_r($array_one); echo "<br>"; echo "Array two =", "<br>"; print_r($array_two); echo "<br>"; echo " ------------Union(+)------------------ "; echo "<br>"; print_r($array_one + $array_two); echo "<br>"; echo " ------------Equality(==)------------------ "; echo "<br>"; var_dump($array_one == $array_two); echo "<br>"; echo " ------------Identity(===)------------------ "; echo "<br>"; var_dump($array_one === $array_two); echo "<br>"; echo " ------------Inequality(!=)------------------ "; echo "<br>"; var_dump($array_one != $array_two); echo "<br>"; echo " ------------Inequality(<>)------------------ "; echo "<br>"; var_dump($array_one <> $array_two); echo "<br>"; echo " ------------Non-identity(!==)------------------ "; echo "<br>"; var_dump($array_one !== $array_two); ?>
Output
PHP Array Operator Example ---------------------------------------- Array one = Array ( [one] => PHP [two] => C ) Array two = Array ( [three] => JAVA [four] => COBOL ) ------------Union(+)------------------ Array ( [one] => PHP [two] => C [three] => JAVA [four] => COBOL ) ------------Equality(==)------------------ bool(false) ------------Identity(===)------------------ bool(false) ------------Inequality(!=)------------------ bool(true) ------------Inequality(<>)------------------ bool(true) ------------Non-identity(!==)------------------ bool(true)
This concludes the PHP Array Operators lesson. In the next lesson, you will learn about if…else statement in PHP.