Tuesday 14 August 2018

PHP Operators


What are operators?

  • Operators are symbols such as +, -, *, / etc.
  • An operator can take a value (or an expression) on its left or on its right or on both sides and based on a predefined rule, it produces a result.
  • For example + operator performs addition on the values on its both side.

What are operands?

  • Operands are data that can be manipulated.
  • Operands may be an literals (ex. 3), strings (ex. Wednesday), variables (ex. $var), constants (ex. M_PI), expressions (ex.$var=10 )

What are expressions?

  • Anything that yields a value is an expression. For example-
    1
    2
    3
    <?php
         $var = 10;
    ?>
    Here, In the above example, 10 is a value itself so it is an expression, again, 10 is assigned to the variable $var, so it also has the value 10. That’s why $var is also an expression.
  • Expression may consist of operands and operators;
    1
    2
    3
    <?php
        $var = 5 + 10;
    ?>

Operator types

Depending on the number of values (or expressions) it takes, operators can be grouped in three categories.
  1. Unary operators
  2. Binary operators and
  3. Ternary operators

Unary operators

These operators take only one value.
  1. Arithmetic negation operator (-)
  2. Bitwise not operator (~)
  3. Error control operators
  4. Increment/decrement operators
  5. Logical not operator (!)

Binary operators

These operators take two values.
  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. String operators
  6. Array operators

Ternary operator

This operator takes three values
  1. ?:

Arithmetic operators

  • Arithmetical operators are used to perform mathematical calculations.
  • Arithmetic operators are negation(-), addition(+), subtraction(-), multiplication(*), division(/), and modulus(%).
  • PHP also provides a lot of predefined mathematical functions that are capable of performing various mathematical operations.
The following table displays arithmetic operators-
Here, it is assumed that $a = 7 and $b = 2;
OperatorsNameExampleResultMeaning
Negation-$a;-7Opposite of $a
+Addition$a + $b;9Sum of $a and $b
Subtraction$a – $b;5Subtraction $b from $a
*Multiplication$a * $b;14Product of $a and $b
/Division$a / $b;3Quotient of $a and $b
%Modulus$a % $b;1Remainder of $a divided by $b
Example of arithmetic operators
1
2
3
4
5
6
7
8
9
<?php
    $a = 4;
    $b = 3;
    echo –a;  // -4
    echo $a + $b// 7
    echo $a - $b// 1
    echo $a * $b// 12
    echo $a / $b// 1
?>

  • The decimal parts of the operands of modulus are stripped before performing the modulus.
    1
    2
    3
    <?php
        echo 19 % 4.75; //  output 3
    ?>
    In the above example in line no 2, .75 is stripped from 4.75, then, 19 is divided by 4.
  • The sign of the result after the modulus will be the sign of the dividend.
    1
    2
    3
    <?php
        echo -5 % 3; //  output -2
    ?>
    Here, the dividend’s sign is minus, so there is minus before the result(-2)

Assignments operators

  • The assignment operator sets value of an expression that is on its right to the operand on its left.
  • The basic assignment operator is =
  • In the variable lesson, remember how we assigned a value to a variable? Check the following example-
    1
    2
    3
    <?php
        $today = “Wednesday”;
    ?>
    The above code is an example of using assignment operator. The assigning operator (=) is assigns the value Wednesday that is on its right to the variable $today that is on its left side.
  • In the above example = operator is used to assign value in a variable. To assign a value in an array key => operator is used. Check the following example
    1
    2
    3
    4
    5
    6
    7
    <?php
        $month = array(
        0 => “January”,
        1 => “February”,
        2 => “March”,
        )
    ?>

Combined operators

PHP supports combination of assignment operator with arithmetic, string, and array union operators. These combined operators provide a way of performing calculation on variable and reassigning the result to the variable. The following table displays arithmetic-assignment operators. The other two combinations will be discussed later.
OperatorsExampleEquivalent toMeaning
+=$a += 1;$a = $a + 1;Add 1 to $a and assign the result to $a
-=$a -= $b;$a = $a – 1;Subtract 1 from $a and assign the result to $a
*=*=$a = $a * 1;Multiply 1 by $a and assign the result to $a
/=$a /= $b;$a = $a / 1;Divide $a by 1 and assign the result to $a
%=$a %= $b;$a = $a % 1;Divide $a by 1 and assign the remainder to $a
Example:
1
2
3
4
<?php
    $a = 5;
    $a += 1;  // result: 6
?>
In the above example in line no 3 1 is add to variable $a (the value of $a is 5 here) and assign the value 6 to the same variable $a.

Bitwise operators

Computer uses binary system to represent anything. In binary system only two values 0 and 1 are used. These are bits. For example, the following table shows decimal 1 to 10 (that we use in daily life) in binary system-
Decimal012345678910
Binary011011100101110111100010011010
For easy calculation, combining eight bits and making a byte, computer represent characters, numbers etc. For example, the following table shows decimal 1 to 5 in bytes-
Decimal012345
Binary000000000000000100000010000000110000010000000101
If you look carefully in the above two tables, you’ll see that the difference in binary numbers between the two tables is the 0s at left side.
The following bitwise operators work with the corresponding bits in the two operands that it takes. That means, first bit of the left operand paired with first bit of the right operand, second with second, and so on.
OperatorsNameExampleMeaning
&Bitwise AND$a & $bReturns 1 in each bit position if both corresponding bits are 1, otherwise return 0
|Bitwise OR$a | $bReturns 1 in each bit position if any of the corresponding bits is 1, otherwise return 0
^Bitwise XOR$a ^ $bReturns 1 in each bit position if any of the corresponding bits is 1 but not both, otherwise return 0
~Bitwise NOT~$aInverts the bits in each position; 0 to 1, and 1 to 0.
Example of Bitwise AND
1
2
3
4
5
6
<?php
    $a = 4;
    $b = 5;
    $c = $a & $b;
    echo $c; // 4
?>
Explanation:
ExpressionBinaryDecimal
Left operand, $a=00000100=4
Right Operand, $b=00000101=5
Result, $c=00000100=4
As bitwise AND return 1 in a bit position if both operands have 1 in that position and as in the third position both operands have 1 in common, so 1 is returned in the third position in the result and 0s in all other positions. And, binary 00000100 is decimal 4; so, the result is 4.
Example of Bitwise OR
1
2
3
4
5
6
<?php
    $a = 4;
    $b = 5;
    $c = $a ! $b;
    echo $c; // 5
?>
Explanation:
ExpressionBinaryDecimal
Left operand, $a=00000100=4
Right Operand, $b=00000101=5
Result, $c=00000101=5
As bitwise OR return 1 in a bit position if any of the operands have 1 in that position and as in the first and the third positions there is 1 in any of the operands, so 1 is returned in those both positions in the result and 0s in all other positions. And, binary 00000101 is decimal 5; so, the result is 5.
Example of Bitwise XOR
1
2
3
4
5
6
<?php
    $a = 4;
    $b = 5;
    $c = $a ^ $b;
    echo $c; // 5
?>
Explanation:
ExpressionBinaryDecimal
Left operand, $a=00000100=4
Right Operand, $b=00000101=5
Result, $c=00000001=1
As bitwise XOR return 1 in a bit position if one of the operands have 1 in that position, but both don’t have and as in the first position there is 1 in right operand($b) but not in left operand($a), so 1 is returned in the first position in the result and 0s in all other positions. And, binary 00000001 is decimal 1; so, the result is 1.
Example of Bitwise NOT
1
2
3
4
5
<?php
    $a = 4;
    $c = ~$a;
    echo $c; // 5
?>
ExpressionBinaryDecimal
$a=Step 100000100=4
Step 2 (inversion)11111011
Step 3 (inversion)00000000
Step 4 (add 1)00000001
$c=Step 5 (result)00000101=-5
The first line shows the binary 4 (=00000100). After binary NOT operation, the bits are inverted in step 1. As the left most bit is 1, so it expresses a negative number in binary. To convert a negative number in decimal number, first we invert its bits (step 2), then add 1 (step 3). Thus the last result -5 (00000101) come out.

Bitwise shift operators

OperatorsNameExampleMeaning
<<Shift left$a << $bShift the bits of $a $b steps to the left
>>Shift right$a >> $bShift the bits of $a $b steps to the right
Example of Shift left operator
1
2
3
4
5
6
<?php
    $a = 4;
    $b = 1;
    $c = $a << $b;
    echo $c// 10
?>
ExpressionBinaryDecimal
$a=Step 100000100=4
$c=Step 2(After shifting 1 step left)00001000=8
In the above table, step 1 shows binary representation of decimal 4. After shifting 1 bit left, step 2 shows the result decimal 8.
Example of Shift left operator
1
2
3
4
5
6
<?php
    $a = 4;
    $b = 1;
    $c = $a >> $b;
    echo $c// 2
?>
ExpressionBinaryDecimal
$a=Step 100000100=4
$c=Step 2(After shifting 1 step right)00000010=2
In the above table, step 1 shows binary representation of decimal 4. After shifting 1 bit right, step 2 shows the result decimal 2.

0 comments:

Post a Comment