Repasos Inglés

PHP operators


Through the operators we can make the variables interact with each other or that they are used by functions either specific to the system.

Arithmetic Operators

The most common arithmetic operators soon: addition, subtraction, multiplication and division. There is also a Module (%) which gives us the remainder of the division between two numbers. Exponential operators (**) that raise the first number or operate at the power indicated by the second. There is also the negative operator (-) which inverts or gives a negative value of the operand used.
Here are some examples of these operators. We have included the "echo" function that prints the results on the browser screen and also prints a line break ""

<?php
$a = 5;
$b = 3;

var_dump($a + $b); // Result: 8
echo "<br/>";
var_dump($a - $b); // Result:  2
echo "<br/>";
var_dump($a * $b); // Result:  15
echo "<br/>";
var_dump($a / $b); // Result:  1.66666666667
echo "<br/>";
var_dump($a % $b); // Result:  2
echo "<br/>";
var_dump($a ** $b); // Result:  125
echo "<br/>";
var_dump(-$a); // Result:  -5
echo "<br/>";

Assignment Operators

Assignment operators are those that allow you to store the results of an expression in a variable. These expressions can be simple numbers or the result of arithmetic or other operations that we will see later.

There are some operators that have shortcuts to assign and modify a value at the same time.

Some examples of assignment operators are shown in the following code:

<?php
$a = 5 + 5 +6 +7;

echo $a; // Result de 5+5+6+7 = 23
echo "<br/>";

$b = 0;

var_dump($b += 10); // Result 10
echo "<br/>";
var_dump($b -= 2); // Result 8
echo "<br/>";
var_dump($b *= 4); // Result 32
echo "<br/>";


Comparison Operators

Comparison operators take two operands and return the result of comparing them to each other. This result is almost always a Boolean value (True-'True 'or False-'False').
The most common comparisons are as follows:

1. "<", less than.
2. "<=", less than or equal to.
3. ">", greater than.
4. "> =", greater than or equal to.

There is also a special operator "<=>" (spaceship) that compares two operators and returns the following results:

1. Less than "0" (-1) if the operator "a" is less than "b".
2. "0" if the operator "a" = "b".
2. Greater than "0" (1) if "a" is greater than "b".

Here we can see some examples:

<?php

var_dump(3 < 4); // Result: true
echo "<br/>";
var_dump(6 < 6); // Result: false
echo "<br/>";
var_dump(9 <= 9); // Result: true
echo "<br/>";
var_dump(10 <= 9); // Result: false
echo "<br/>";
var_dump(2 > 4); // Result: false
echo "<br/>";
var_dump(5 >= 5); // Result: true
echo "<br/>";
var_dump(7 > 7); // Result: false
echo "<br/>";
var_dump(4 <=> 5); // Result: -1
echo "<br/>";
var_dump(4 <=> 4); // Result: 0
echo "<br/>";
var_dump(5 <=> 4); // Result: 1
echo "<br/>";
var_dump(8 <=> 4); // Result: 1
echo "<br/>";
var_dump(8 <=> 16); // Result: -1
echo "<br/>";
PHP type juggling in comparisons

When using certain comparison operators, be careful with "data manipulation" or (Type Juggling). For example, the "==" operator (same as) will evaluate two expressions after manipulating the data and trying to convert the values to the same data type.
The operator "===" (Identical to) evaluates 2 expressions without manipulating data, that is to say that although two values appear to be the same, if they are not of the same type, the comparison will be false.
The same consideration must be taken for "! =" Or "<>" and when using "! ==" (not identical to)

<?php

$a = 10;
$b = '10';
$c = 7;

// PHP takes into consideration that
// number 10 and string '10'
// are the same after type juggling

var_dump($a ==  $b); // true
echo "<br/>";

// In this next example PHP considers the data types 
// and evaluates them as 2 different expressions
var_dump($a === $b); // false
echo "<br/>";


var_dump($a !=  $b); // false
echo "<br/>";
var_dump($a !== $b); // true
echo "<br/>";
var_dump($a ==  $c); // false
echo "<br/>";
var_dump($a <>  $c); // true
echo "<br/>";
Logical operators

Logical operators are used to perform logical or binary operations as follows:

one. "!" (not) returns the value "true" if the evaluated expression is "false" and vice versa.
2. "&&" (and) returns the value "true" if when evaluating two operands, both are "true".
3. "||" (or) returns the value "true" if one of the evaluated operands is "true".

We show some examples below:

<?php

var_dump(true && true); // Result: true
echo "<br/>";
var_dump(true && false); //  Result: false
echo "<br/>";
var_dump(true || false); //  Result: true
echo "<br/>";
var_dump(false || false); // Result: false
echo "<br/>";
var_dump(!false); //  Result: true
echo "<br/>";


Operators to increase / decrease values in numeric variables

There are four types of these operators, which I explain below:

1. "++": This operator applies the increment to a variable according to its location. To the left of a variable, apply the increment and return the value of the variable with the specified increment. When located to the right of the variable, it returns the current value and then increments.

2. "-": Like the previous operator, if it is located to the left of the variable, it reduces its value and returns the value of the variable with said reduction. If it is located to the right of the variable, it returns the value of the variable and then performs the reduction.

Some examples below:

<?php

$a = 4;
$b = $a++; // Variable $b  is assigned value 4 and   $a turns into 5
var_dump($a, $b);
echo "<br/>";

$b = ++$a; // In this case both $a and $b = 6
var_dump($a, $b);
echo "<br/>";

Operators Precedence

Precedence defines the operators that act first within an expression.

The order of precedence is as follows:

one. **
2. ++, -
3. instanceof
4. !
5. *, / %
6. +, -,.
7. <<, >>
8. <,> =,>,> =
9. ==,! =, ===,! ==, <>, <=>
10. &
11. ^
12. /
13. &&
14. ||
fifteen. ??
16.? :
17. =, + =, - =, * =, ** =, / =,. =,% =, & =, / =, ^ =, << =, >> =
18. and
19. xor
20. or

It is very important, in case you are not sure of the order of precedence, always use parentheses to ensure that the desired logic will be obtained

<?php

var_dump(6 * 6 % 5); // (6 * 6) % 5 = 1
echo "<br/>";
var_dump((6 * 6 )% 5); // (6 * 6) % 5 = 1
echo "<br/>";
var_dump(6* (6 % 5)); // 6 * (6 % 5) = 6