Operators

Using variables is nice, but if we cannot make them interact with each other, there is nothing much we can do. Operators are elements that take some expressions—operands—and perform actions on them to get a result. The most common examples of operators are arithmetic operators, which you already saw previously.

An expression is almost anything that has a value. Variables, numbers, or text are examples of expressions, but you will see that they can get way more complicated. Operators expect expressions of a specific type, for example, arithmetic operators expect either integers or floats. But as you already know, PHP takes care of transforming the types of the expressions given whenever possible.

Let's take a look at the most important groups of operators.

Arithmetic operators

Arithmetic operators are very intuitive, as you already know. Addition, subtraction, multiplication, and division (+, -, *, and /) do as their names say. Modulus (%) gives the remainder of the division of two operands. Exponentiation (**) raises the first operand to the power of the second. Finally, negation (-) negates the operand. This last one is the only arithmetic operator that takes just one operand.

Let's see some examples:

<?php
$a = 10;
$b = 3;
var_dump($a + $b); // 13
var_dump($a - $b); // 7
var_dump($a * $b); // 30
var_dump($a / $b); // 3.333333...
var_dump($a % $b); // 1
var_dump($a ** $b); // 1000
var_dump(-$a); // -10

As you can see, they are quite easy to understand!

Assignment operators

You already know this one too, as we have been using it in our examples. The assignment operator assigns the result of an expression to a variable. Now you know that an expression can be as simple as a number, or, for example, the result of a series of arithmetic operations. The following example assigns the result of an expression to a variable:

<?php
$a = 3 + 4 + 5 - 2;
var_dump($a); // 10

There are a series of assignment operators that work as shortcuts. You can build them combining an arithmetic operator and the assignment operator. Let's see some examples:

$a = 13;
$a += 14; // same as $a = $a + 14;
var_dump($a);
$a -= 2; // same as $a = $a - 2;
var_dump($a);
$a *= 4; // same as $a = $a * 4;
var_dump($a);

Comparison operators

Comparison operators are one of the most used groups of operators. They take two operands and compare them, returning the result of the comparison usually as a Boolean, that is, true or false.

There are four comparisons that are very intuitive: < (less than), <= (less or equal to), > (greater than), and >= (greater than or equal to). There is also the special operator <=> (spaceship) that compares both the operands and returns an integer instead of a Boolean. When comparing a with b, the result will be less than 0 if a is less than b, 0 if a equals b, and greater than 0 if a is greater than b. Let's see some examples:

<?php
var_dump(2 < 3); // true
var_dump(3 < 3); // false
var_dump(3 <= 3); // true
var_dump(4 <= 3); // false
var_dump(2 > 3); // false
var_dump(3 >= 3); // true
var_dump(3 > 3); // false
var_dump(1 <=> 2); // int less than 0
var_dump(1 <=> 1); // 0
var_dump(3 <=> 2); // int greater than 0

There are comparison operators to evaluate if two expressions are equal or not, but you need to be careful with type juggling. The == (equals) operator evaluates two expressions after type juggling, that is, it will try to transform both expressions to the same type, and then compare them. Instead, the === (identical) operator evaluates two expressions without type juggling, so even if they look the same, if they are not of the same type, the comparison will return false. The same applies to != or <> (not equal to) and !== (not identical):

<?php
$a = 3;
$b = '3';
$c = 5;
var_dump($a == $b); // true
var_dump($a === $b); // false
var_dump($a != $b); // false
var_dump($a !== $b); // true
var_dump($a == $c); // false
var_dump($a <> $c); // true

You can see that when asking if a string and an integer that represent the same number are equal, it replies affirmatively; PHP first transforms both to the same type. On the other hand, when asked if they are identical, it replies they are not as they are of different types.

Logical operators

Logical operators apply a logic operation—also known as a binary operation—to its operands, returning a Boolean response. The most used ones are ! (not), && (and), and || (or). && will return true only if both operands evaluate to true. || will return true if any or both of the operands are true. ! will return the negated value of the operand, that is, true if the operand is false or false if the operand is true. Let's see some examples:

<?php
var_dump(true && true); // true
var_dump(true && false); // false
var_dump(true || false); // true
var_dump(false || false); // false
var_dump(!false); // true

Incrementing and decrementing operators

Incrementing/decrementing operators are also shortcuts like += or -=, and they only work on variables. There are four of them, and they need special attention. We've already seen the first two:

  • ++: This operator on the left of the variable will increase the variable by 1, and then return the result. On the right, it will return the content of the variable, and after that increase it by 1.
  • --: This operator works the same as ++ but decreases the value by 1 instead of increasing by 1.

Let's see an example:

<?php
$a = 3;
$b = $a++; // $b is 3, $a is 4
var_dump($a, $b);
$b = ++$a; // $a and $b are 5
var_dump($a, $b);

In the preceding code, on the first assignment to $b, we use $a++. The operator on the right will return first the value of $a, which is 3, assign it to $b, and only then increase $a by 1. In the second assignment, the operator on the left first increases $a by 1, changes the value of $a to 5, and then assigns that value to $b.

Operator precedence

You can add multiple operators to an expression to make it as long as it needs to be, but you need to be careful as some operators have higher precedence than others, and thus, the order of execution might not be the one you expect. The following table shows the order of precedence of the operators that we've studied until now:

Operator

Type

**

Arithmetic

++, --

Increasing/decreasing

!

Logical

*, /, %

Arithmetic

+, -

Arithmetic

<, <=, >, >=

Comparison

==, !=, ===, !==

Comparison

&&

Logical

||

Logical

=, +=, -=, *=, /=, %=, **=

Assignment

The preceding table shows us that the expression 3+2*3 will first evaluate the product 2*3 and then the sum, so the result is 9 rather than 15. If you want to perform operations in a specific order, different from the natural order of precedence, you can force it by enclosing the operation within parentheses. Hence, (3+2)*3 will first perform the sum and then the product, giving the result 15 this time.

Let's see some examples to clarify this quite tricky subject:

<?php
$a = 1;
$b = 3;
$c = true;
$d = false;
$e = $a + $b > 5 || $c; // true
var_dump($e);
$f = $e == true && !$d; // true
var_dump($f);
$g = ($a + $b) * 2 + 3 * 4; // 20
var_dump($g);

This preceding example could be endless, and still not be able to cover all the scenarios you can imagine, so let's keep it simple. In the first highlighted line, we have a combination of arithmetic, comparison, and logical operators, plus the assignment operator. As there are no parentheses, the order is the one detailed in the previous table. The operator with the highest preference is the sum, so we perform it first: $a + $b equals 4. The next one is the comparison operator, so 4 > 5, which is false. Finally, the logical operator, false || $c ($c is true) results in true.

The second example might need a bit more explanation. The first operator we see in the table is the negation, so we resolve it. !$d is !false, so it is true. The expression is now, $e == true && true. First we need to solve the comparison $e == true. Knowing that $e is true, the comparison results in true. The final operation then is the logical end, and it results in true.

Try to work out the last example by yourself to get some practice. Do not be afraid if you think we are not covering operators enough. During the next few sections, we will see a lot of examples.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.47.208