Php operators are grouped in some sections, we gave the common operators in our previous article;

- Assignment
- Arithmetic
- Comparison
- Logical
- String
- Bitwise
- Error control
- Execution
- Incrementing / decrementing
- Array
- Type

**What is an operator?**

If you want to change a variables value, multiply two numbers, compare two variables you use operators. Operators do operations on variables and sometimes you use them in your functions also.

**Assignment Operators**

They set the left operands value from its right. If you assign a value to a varible you use = (equals) operator. $variable = 1453; It means assign 1453 to variable.

**Arithmetic Operators**

They are used for arithmetic operations like adding,subtracting,modulus and multiplying.

-$a | Negation | Opposite of .$a |

$a + $b | Addition | Sum of $aand .$b |

$a – $b | Subtraction | Difference of and $a.$b |

$a * $b | Multiplication | Product of and $a.$b |

$a / $b | Division | Quotient of and $a.$b |

$a % $b | Modulus | Remainder of divided by $a.$b |

$variable = (((3+7)*9+9)*10)%10;

$variable’s value is 0

$variable = 97%7;

$variable is 6

**Comparison Operators**

Used for comparing two values, they do not assign anything just compare the values. You may use comparison operators on different types of variables, like comparing string and integer variables, since php is a loosely typed language php does not give you any error, but you should be careful about comparing two variables. If you just want to check if a variable has “FALSE” boolean value you may try to use ($c == false), but if $c is 0 then comparison will be true but that s not what you expect! Ok, no problem php has a solution for this you may always try === for checking for variable type and value. Comparison operators are used everywhere in your code, if you are new to programming just learn them very well.

$a == $b | Equal | TRUE if is equal to $a.$b |

$a === $b | Identical | TRUE if is equal to $a, and they are of the same type. (introduced in PHP 4)$b |

$a != $b | Not equal | TRUE if is not equal to $a.$b |

$a <> $b | Not equal | TRUE if is not equal to $a.$b |

$a !== $b | Not identical | TRUE if is not equal to $a, or they are not of the same type. (introduced in PHP 4)$b |

$a < $b | Less than | TRUE if is strictly less than $a.$b |

$a > $b | Greater than | TRUE if is strictly greater than $a.$b |

$a <= $b | Less than or equal to | TRUE if is less than or equal to $a.$b |

$a >= $b | Greater than or equal to | TRUE if is greater than or equal to $a.$b |

**Logical Operators**

We use them for logical operations like xor,and, not,or, they return boolean values

$a and $b | And | TRUE if both and $a are $bTRUE. |

$a or $b | Or | TRUE if either or $a is $bTRUE. |

$a xor $b | Xor | TRUE if either or $a is $bTRUE, but not both. |

! $a | Not | TRUE if is not $aTRUE. |

$a && $b | And | TRUE if both and $a are $bTRUE. |

$a || $b | Or | TRUE if either or $a is $bTRUE. |

**String Operators**

Php has two string operators one of them is string concatenation operator and the other one is concatenating assignment operator.

String concatenating operator is . , and used for concatenating two strings.

$string_me = 'Izmir'; $string = 'Istanbul'; $string_new = 'Eskisehir'; $string_concat = $string_me.$string.$string_new; //concats,adds, strings to each other $string_concat is 'IzmirIstanbulEskisehir'

String concatenating assignment operator is .= , it appends argument on the right to the argument left.

$string = 'C++'; //$string value is 'C++' $string .= ' PHP'; // PHP is appended to $string , new value of the $string is 'C++ PHP'

**Bitwise operators**

Bitwise operators are used to evaluate and manipulate of specific bits within an integer. Multiplying by two and dividing by two can be made by bit shifting in Php with bitwise operators.

`$a & $b` |
And | Bits that are set in both and $a are set.$b |

`$a | $b` |
Or (inclusive or) | Bits that are set in either or $a are set.$b |

`$a ^ $b` |
Xor (exclusive or) | Bits that are set in or $a but not both are set.$b |

`~ $a` |
Not | Bits that are set in are not set, and vice versa.$a |

`$a << $b` |
Shift left | Shift the bits of $a steps to the left (each step means “multiply by two”)$b |

`$a >> $b` |
Shift right | Shift the bits of $a steps to the right (each step means “divide by two”)$b |

**Error Control Operators**

@ is the php error control operator which is used to ignore any error messages in expressions. If it is used in an expression (we put it inf front of expressions) error messages caused by expression will not be showed. I dont suggest this operator, dont try to use it. Dont try to hide errors or notices with @ operator. Try to solve your problems and then use error_reporting function or php.ini file to set your error reporting level.

$variable = @$_SESSION[$key]; //will not show you a notice if the index $key does not exist

**Execution operators**

Php uses backticks “ as execution operators, you may run any command by using execution operators. It is dangerous to use backticks to run commands on your system, be careful about backticks.

$showme = `ls /var/www`; echo $showme;

**Incrementing / decrementing operators**

++$a | Pre-increment | Increments by one, then returns $a.$a |

$a++ | Post-increment | Returns , then increments $a by one.$a |

–$a | Pre-decrement | Decrements by one, then returns $a.$a |

$a– | Post-decrement | Returns , then decrements $a by one.$a |

**Array operators**

$a + $b | Union | Union of and $a.$b |

$a == $b | Equality | TRUE if and $a have the same key/value pairs.$b |

$a === $b | Identity | TRUE if and $a have the same key/value pairs in the same order and of the same types.$b |

$a != $b | Inequality | TRUE if is not equal to $a.$b |

$a <> $b | Inequality | TRUE if is not equal to $a.$b |

$a !== $b | Non-identity | TRUE if is not identical to $a.$b |

**Type operators**

İnstance of is used as type operator, if we want to know if an object is instantiated from a certain class we use instanceof.

class Aircraft { } class Airbus extends Aircraft { } class Boeing extends Aircraft { } $airbus = new Airbus(); $boeing = new Boeing(); if($airbus instanceof Airbus) { echo "airbus object is instance of the Airbus class\n"; } else { echo "airbus object is not instance of Airbus class"; } if($boeing instanceof Airbus) { echo "boeing object is instance of Airbus class"; } else { echo "boeing object is not instance of Airbus class\n"; }

Output:

airbus object is instance of the Airbus class

boeing object is not instance of Airbus class

Php has three types of operators

- Unary operators : They operate on one value, like !,++,–
- Binary operators : They operate on two value, like assignment, increment and assignment, comparison, multiplying
- Tertiary operator : ?: It is used for comparison, it is used like in here ( $compare_me == $compare_me_second ) ? $we_are_same : $no_we_are_not_same; If $compare_me and $compare_me_second are same it returns $compare_me