Operators in PHP
Operators are symbols or keywords used to perform operations in PHP. These are used to manipulate values and variables.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations in PHP.
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y |
– | Subtraction | $x – $y | Difference of $x and $y |
* | Multiplication | $x * $y | Multiplication of $x and $y |
/ | Division | $x / $y | Division of $x by $y |
% | Modulus | $x % $y | Remainder of $x divided by $y |
** | Power | $x ** $y | Result of raising $x to the power of $y (Introduced in PHP 5.6) |
Assignment Operators
In PHP, we have different ways to assign a value, the following is a list of operators that could be used:
- Simple assignment (=)
- Addition assignment (+=)
- Subtraction assignment (-=)
- Multiplication assignment (*=)
- Division assignment (/=)
- Modulus assignment (%=)
Assignment | Another way to write it | Description |
---|---|---|
x = y | x = y | The value on the right is loaded into the variable on the left |
x += y | x = x + y | Addition |
x -= y | x = x – y | Subtraction |
x *= y | x = x * y | Multiplication |
x /= y | x = x / y | Division |
x %= y | x = x % y | Modulo |
Comparison Operators
What operator we have for comparison? Let’s check it out:
- Equality (==)
- Inequality (!=)
- Identity (===)
- Non-identity (!==)
- Less than (<) Greater than (>)
- Less than or equal to (<=) Greater than or equal to (>=)
Comparison | name | example | Result |
---|---|---|---|
== | equality | $x == $y | Returns true if $x is equal to $y |
=== | identity | $x === $y | Returns true if $x is equal to $y, and they are of the same type |
!= | inequality | $x != $y | Returns true if $x is not equal to $y |
<> | inequality | $x <> $y | Returns true if $x is not equal to $y |
!== | non-identity | $x !== $y | Returns true if $x is not equal to $y, and they are not of the same type |
> | greater than | $x > $y | Returns true if $x is greater than $y |
< | less than | $x < $y | Returns true if $x is less than $y |
>= | greater than or equal to | $x >= $y | Returns true if $x is greater than or equal to $y |
<= | less than or equal to | $x <= $y | Returns true if $x is less than or equal to $y |
Logical Operators
Logical operators are used to combine two or more logical expressions.
- Logical AND (&& or and)
- Logical OR (|| or or)
- Logical NOT (!)
operator | name | example | result |
---|---|---|---|
and | And | $x and $y | true if both $x and $y are true |
or | Or | $x or $y | true if either $x or $y is true |
xor | Xor | $x xor $y | true if either $x or $y is true, but not both |
&& | And | $x && $y | true if both $x and $y are true |
|| | Or | $x || $y | true if either $x or $y is true |
! | Not | !$x | true if $x is not true |
Increment/Decrement Operators
Increment/decrement operators are used to increment or decrement the value of a variable.
- Increment (++$variable or $variable++)
- Decrement (–$variable or $variable–)
operator | name | description |
---|---|---|
++$x | Pre-increment | increments $x by 1, then returns $x |
$x++ | Post-increment | returns $x, then increments $x by 1 |
–$x | Pre-decrement | decrements $x by 1, then returns $x |
$x– | Post-decrement | returns $x, then decrements $x by 1 |
Operator of execution in PHP
To run a command through PHP in the console, we can use this operator, which is really simple. Let’s say we want to run dir (Windows) or ls (Linux) in the console, to do this we simply put the command in backticks. Let’s see an example:
execution operator<?php $output = `dir`; echo "<pre>$output</pre>"; ?>
El volumen de la unidad C no tiene etiqueta. El n�mero de serie del volumen es: D438-52A5 Directorio de C:\xampp\htdocs\tutorialphp\operadores 31/07/2022 20:11 . 31/07/2022 20:11 .. 30/07/2022 21:49 381 arrays.php 31/07/2022 20:12 57 ejecucion.php 2 archivos 438 bytes 2 dirs 193.331.752.960 bytes libres
String Operators
PHP has 2 operators which were specifically designed for strings.
Operator | Name | Example | Result |
---|---|---|---|
. | Concatenation | $txt1 . $txt2 | Concatenation of $txt1 and $txt2 |
.= | Concatenation assignment | $txt1 .= $txt2 | Appends $txt2 to $txt1 |
<?php $s1 = "hello"; $s2 = "world"; $message = $s1 ." ". $s2; echo $message;
Array Operators
operator | name | example | result |
---|---|---|---|
+ | union | $x + $y | creates a union of arrays $x and $y |
== | equality | $x == $y | returns true if $x and $y contain the same key/value pairs |
=== | identity | $x === $y | returns true if $x and $y contain the same key/value pairs in the same order and of the same type |
!= | not equal | $x != $y | returns true if $x is not equal to $y |
<> | not equal | $x <> $y | returns true if $x is not equal to $y |
!== | not identical | $x !== $y | returns true if $x is not identical to $y |
example:
Union con +<?php $nombres = array("Alicia","Ruben","Clara"); $nombres2 = array("Pamela","Rosario"); $all = []; $all = $nombres2 + $nombres; foreach ($all as $element){ echo $element." "; }
Keep in mind that the “+” operator creates the union of arrays, which may not be what you are looking for. If you want to merge the arrays, you can use a different approach.