PHP Operators

PHP Operators is a symbol which used to perform operations on operands.
$num=14+16;
Explanation:
Here + is the operator and 14,16 are operands $num is variable

PHP Operators Types:

  • Assignment Operators
  • Arithmetic Operators
  • Array Operators
  • Bitwise Operators
  • Comparison Operators
  • Logical Operators
  • String Operators
  • Incrementing/Decrementing Operators
  • Type Operators
  • Execution Operators
  • Error Control Operators

We can also categorize operators on behalf of operands. 

Binary Operators: works on two operands such as binary +, -, *, / etc.

Unary Operators: works on single operands such as ++, — etc.

Ternary Operators: works on three operands such as “?:”.

PHP Operators Precedence

OperatorsAdditional InformationAssociativity
[array()left
**arithmeticright
++ -- ~ increment/decrement and typesright
instanceotypesnon-associative
!logical (negation)right
* / %arithmetiarithmeti
+ - .arithmetic and string concatenationleft
<< >>bitwise (shift)left
< <= > >=comparisonnon-associative
!ERROR! illegal character '!'comparisonnon-associative
&bitwise ANDleft
^bitwise XORleft
|bitwise ORleft
&&logical ANDleft
||logical ORleft
?:ternaryleft
!ERROR! illegal character '%'assignment right
andlogicalleft
xorlogicalleft
orlogicalleft
,many uses (comma) left

Question and Answer-

  •  What will be the output of the following PHP code?
Output: 22

  • What will be the output of the following PHP code?
Finally Output: testthisalsothisalso

Explanation: The x .= y is a shorthand for x = x.y and this is evaluated from right to left.

  • What will be the output of the following PHP code?
Output: true 23

Explanation: x is pre-incremented and y is post-incremented thus both are 2 in the if condition, later y is incremented.

  • What will be the output of the following PHP code?
Output:
Explanation: First $y = 4 is compared to and then decremented, then incremented and compared to $y = 4