PHP Operator Type

PHP Operators let to perform operations such as addition and subtraction .PHP provide different operators to play with variables.for example (  logical operators,arithmetic operators and  comparison ).

There are two operators  types : binary and unary.

Binary needs two operands to do the operation while unary requires only one.

Binary needs 2 operands to perform the operation.
Unary needs 1 operands top perform the operation.

Arithmetic Operator

These are the arithmetic operators in PHP language  −

See this Example

Operator Description Example
+ It add the Values (Single or Multiple) $a + $b   
- It subtract the values  (Single or Multiple) $a  -  $b  
* It multiply the values (Single or Multiple) $a  *  $b  
/ It divide the values (Single or Multiple) $a  /  $b  
% Modulus Operator and remainder of after an integer division $a  %  $b  
++ It increment the value $a++  
-- It decrements the value $a-- 
 
< ?php
$a = 10;
$b= 4;
echo($a+ $b); // result: 14
echo($a- $b); // result: 6
echo($a* $b); // result: 40
echo($a/ $b); // result: 2.5
echo($a % $b); // result: 2
?>

Assignment Operators

Assignment operator used to assign the values .

Operator Description Example
= Assign the value $a = $b   
+= Assign and add $a+=$b  
-= Subtract and assign $a-=$b  
*= Multiply and assign $a*=$b  
/= Assign and divide $a /=$b  
%= Assign quotient and devide $a %= $b            
 
 
< php
$x = 10; echo $a; // result: 10 ?>


Comparison Operators

Comparison operator used to compare the values 

Operator Description Example
== Compare two values  $a == $b   
!= It check that values is not equal to value $a !$b  
> It check if value is grater then this or not $a $b  
< It check if value if less than this or not $a < $b  
>= It check if value equal to or grater than this $a >= $b  
<= It check if value is equal to or less than this $a <= $b            
  

< ?php
$a=10; $b=10; if ($a == 10) { echo 'true'; } if ($a != 10 ) { echo 'false'; } if ($a > 10) { echo 'false'; } if ($a < 10) { echo 'false'; } if ($a >= 10) { echo 'true'; } if ($a <= 10) { echo 'true'; } ?>
      

Logical Operators

There are logical operators

Operator Description Example
and If both operands are true  AND false $a  AND $b   
or If both operands are true  OR false $a OR $b  
&& If both operands are true  AND false (If NON Zero) $a && $b  
|| If both operands are true  OR false (If NON Zero) $a || $b  
! If NOT ,but condition is true $a ! $b  
 
Example:


< ?php
$a=10; $b=10; if ($a == 10 AND $b == 10) { echo 'true'; } if ($a == 10 OR $b == 10) { echo 'true'; } if ($a == 10 && $b == 10) { echo 'true'; } if ($a == 10 || $b == 10) { echo 'true'; } ?>
 

Increment and Decrement Operators

++$xPre-incrementIncrement $x and then return $x
$x++Post-incrementReturn $x first and then increment $x
--$xPre-decrementDecrement $x first and then return $x
$x--Post-decrementReturn $x first and then decrement $x

Increment and decrements operators Example:

<?php
echo "<h3>Post increment example</h3>";
$x = 5;
echo "Should be 5: " . $x++ . "<br />\n";
echo "Should be 6: " . $x . "<br />\n";

echo "<h3>Pre increment Example </h3>";
$x = 5;
echo "Should be 6: " . ++$x . "<br />\n";
echo "Should be 6: " . $x . "<br />\n";

echo "<h3>Post decrement Example</h3>";
$x = 5;
echo "Should be 5: " . $x-- . "<br />\n";
echo "Should be 4: " . $x . "<br />\n";

echo "<h3>Pre decrement Example</h3>";
$x = 5;
echo "Should be 4: " . --$x . "<br />\n";
echo "Should be 4: " . $x . "<br />\n";
?>
 

Array Operators

Example Name Result
$x+ $y Union Union of $x$y.
$x == $y Equality TRUE if $xand $y have the same key/value pairs.
$x=== $y Identity TRUE if $x and $y have the same key/value pairs in the same order  .
$x != $y Inequality TRUE if $x is not = to $y .
$x <> $y Inequality TRUE if $x is not = to $y .
$x !== $y Non-identity TRUE if $x is not identical to $y .

Example:
<?php
$x = array("a" => "banana", "b" => "peach");
$y = array("a" => "mango", "b" => "strawberry", "c" => "watermelon");

$c = $x + $y; // Union of $x and $y
echo "Union of \$x and \$y: \n";
var_dump($c);

$c = $y + $x; // Union of $y and $x
echo "Union of \$y and \$x: \n";
var_dump($c);

$x += $y; // Union of $x += $y is $x and $y
echo "Union of \$x += \$y: \n";
var_dump($x);
?>
 

Type Operators

Example:
<?php
class MainClass
{
}

class MyClass extends MainClass
{
}

$a = new MyClass;

var_dump($a instance of MyClass);
var_dump($a instance of MainClass);
?>

Output:
bool(true)
bool(true)

 

Execution Operators

PHP supports execution operator (``) . You can find this symbol below [esc] button.
 
Example
<?php
$result= `ls -al`;
echo "<pre>$result</pre>";
?>
 

Error Control Operators

PHP supports one error control with  sign/symbol (@).

Example
<?php
/* Intentional file error */
$file= @file ('non_existent_file') or    die ("Failed  to open file");

// this works for any expression, not just functions:
$data_value = @$cache[$key];
 

?>
 



See this video to know in more detail

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :