# Operators in PHP | PHP Operators

**Operators in PHP | PHP Operators**

There are three types of operators:

- Firstly there is the
**unary operato**r which operates on only one value. (For Example,**++ increment operator**) - The second group is termed as
**Binary operator**s; this group contains most of the operators that PHP supports. (For Example logical, conditional operator etc.) - The third group is the ternary operator ? : . IT should be used to select between two expressions depending on a third one.

**1. Arithmetic Operators in PHP: **

Example |
Name |
Result |

-$X | Negation | Opposite of $X |

$X + $Y | Addition | Sum of $X and Â $Y |

$X – $Y | Subtraction | Difference of $X and $Y |

$X *$Y | Multiplication | Product of $X and $Y |

$X / $Y | Division | Quotient of $X and $Y |

$X % $Y | Modulus | Remainder of $X divided by $Y |

Example:

<?php

$X Â = 5;

$Y = 3;

echo ( $X + $Y) , “<br>” ;

echo ( $X – $Y) , “<br>” ;

echo ( $X * $Y) , “<br>” ;

echo ( $X / $Y) , “<br>” ;

echo ( $XÂ % Â $Y) ;

?>

**2. Increment / Decrement Operator in PHP : **

Example |
Name |
Result |

++$X | Pre Increment | Increase $X by one and then returns $X. |

$X++ | Post Increment | Returns $X and then increase $X by one. |

– -$X | Pre Decrement | Decrease $X by one and then returns $X. |

$X- – | Post Decrement | Returns $X and then decrease $X by one. |

Example:

<?php

$X =5;

Echo ++$X ,Â “<br>”;$X =5;

Echo Â $X++ , “<br>”;$Y =9;

Echo Â – -$Y , “<br>”;$Y =5;

EchoÂ $Y- – , “<br>”;

?>

**3. Assignment Operators in PHP: **

Operator |
Example |
IS the same as |

= |
$X = $Y | $X = $Y |

+= |
$X += $Y | $X = $X + $Y |

-= |
$X -= $Y | $X = $X – $Y |

*= |
$X *= $Y | $X = $X * $Y |

/= |
$X /= $Y | $X = $X / $Y |

%= |
$X %= $Y | $X = $X % $Y |

Example:

<?php

$A = 10;

$A += 15;

echo Â $A , “<br>”;$B = 20;

echo $B -=5 ,Â “<br>”;$C = 10;

echo $C *= 2 , “<br>”;$D Â = 15;

echo $D /=4 ,”<br>”;$E = 5;

echo $E %=3 ;

?>

**4. Comparison Operators in PHP : **

Example |
Name |
Result |

$P == $Q | Equal | True if $P is equal to $Q. |

$P === $Q | Identical | True if $P is equal to $Q with the same type. |

$P != $Q | Not Equal | True if $P is not equal to $Q. |

$P <> $Q | Not Equal | True if $P is not equal to $Q. |

$P !== $Q | Not Identical | True if $P is not equal to $Q or they are not of the same type. |

$P < $Q | Less than | True if $P is less than $Q. |

$P <= $Q | Less than or equal to | True if $P is less than or equal to $Q. |

$P > $Q | Greater than | True if $P is greater than $Q. |

$P >= $Q | Greater than or equal to | True if $P is greater than or equal to $Q. |

**var_dump() function** in php is used to display structured information with its type & value about the one or more variables.

Example:

<?php

$X = 100;

$Y = “100”;

echo var_dump ($X == $Y);

echo var_dump ($X === $Y);

echo var_dump ($X != $Y);

echo var_dump ($X !== $Y);$X5= 5;Â $Y = 4;

echo var_dump ($X > $Y);

echo var_dump ($X < $Y);

?>

**5. Logical Operators in PHP : **

Example |
Name |
Result |

$P and $Q | And | True if both $P and $Q are true. |

$P or $Q | Or | True if either $P or $Q is true. |

$P xor $Q | Xor | True if either $P or $Q is True but not both. |

!$P | Not | True if $P is not true. |

$P && $Q | And | True if both $P and $Q are true. |

$P || $Q | Or | True if either $P or $Q is true. |

**6. Ternary Operator in PHP:**

- One type of conditional operator which takes three expression and use truth value of first expression to decide which of the other two expression to evaluate and return.
**Syntax**:Â Test expression ? yes expression : no expression- The value of this expression is the result of yes expression if test expression is true otherwise no expression.

Example:

<?php

$A = 11;

$B = 22;

$A > $B?$ans = “A is Big”:$ans= “B is Big”;

echo “<br>” , $ans;

?>

**7. String Operators in PHP:**

Two string operators are there. First string operator is â€˜**concatenation operator (.)** â€˜ which returns the concatenation of its left and right arguments.

Second is â€˜**Concatenation assignment operator ( := ) ‘** which appends the argument on the right side to the argument on the left side.

Example:

<?php

$X Â = “Hello”;

$Y = $X.“Welcome”;

echo $Y;$Z Â = “Welcome”;

$Z.=” To Code.Freefeast.info”;

echo “<br>” , $Z;

?>