# Operations

## PHP

#### Arithmetic Operators

PHP 5
``````// assume \$a=5, \$b=2 for each line.

\$val = \$a + \$b;  // \$val -> 7
\$val = \$a - \$b;  // \$val -> 3
\$val = \$a * \$b;  // \$val -> 10
\$val = \$a / \$b;  // \$val -> 2.5
\$val = \$a % \$b;  // \$val -> 1
\$val = \$a++;     // \$val -> 5 then \$a -> 6
\$val = ++\$a;     // \$a -> 6 then \$val -> 6
\$val = \$b--;     // \$val -> 2 then \$b -> 1
\$val = --\$b;     // \$b -> 1 then \$val -> 1
\$val = \$a**\$b;   // \$val -> 25``````

`+`     sums left and right
`-`     subtracts right from left
`*`     multiplies left and right
`/`     divides left and right
`%`     reminder of the left divided by right
`x++`   returns `x` and then increments `x` by 1
`++x`   increments `x` by 1 and then returns `x`
`x--`   returns `x` and then decrements `x` by 1
`--x`   decrements `x` by 1 and then returns `x` `**`    raise of left to power of right

#### Comparison Operators

PHP 5
``````// assume \$a=5, \$b=2 for each line.

\$val = (\$a == \$b);     // \$val -> false
\$val = (\$a === \$b);    // \$val -> false
\$val = (\$a != \$b);     // \$val -> true
\$val = (\$a !== \$b);    // \$val -> true
\$val = (\$a > \$b);      // \$val -> true
\$val = (\$a >= \$b);     // \$val -> true
\$val = (\$a < \$b);      // \$val -> false
\$val = (\$a <= \$b);     // \$val -> false``````

`==`    is left and right equal
`===`   is left and right have same type and equal
`!=`    is left and right different
`!==`   is left and right have different type or value
`>`     is left bigger than right
`>=`    is left bigger or equal than right
`<`     is left smaller than right
`<=`    is left smaller or equal than right

PHP does implicit conversions between value types when necessary. By using `===` and `!==` you can make sure that on comparing values, the types of the values are considered too.

``````"2" == 2   // true
"2" === 2  // false  ``````

#### Spaceship Operator

PHP 7
``````// assume \$a=5, \$b=2

\$val = (\$a <=> \$b);  // \$val -> 1``````

`<=>`   This comparison operator returns:

• 1 if left is greater than right
• 0 if left is equal to right
• -1 if left is less than right

#### Logical Operators

PHP 5
``````// assume \$yes=true, \$no=false for each line.

\$val = (\$no && \$no);       // \$val -> false
\$val = (\$no && \$yes);      // \$val -> false
\$val = (\$yes && \$no);      // \$val -> false
\$val = (\$yes && \$yes);     // \$val -> true

\$val = (\$no and \$no);      // \$val -> false
\$val = (\$no and \$yes);     // \$val -> false
\$val = (\$yes and \$no);     // \$val -> false
\$val = (\$yes and \$yes);    // \$val -> true

\$val = (\$no || \$no);       // \$val -> false
\$val = (\$no || \$yes);      // \$val -> true
\$val = (\$yes || \$no);      // \$val -> true
\$val = (\$yes || \$yes);     // \$val -> true

\$val = (\$no or \$no);       // \$val -> false
\$val = (\$no or \$yes);      // \$val -> true
\$val = (\$yes or \$no);      // \$val -> true
\$val = (\$yes or \$yes);     // \$val -> true

\$val = (\$no xor \$no);      // \$val -> false
\$val = (\$no xor \$yes);     // \$val -> true
\$val = (\$yes xor \$no);     // \$val -> true
\$val = (\$yes xor \$yes);    // \$val -> false

\$val = (!\$yes);           // \$val -> false
\$val = (!\$no);            // \$val -> true``````

`&&`   logical and of left and right
`and`  logical and of left and right
`||`   logical or of left and right
`or`   logical or of left and right
`xor`  logical xor of left and right
`!`    logical not of its right operand

The reason for the two different variations of and, or operators is that they operate at different precedences.

#### Bitwise Operators

PHP 5
``````\$a=5;     // 0000 0101
\$b=12;    // 0000 1100

\$val = (\$a & \$b);  // \$val -> 4  (0000 0100)
\$val = (\$a | \$b);  // \$val -> 13 (0000 1101)
\$val = (\$a ^ \$b);  // \$val -> 9  (0000 1001)
\$val = (~\$b);      // \$val ->-13 (1111 0011)
\$val = (\$b<<2);    // \$val -> 48 (0011 0000)
\$val = (\$b>>2);    // \$val -> 3  (0000 0011)``````

`&`   bitwise and of left and right
`|`   bitwise or of left and right
`^`   bitwise xor or left and right
`~`   bitwise one complement of right operand
`<<`  bitwise left shift of left in amount of right
`>>`  bitwise right shift of left in amount of right

#### Assignment Operators

PHP 5
``````// assume \$a=5, \$b=2 for each line.

\$val = (\$a = \$b);     // \$val -> \$a -> 2
\$val = (\$a += \$b);    // \$val -> \$a -> 7
\$val = (\$a -= \$b);    // \$val -> \$a -> 3
\$val = (\$a *= \$b);    // \$val -> \$a -> 10
\$val = (\$a /= \$b);    // \$val -> \$a -> 2.5
\$val = (\$a %= \$b);    // \$val -> \$a -> 1
\$val = (\$a <<= \$b);   // \$val -> \$a -> 20
\$val = (\$a >>= \$b);   // \$val -> \$a -> 1
\$val = (\$a &= \$b);    // \$val -> \$a -> 0
\$val = (\$a |= \$b);    // \$val -> \$a -> 7
\$val = (\$a ^= \$b);    // \$val -> \$a -> 7``````

`=`    assigns left to right
`+=`   assigns left to sum of left and right
`-=`   assigns left to subtract of right from left
`*=`   assigns left to multiplication of left and right
`/=`   assigns left to division of left to right
`%=`   assigns left to reminder of left divided to right
`<<=`  assigns left to rightShift of left to value right
`>>=`  assigns left to leftShift of left to value right
`&=`   assigns left to bitwise and left and right
`|=`   assigns left to bitwise or left and right
`^=`   assigns left to bitwise xor left and right