Operators

This video was created by Dani Krossing

Operatorsopen in new window are symbols that take one or more values or expression and yields another value. There are many categories of operators, but for now we will cover the following:

  • Arithetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Incrementing/Decrementing Operators
  • String Operators

Arithmetic Operators

Arithmetic operatorsopen in new window are used to perform basic arithmetic, including addition, subtraction, multiplication, and division.

OperatorNameExampleResult
+AdditionSum of $a and $b
-SubtractionDifference of $a and $b
*MultiplicationProduct of $a and $b
/DivisionDivides $a by $b
%ModuloRemainder of $a divided by $b
<?php
  $a = 4;
  $b = 3;

  echo $a + $b; // 7
  echo $a - $b; // 1
  echo $a * $b; // 12
  echo $a / $b; // 1.3333333333
  echo $a % $b; // 1
?>

Assignment Operators

Assignment operatorsopen in new window are used to assign a value to a variable. The most common assignment operator is the =. There are also combined operators which are used to perform arithmetic, array unions, and string concatenations while also assigning the value to a variable.

OperatorNameExampleResultEquivalent
=Basic assignment$a will be set to the value of $b
+=Addition assignment$a will be set to the difference of $a and $b
-=Subtration assignment$a will be set to the sum of $a and $b
*=Multiplication assignment$a will be set to the product of $a and $b
/=Division assignment$a will be set to the division of $a by $b
%=Modulo assignment$a will be set to the remainder of $a divided by $b
.=String concatentation assignment$a will be set to the concatenation of $a and $b

Comparison Operators

Comparison Operatorsopen in new window are used to compare two values. When comparing values of different data types, for example, a string and a number, PHP will attempt to convert the string to a number first and then do the comparison.

OperatorNameExampleResult
==EqualTRUE if $a is equal to $b in value
===IdenticalTRUE if $a is equal to $b in value and data type
!=Not EqualTRUE if $a is not equal to $b in value
!==Not IdenticalTRUE if $a is not equal to $b in value and data type
<Less thanTRUE if $a is less than $b
>Greater thanTRUE if $a is greater than $b
<=Less than or equal toTRUE if $a is less than or equal to $b
>=Greater than or equal toTRUE if $a is greater than equal to $b
<=>SpaceshipReturns 0 if $a is equal to $b

Returns 1 if $a is greater than $b

Returns -1 if $a is less than $b
<?php
  $a = 4;
  $b = 3;
  $c = 1;
  $d = 20;

  var_dump($c == 1);    // bool(true)
  var_dump($c == "1");  // bool(true)
  var_dump($c === 1);   // bool(true)
  var_dump($c === "1"); // bool(false)
  var_dump($c != 1);    // bool(false)
  var_dump($c != "1");  // bool(false)
  var_dump($c !== 1);   // bool(false)
  var_dump($c !== "1"); // bool(true)

  var_dump($a > $b);    // bool(true)
  var_dump($c < $b);    // bool(true)
  var_dump($d < $d);    // bool(false)
  var_dump($d <= $d);   // bool(true)
  var_dump($d >= $b);   // bool(true)

  var_dump($a <=> $b);  // int(1)
  var_dump($a <=> $d);  // int(-1)
  var_dump($a <=> $a);  // int(0)
?>

Logical Operators

Logical Operatorsopen in new window are used to combine and make more complex expressions.

OperatorNameExampleResult
!NotTRUE if $a is not TRUE
&&AndTRUE if both $a and $b are TRUE.
``Or

Caution

It is also possible to use and and or in place of && and ||. But these operators have different precedence and can result in unexpected results. Review the Logical Operators documentation for more information.

<?php
  $a = 4;
  $b = 3;
  $c = 1;
  $d = 20;

  // this condition will be FALSE
  if (($a > $b) && ($c > $d)) {
    echo "a is larger than b AND ";
    echo "c is larger than d"; 
  }

  // this condition will be TRUE
  if (($a > $b) || ($c > $d)) {
    echo "a is larger than b OR ";
    echo "c is larger than d"; 
  }

  // setting e if it is not set
  if (!isset($e)) {
    $e = 200;
  }
  echo $e;
?>

Incrementing/Decrementing Operators

Incrementing/Decrementing operatorsopen in new window supports pre- and post-increment and decrement operators.

OperatorNameExampleResult
++Pre-incrementIncrements $a by one, then returns $a
++Post-incrementReturns $a, then increments $a by one
--Pre-incrementDecrements $a by one, then returns $a
--Post-incrementReturns $a, then decrements $a by one

String Operators

PHP only supports two String Operatorsopen in new window, the concatenating assignment operator (.=) mentioned above, and the concatenation operator, which is used to combine strings.

<?php 
$a = "Hello ";
$b = "World"; 

echo $a . $b; // Hello World
?>