Chose Language:
comments
Author: Admin/Publisher |finished | checked

Operators in PHP

Operators are symbols or keywords used to perform operations in PHP. These are used to manipulate values and variables.

We have other operator that is called the ternary operator that will be appart of this post, you can find it here: https://blastcoding.com/en/php-ternary-operator/

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations in PHP.

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
Operator Name Example Result
+Addition$x + $ySum of $x and $y
Subtraction$x – $yDifference of $x and $y
*Multiplication$x * $yMultiplication of $x and $y
/Division$x / $yDivision of $x by $y
%Modulus$x % $yRemainder of $x divided by $y
**Power$x ** $yResult 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 (%=)
AssignmentAnother way to write itDescription
x = yx = yThe value on the right is loaded into the variable on the left
x += yx = x + yAddition
x -= yx = x – ySubtraction
x *= yx = x * yMultiplication
x /= yx = x / yDivision
x %= yx = x % yModulo

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 == $yReturns true if $x is equal to $y
===identity$x === $yReturns true if $x is equal to $y, and they are of the same type
!=inequality$x != $yReturns true if $x is not equal to $y
<>inequality$x <> $yReturns true if $x is not equal to $y
!==non-identity$x !== $yReturns true if $x is not equal to $y, and they are not of the same type
>greater than$x > $yReturns true if $x is greater than $y
<less than$x < $yReturns true if $x is less than $y
>=greater than or equal to$x >= $yReturns true if $x is greater than or equal to $y
<=less than or equal to$x <= $yReturns 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
andAnd$x and $ytrue if both $x and $y are true
orOr$x or $ytrue if either $x or $y is true
xorXor$x xor $ytrue if either $x or $y is true, but not both
&&And$x && $ytrue if both $x and $y are true
||Or$x || $ytrue if either $x or $y is true
!Not!$xtrue 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
++$xPre-incrementincrements $x by 1, then returns $x
$x++Post-incrementreturns $x, then increments $x by 1
–$xPre-decrementdecrements $x by 1, then returns $x
$x–Post-decrementreturns $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 . $txt2Concatenation of $txt1 and $txt2
.=Concatenation assignment$txt1 .= $txt2Appends $txt2 to $txt1
string concatenation
<?php
$s1 = "hello";
$s2 = "world";
$message = $s1 ." ". $s2;
echo $message;

Array Operators

operator name example result
+union$x + $ycreates a union of arrays $x and $y
==equality$x == $yreturns true if $x and $y contain the same key/value pairs
===identity$x === $yreturns true if $x and $y contain the same key/value pairs in the same order and of the same type
!=not equal$x != $yreturns true if $x is not equal to $y
<>not equal$x <> $yreturns true if $x is not equal to $y
!==not identical$x !== $yreturns 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.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments