The array operators are used in the case of an array. These operators are used to compare the values of arrays.
Operator | Name | Example | Explanation |
+ | Union | $a + $b | Union of $a and $b |
== | Equality | $a == $b | Return TRUE if $a and $b have same key/value pair |
!= / <> | Inequality | $a != $b | Return TRUE if $a is not equal to $b |
=== | Identity | $a === $b | Return TRUE if $a and $b have the same key/value pair of the same type in the same order |
!== | Non-Identity | $a !== $b | Return TRUE if $a is not identical to $b |
+ Union array operator
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
Program of union array operator
<?php
$a=array("rahul"=>500,"sonam"=>600,"sumit"=>700); $b=array("rahul"=>"PHP","sonam"=>"java","sam"=>"CSS","JK"=>"book");
$result= $b+$a;
print_r($result);
?>
Output
Array ( [rahul] => PHP [sonam] => java [sam] => CSS [JK] => book [sumit] => 700 )
Explanation: -union arrays always print data from left to right, as you have seen ‘rahul’ key is assigned to both variables i.e. $a and $b. but the array will only give preference to a variable that is on the leftmost side and will ignore the right-side variable. The key and values which are not the same will print as usual.
= = Equality array operator
Inequality array operator only key: value pair should be same. Whether their arrangement is not in order but if the key: value is the same, the result will be true.
Program of equality array operator
<?php
$a=array("rahul"=>500,"sonam"=>600,"sumit"=>700); $x=array("rahul"=>500,"sonam"=>600,"sumit"=>700);
if ($a==$x):
echo "both array key/value are same";
else:
echo "key.value not same";
endif;
?>
Output
both array key/value is the same
If key: value is not the same, the result will be false
Program
<?php
$a=array("rahul"=>500,"sonam"=>600,"sumit"=>700); $b=array("rahul"=>"PHP","sonam"=>"java","sam"=>"CSS","JK"=>"book");
if ($a==$b):
echo "both array key/value are same";
else:
echo "key/value not same";
endif;
?>
Output
Key/value not same
If the key: value is not the same the result will become false.
= = = Identity array operator
In the identity array operator the key: value pair, the data types, and order of arrangement all should be the same only then the result will be true otherwise false.
Program of identity array operator if all are same
<?php
$a=array("rahul"=>500,"sonam"=>600,"sumit"=>700); $x=array("rahul"=>500,"sonam"=>600,"sumit"=>700); $b=array("rahul"=>"PHP","sonam"=>"java","sam"=>"CSS","JK"=>"book");
if ($a===$x):
echo "both array key/value/order/data-types are same";
else:
echo "key/value/order/data-types are not same";
endif;
?>
Output
key/value/order/data-types are same
program if values, data-types are not same
<?php
$a=array("rahul"=>500,"sonam"=>"600","sumit"=>700); $b=array("rahul"=>"PHP","sonam"=>"java","sam"=>"CSS","JK"=>"book");
if ($a===$x):
echo "both array key/value/order/data-types are same";
else:
echo "key/value/order/data-types are not same";
endif;
?>
Output
key/value/order/data-types are not same
!= Inequality array operator
Inequality array operator returns true if data-types and key:value are not same. And return false if they are same.
Program of inequality array operator
<?php
$a=array("rahul"=>500,"sonam"=>"600","sumit"=>700); $b=array("rahul"=>"PHP","sonam"=>"java","sam"=>"CSS","JK"=>"book");
if ($a!=$b):
echo "result is false";
else:
echo "result is true";
endif;
?>
Output
it will be “result is false”.
If they are the same output will be ‘result is true’.
<?php
$a=array("rahul"=>500,"sonam"=>"600","sumit"=>700); $x=array("rahul"=>500,"sonam"=>600,"sumit"=>700);
if ($a!=$x):
echo "result is false";
else:
echo "result is true";
endif;
?>
!== non-identity array operator
When order/Data-types/key:value is not same, the result is true otherwise false.
Program of non-identity array operator
<?php
$a=array("rahul"=>500,"sonam"=>"600","sumit"=>700); $b=array("rahul"=>"PHP","sonam"=>"java","sam"=>"CSS","JK"=>"book");
if ($a!==$b):
echo "they are not identical";
else:
echo "they are identical";
endif;
?>
Conditional Operator
There is one more operator called conditional operator. The conditional operator just works like the ‘if else’ condition. But we do not write exactly ‘if else’. The conditional operator has true values as result one is true and the other is false. The conditional operator has this syntax –
Operator | Description | Example |
? : | Conditional Expression | If Condition is true? Then value X: Otherwise value Y |
Syntax
(condition)? True statement : False statement
- If the condition is true the True statement will run.
- If the condition is false False statement will run.
- In conditional operators, we can only give only one condition at a time.
- But in ‘if else’ we can print multiple statements.
For example, if we use conditional operator: –
$x = 15;
$z = ($x > 10) ? $z = “Greater” : $z = “Smaller”;
Echo $z;
In the above example if condition,$x = 15, is greater than $x = 10 the result will be ‘Greater’, otherwise, the result will be ‘Smaller’.
Note :- the condition is not necessarily to be taken in ( ). You can also write them without ( ).
For example, the same code can be written as this also.
$x > 10 ? $z = “Greater” : $z = “Smaller”;
we can also write our condition with some other statements. For example,
$x = 15;
$z = “value is : ” . ($x > 10 ? $z = “Greater” : $z = “Smaller”);
Echo $z;
Conditional operators are only used where you have to print only one statement. But if you have to print multiple statements, then we have to use ‘if else’ statements.
Type Operators
The instance of operator is used in PHP to find out if an object is an instantiated instance of a class or not.
Syntax:
$a instanceof MyClass
In this, we will find out if MyClass is an instanceof $a object or not.
Operands: This operator contains two operands which are listed below:
- $a: This is used as an object.
- MyClass: It is a class name.
Return value: It returns True if the object is of this class or has this class as one of its parents else it will return a False value.
Program to understand
<?php
Class MyClass
{ }
Class NotMyClass
{ }
$a= new MyClass;
Var_dump($a instanceof MyClass);
Var_dump($a instanceof NotMyClass);
?>
The above will example will output:
- bool(true)
- Bool(false)
Explanation: – In the above program there are 2 classes named ‘MyClass’ and ‘NotMyClass’. Now define a variable and check the dump value of $a. If ‘MyClass’ matches its object $a the output will be True. Otherwise, it will be False.