Operations in PHP

In PHP, we can use various operators: arithmetic, logical, etc. Let’s look at each type of operation.

Arithmetic operations

  • + (addition operation) For example: $a + 5
  • – (subtraction operation) For example: $a – 5
  • * (multiplication) For example : $a * 5
  • / (division) For example: $a / 5
  • % (remainder of division) For example: $a = 12; echo $a % 5; // equals 2
  • ** (exponentiation) For example: $a ** 2

Operation examples:

  
$a = 8 + 2; // 10, addition
$a = 8 - 2; // 6, subtraction
$a = 8 * 2; // 16, multiply
$a = 8 / 2; // 4, division
$a = 8% 2; // 0, modulo
$a = 8 ** 2; // 64, exponentiation

increment and decrement

Separately, it should be said the increment and decrement operations, are also arithmetic operations but are performed on one operand.

Increment – the ++ operation increments a number by one. For example, ++$a

There are two types of increment: prefix increment ( ++$a) and postfix increment ( $a++). It is important to understand the difference between these operations. Consider first the prefix increment:

 
$a = 12;
$b = ++$a; // $b equals 13
echo "a = $a   b = $b";

Result of work:

 
a = 13 b = 13

Here, first, one is added to the value of the variable $a, and then its value is equalized to the variable $b.

Now let’s see what happens in the case of a postfix increment:

 
$a = 12;
$b = $a++; // $b equals 12
echo "a = $a   b = $b";

Result of work:

 
a = 13 b = 12

Here, first the value of the variable $a is passed to the variable $b, and then the value of the variable $a is incremented.

The decrement operation — represents decreasing a value by one. It can be prefix and postfix, similar to the increment, and works similarly. For example, prefix decrement:

 
$a = 12;
$b = --$a; // $b equals 11
echo "a = $a   b = $b";

Result of work:

 
a = 11 b = 11

First, the value of $a is decremented by one, and then its value is set equal to $b.

Postfix decrement:

  
$a = 12;
$b = $a--; // $b equals 12
echo "a = $a   b = $b";

Result of work:

 
a = 11 b = 12

Here, the value of $a is first passed to $b, and only then is it decremented by one.

String Concatenation

The dot operator is used to concatenate strings. For example, let’s join several lines:

  
$a="Hi, ";
$b="world";
echo $a . " " . $ b . "!"; // Hello World!

If the variables do not represent strings, but other types, such as numbers, then their values ​​are converted to strings and then the string concatenation operation also occurs.

Comparison operations

Comparison operations, as a rule, are used in conditional constructions when it is necessary to compare two values, and, depending on the result of the comparison, perform some actions. The following comparison operations are available.

  • == The equality operator compares two values, and if they are equal, it returns true, otherwise it returns false:$a == 5
  • === The identity operator also compares two values, and if they are equal, it returns true, otherwise it returns false:$a === 5
  • != Compares two values, and if they are not equal, returns true, otherwise returns false:$a != 5
  • !== Compares two values, and if they are not equal, returns true, otherwise returns false:$a !== 5
  • > Compares two values, and if the first is greater than the second, then returns true, otherwise returns false:$a > 5
  • < Compares two values, and if the first is less than the second, then returns true, otherwise returns false:$a < 5 >= Compares two values, and if the first is greater than or equal to the second, then returns true, otherwise returns false:$a >= 5
  • <= Compares two values, and if the first is less than or equal to the second, then returns true, otherwise returns false:$a <= 5

Equality and Identity Operator

Both operators compare two expressions and return true if the expressions are equal. But there are differences between them. If two values ​​of different types are taken in the equality operation, then they are reduced to one – the one that the interpreter finds optimal. For example:

   
$a = (2 == "2"); // true (values ​​are equal)
$b = (2 === "2"); // false (values ​​represent different types)

The string “2” essentially represents the same value as the number 2, the comparison operator will return true. However, they represent different types, so the identity operator will return false

!= The inequality operators and operate similarly !==.

 
$a = (2 != "2"); // false since the values ​​are equal
$b = (2 !== "2"); // true since the values ​​represent different types

<=> operator

Separately, it is worth mentioning the <=> operator. It also compares two values ​​and returns

0 if both values ​​are equal

1 if the value on the left is greater than the value on the right

-1 if the value on the left is less than the value on the right

Application:

 
$a = 2 <=> 2; // 0 (equivalent to 2 == 2)
$b = 3 <=> 2; // 1 (equivalent to 3 > 2)
$c = 1 <=> 2; // -1 (equivalent to 1 < 2)
echo "a=$a b=$b c=$c"; // a=0 b=1 c=-1

Boolean operations

Boolean operations are typically used to combine the results of two comparison operations. For example, we need to perform a certain action if several conditions are true. The following logical operations are available:

  • && Returns true if both comparison operations return true, otherwise return false:$a && $b
  • and Similar operation &&:$a and $b
  • || Returns true if at least one comparison operation returns true, otherwise returns false:$a || $b
  • or Similar operation ||:$a or $b
  • ! Returns true if the comparison operation returns false:!$a
  • xor Returns true if only one of the values ​​is true. If both are true or neither is true, returns false:$a xor $b

