Operators are one of the most important and basic topics. Almost all expressions somehow contain them. Without PHP operators, it would not be possible to do anything with data in general, just as, for example, in mathematics. Therefore, take this topic seriously, and in the end, be sure to do your homework – only practice, only hardcore.
But before moving on, I suggest exploring the var_dump() function. It allows you to output the type and value of something.
Or a variable:
<?php $result = 2 < 4; var_dump($result);
2 less than 4. The result is true. That’s right.
Congratulations, now at any moment we can find out what is in a variable, or we can immediately find out the result of an expression. Let’s get back to our main lesson topic.
The topic of operators is very closely intertwined with the topic of data types. As we have already said in the last lesson, the type of data that will result from any expression will depend on the operator. And, for example, the result of addition when using the plus operator will always be a number.
To begin with, operators have different priorities. As, for example, in mathematics: the multiplication operator will be more priority than the addition operator:
<?php $result = 2 + 2 * 2; var_dump($result);
Priority can be controlled using parentheses, for example:
<?php $result = (2 + 2) * 2; var_dump($result);
Well, here I think everything is clear:
In general, operator priorities are fairly predictable. The priority of all operators in PHP can be found in the official documentation.
PHP operator types
Now let’s look at the different types of operators.
Here the operators familiar from school are waiting for us:
<?php $a = 6; $b = 3; // Addition var_dump($a + $b); // Subtraction var_dump($a - $b); // Multiplication var_dump($a * $b); // Division var_dump($a / $b); // Remainder of the division var_dump($a % 4); // Exponentiation var_dump($a ** 2);
int 9 int 3 int 18 int 2 int 2 int 36
Many newbies do not understand how the remainder operator works. Everything is simple here. In our case, we calculate the remainder of dividing 6 by 4. It is not completely divisible, the integer part is 1, and 2 is in the remainder. A few more similar examples to understand:
<?php var_dump(5 % 2); var_dump(17 % 12); var_dump(8 % 4);
int 1 int 5 int 0
I think arithmetic operators no longer require further explanation.
We have already worked with the Assignment operator. Used to assign a value to a variable.
<?php $a = 55;
Ok, it’s simple. And what about this:
<?php $result = ($x = 5) * 2;
Here, the variable $x was assigned the value 5, and then multiplied by 2 and put the result of the expression in the variable $result. Yes, this is also possible in PHP, although it is not welcome, as the code looks rather confusing.
In addition, there are so-called abbreviated, combined assignment operators. For example, in combination with the addition operator:
<?php $x = 5; $y = 7; $y += $x; var_dump($y);
$y += $x
Could be written as:
$y = $y + $x
Thus, if something is done with the left operand in its original form, then these abbreviated options can be used. And yes, it applies and looks very elegant, you should get used to them now. Let’s look at a few more examples:
<?php $x = 6; $y = 3; $x /= $y; var_dump($x);
And with string concatenation:
<?php $hello = 'Hello '; $hello .= 'world!'; var_dump($hello);
string 'Hello world!' (length=12)
Ok, let’s move on.
Well, from the name it’s clear what kind of operators these are and what they are for. The result of their work will always be a Boolean value (true or false). Let’s start with the equality or inequality operators:
<?php $x = 2; $y = '2'; var_dump($x == $y); // equality test var_dump($x === $y); // identity verification var_dump($x != $y); // check for inequality var_dump($x !== $y); // checking for identity inequality
boolean true boolean false boolean false boolean true
Let’s explain. The == operator converts the operands to the same type and then compares their values. So the string ‘2’ was converted to a number and the values turned out to be equal.
The identity equality operator === does not perform type conversions and first compares that the types of values are identical, for example, integers, and then compares their values. And if they are the same, then only in this case returns true.
<?php $x = 2; $y = 2; var_dump($x === $y);
The inequality operator != converts the types to one and compares the values. If they are not equal, returns true, otherwise false.
The identity inequality operator !== first compares the types, if they are not identical, for example, a string and a number, it returns true, otherwise, it compares their values. If they are not equal, returns true, otherwise false.
Comparison operators also include:
<?php $x = 2; $y = 4; var_dump($x > $y); // $x more than $y var_dump($x < $y); // $x less than $y var_dump($x >= $y); // $x more or equal to $y var_dump($x <= $y); // $x less than or equal to $y
boolean false boolean true boolean false boolean true
Everything is obvious here, we will not linger.
Now consider the comparison operator that appeared in PHP 7. This is a spaceship <=>. Looks like a Star Wars stormtrooper ship, doesn’t it?
The logic of this operator is as follows:
a <=> $b If $a > $b return 1 If $a == $b return 0 If $a < $b return -1
Let’s look at an example:
<?php var_dump(2 <=> 4); var_dump(2 <=> 2); var_dump(5 <=> 3);
int -1 int 0 int 1
This operator was introduced for use in sorting functions. We will not raise this topic, for now, just know that such an interesting operator appeared in the latest version of PHP.
Increment and decrement
Often, different algorithms have to increase and decrease integers by one. There are special increment and decrement operators for this. Options can be very different:
$i++ – postfix increment, returns the current value of $i, and then increments the value by one;
$i– – postfix decrement, returns the current value of $i, and then decreases the value by one;
++$i – prefix increment, first increases the value of the variable $i by one and then returns the result;
–$i – prefix decrement, first decreases the value of the variable $i by one and then returns the result.
Let’s look at a few simple examples:
<?php $i = 5; $x = $i++; // First, the variable $x was assigned the current value of $i (5), then $i was increased by 1 (6) $y = ++$i; // First, we increase the variable $i by 1 (7), then we assigned a new value to the variable $y (7) $z = $i--; // First, $z was assigned the value $i (7), then it was reduced $i by 1 (6) var_dump($x); var_dump($y); var_dump($z); var_dump($i);
int 5 int 7 int 7 int 6
That’s all with increment and decrement. Move on.
Now let’s talk about one of the most important types of operators – logical. These operators are used to calculate conditions. The result of such an operator will always be true or false. In this case, the operands are also true or false. Let’s list them:
&& – logical AND, will return true only if both operands are true;
|| – logical OR, returns true if at least one of the operands is true;
! – NEGATIVE, returns true if the operand is false, and vice versa. It is used if you need to invert a condition, often this is very helpful;
xor – exclusive OR, returns true only if one of the operands is true and the other false.
Let’s look at an example:
<?php var_dump(true && true); // true var_dump(true && false); // false var_dump(true || true); // true var_dump(true || false); // true var_dump(false || true); // true var_dump(!true); // false var_dump(!false); // true var_dump(false xor true); // true var_dump(true xor true); // false var_dump(true xor false); // true var_dump(false xor false); // false
And now let’s get more interesting – let’s determine whether the number is even and at the same time it is more than 10. In order to check for parity – just take the remainder of dividing by 2 and compare it with zero. If it is 0, then the number is even. Let’s write the first calculation:
<?php $x = 14; $isEven = $x % 2 == 0; var_dump($isEven); // true
<?php $x = 14; $isEven = $x % 2 == 0; var_dump($isEven); // true $isMoreThan10 = $x > 10; var_dump($isMoreThan10); // true
It remains to make sure that both conditions are satisfied. We use the logical AND:
<?php $x = 14; $isEven = $x % 2 == 0; var_dump($isEven); $isMoreThan10 = $x > 10; var_dump($isMoreThan10); $isEvenAndMoreThan10 = $isEven && $isMoreThan10; var_dump($isEvenAndMoreThan10);
The calculation could be expressed in one line:
<?php $x = 14; $isEvenAndMoreThan10 = $x % 2 == 0 && $x > 10;
However, you must admit that the first example is more understandable. I want you to understand. Your code should not take up as little space as possible. It must be understood. It should be easy to read by programmers. In the first case, everything is obvious: they found out that the number is even, that it is more than 10, and decided that both of these conditions are fulfilled simultaneously. Somehow, the person reading your code should think like this.
And yet, if the operands are not Boolean values, they will be automatically converted to true or false.
String concatenation operator
Yes, for strings there is only one operator for combining them into one – the concatenation operator. It is a point and its use is as follows:
<?php $string1 = 'Hello'; $string2 = 'World!'; echo $string1 . ', ' . $string2;
Read the official documentation on operator priorities. Using the var_dump() function, get the results of the following expressions:
!1 !0 !true 2 && 3 5 && 0 3 || 0 5 / 1 1 / 5 5 + '5string' '5' == 5 '05' == 5 '05' == '5'
You can comment on the article below or ask a question on Uzabila QNA.