PHP Operators – Part1

Operators in programming languages are symbols that help to perform different types of tasks like arithmetical, logical, relational etc

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.

    PHP operators operands

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-
    <?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;
    <?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;

Operators Name Example Result Meaning
- Negation -$a; -7 Opposite of $a
+ Addition $a + $b; 9 Sum of $a and $b
- Subtraction $a – $b; 5 Subtraction $b from $a
* Multiplication $a * $b; 14 Product of $a and $b
/ Division $a / $b; 3 Quotient of $a and $b
% Modulus $a % $b; 1 Remainder of $a divided by $b

Example of arithmetic operators

<?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.
    <?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.
    <?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-
    <?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
    <?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.

Operators Example Equivalent to Meaning
+= $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:

<?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-

Decimal 0 1 2 3 4 5 6 7 8 9 10
Binary 0 1 10 11 100 101 110 111 1000 1001 1010

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-

Decimal 0 1 2 3 4 5
Binary 00000000 00000001 00000010 00000011 00000100 00000101

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.

Operators Name Example Meaning
& Bitwise AND $a & $b Returns 1 in each bit position if both corresponding bits are 1, otherwise return 0
| Bitwise OR $a | $b Returns 1 in each bit position if any of the corresponding bits is 1, otherwise return 0
^ Bitwise XOR $a ^ $b Returns 1 in each bit position if any of the corresponding bits is 1 but not both, otherwise return 0
~ Bitwise NOT ~$a Inverts the bits in each position; 0 to 1, and 1 to 0.

Example of Bitwise AND

<?php
    $a = 4;
    $b = 5;
    $c = $a & $b;
    echo $c; // 4
?>

Explanation:

Expression Binary Decimal
Left operand, $a = 0 0 0 0 0 1 0 0 = 4
Right Operand, $b = 0 0 0 0 0 1 0 1 = 5
Result, $c = 0 0 0 0 0 1 0 0 = 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

<?php
    $a = 4;
    $b = 5;
    $c = $a ! $b;
    echo $c; // 5
?>

Explanation:

Expression Binary Decimal
Left operand, $a = 0 0 0 0 0 1 0 0 = 4
Right Operand, $b = 0 0 0 0 0 1 0 1 = 5
Result, $c = 0 0 0 0 0 1 0 1 = 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

<?php
    $a = 4;
    $b = 5;
    $c = $a ^ $b;
    echo $c; // 5
?>

Explanation:

Expression Binary Decimal
Left operand, $a = 0 0 0 0 0 1 0 0 = 4
Right Operand, $b = 0 0 0 0 0 1 0 1 = 5
Result, $c = 0 0 0 0 0 0 0 1 = 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

<?php
    $a = 4;
    $c = ~$a;
    echo $c; // 5
?>
Expression Binary Decimal
$a = Step 1 0 0 0 0 0 1 0 0 = 4
Step 2 (inversion) 1 1 1 1 1 0 1 1
Step 3 (inversion) 0 0 0 0 0 0 0 0
Step 4 (add 1) 0 0 0 0 0 0 0 1
$c = Step 5 (result) 0 0 0 0 0 1 0 1 = -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

Operators Name Example Meaning
<< Shift left $a << $b Shift the bits of $a $b steps to the left
>> Shift right $a >> $b Shift the bits of $a $b steps to the right

Example of Shift left operator

<?php
    $a = 4;
    $b = 1;
    $c = $a << $b;
    echo $c;  // 10
?>
Expression Binary Decimal
$a = Step 1 0 0 0 0 0 1 0 0 = 4
$c = Step 2(After shifting 1 step left) 0 0 0 0 1 0 0 0 = 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

<?php
    $a = 4;
    $b = 1;
    $c = $a >> $b;
    echo $c;  // 2
?>
Expression Binary Decimal
$a = Step 1 0 0 0 0 0 1 0 0 = 4
$c = Step 2(After shifting 1 step right) 0 0 0 0 0 0 1 0 = 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.

‹‹ PHP Arrays : Previous Lesson Next Lesson: PHP Operators – Part2 ››


Pin It

↑ Back to Top