Examples of logical operations:

 
$a = (true && false); // false
// similar
$a = (true and false); // false

$b = (true || false); // true
// similar to the following operation
$b = (true or false); // true

$c = !true;           // false

Separate examples with the operation xor:

 
$a = (true xor true);   //  false
$b = (false xor true);  //  true
$c = (false xor false); //  false

Bitwise operations

Bitwise operations are performed on individual bits or bits of a number. Numbers are considered in binary representation, for example, 2 in binary representation is 010, and the number 7 is 111.

& (boolean multiplication)

Multiplication is performed bit by bit, and if both operands have bit values ​​equal to 1, then the operation returns 1, otherwise the number 0 is returned. For example:

 
    $a = 4; //100
$b = 5; //101
echo $a & $b; // equals 4 - 100

Here, the number 4 in binary is 100, and the number 5 is 101. Multiply the numbers bit by bit and get (1*1, 0*0, 0*1) = 100, that is, the number 4 in decimal.

| (logical addition)

Similar to logical multiplication, the operation is also performed on binary digits, but now a one is returned if at least one number in this bit has a one. For example:

  
$a = 4; //100
$b = 5; //101
echo $a | $b; // equals 5 - 101

Add the numbers bit by bit and get (1+1, 0+0, 0+1) = 101, that is, the number 5 in decimal format.

^ (exclusive OR operation)

Returns one if both operands have different values ​​of the corresponding bits. But if both operands have the same bit value, then 0 is returned. For example:

 
    $a = 5^4; // 101^100=001 - decimal 1
$b = 7^4; // 111^100=011 - decimal 3

Let’s perform the operation 5 ^ 4bit by bit (in the binary system, it is similar to the operation 101^100): (1^1, 0^0, 0^1) = 001. In the case of c, the 1 ^1values ​​of the bits are the same, so 0 is returned. In the second case, 0^0the values ​​are also the same, so it is also returned 0. In the third case, 0^1the values ​​are different, so 1 is returned. The result is 001, or the number 1 in decimal.

~ (logical negation)

inverts all digits: if the value of the digit is 1, then it becomes zero, and vice versa.

 
$a = 4; //00000100
$b = ~$a; //11111011 -5
echo $b; // equals -5

<<

x << y – shifts the number x to the left by y digits. For example, 4<<1 shift the number 4 (which is 100 in binary) one digit to the left, resulting in 1000 or 8 in decimal >>

x>>y – shifts the number x to the right by y digits. For example, 16>>1 shift the number 16 (which is 10000 in binary) one digit to the right, resulting in 1000 or 8 in decimal

(=) Assignment operations

Sets a variable to a specific value: $a = 5

+=

Addition followed by the assignment of the result. For example:

 
$a=12;
$a += 5;
echo $a; // equals 17

-=

Subtraction followed by assignment of the result. For example:

 
$a=12;
$a -= 5;
echo $a; // equals 7

*=

multiplication followed by the assignment of the result:

 
$a=12;
$a *= 5;
echo $a; // equals 60

/=

Division followed by assignment of the result:

 
$a=12;
$a /= 5;
echo $a; // equals 2.4

.=

Concatenation of strings with the assignment of the result. Applies to two lines. If the variables do not store strings, but, for example, numbers, then their values ​​are converted to strings and then the operation is performed:

 
    
    $a=12;
$a .= 5;
echo $a; // equals 125
// identical
$b="12";
$b .="5"; // equals 125

%=

Getting the remainder of a division and then assigning the result:

 
$a=12;
$a %= 5;
echo $a; 

 **=

Getting the result from exponentiation:

 
$a=8;
$a **= 2;
echo $a; 

 &=

Getting the result from the logical multiplication operation:

 
$a=5; $a &= 4; // 101&100=100 - 4

|=

Getting the result from the logical addition operation:

 
$a=5; $a |= 4; // 101|100=101 - 5

^=

Getting the result from the XOR operation:

 
$a=5; $a ^= 4; // 101^100=001 - 1

<<=

Getting the result of a left shift operation:

 
$a=8; $a <<= 1; // 1000 << 1 = 10000 - 16

>>=

Getting the result of a right shift operation:

 
$a=8; $a >>= 1; // 1000 >> 1 = 100 - 4

Operation Priority

If one expression contains several different operations, then the priority of operations is taken into account when executing the expression. Operations with higher priority are executed first and operations with lower priority are executed at the end.

The priority of operations can be described by the following table:

**
++ — ~
!
* / %
+ – .
<< >>
< > <= >=
== != === !== << <=<
&
^
|
&&
||
?
: (ternary operator)
= += -= *= **= /= .= %= &= |= ^= <<= >>=
(assignment operators)
and
xor
or

The higher the operator in this table, the greater its precedence. For example:

 
$a = 10 + 5 * 2;    // 20

Multiplication has higher precedence than addition. Therefore, the subexpression is executed first5 * 2

In this case, brackets increase the precedence of the operation used in the expression.

 
$a = (10 + 5) * 2;  // 30

In this case, the subexpression is executed first, 10 + 5and only then the multiplication.