Operators

2

We've used some of PHP operators so far. Before we continue learning further, let's review all PHP operators available. Consider this lesson as PHP operators reference.

String operators

Recently we've learned a dot operator which is used to join strings:

$first_name = 'Adam';
$last_name = 'Smith';
$full_name = $first_name . ' ' . $last_name;
echo $full_name; # Prtings "Adam Smith"

There is a short version of a dot operator:

$name = 'Adam';
$name .= ' Smith'; # THe same as $name = $name . ' Smith';
echo $name; # Prtings "Adam Smith"

Arithmetic operators

There are obvious arithmetic operators which you can find in almost all programming languages:

$a = $b + $c; # Plus operator, $a equals $b plus $c
$a = $b - $c; # Munis operator, $a equals $b minus $c
$a = $b * $c; # Multiplication operator, $a equals $b times $c
$a = $b / $c; # Division operator, $a equals $b divided by $c

There are also short versions of these operators:

$a += $b; # The same as $a = $a + $b;
$a -= $b; # The same as $a = $a - $b;
$a *= $b; # The same as $a = $a * $b;
$a /= $b; # The same as $a = $a / $b;

Less common, but still arithmetic operators and their short versions:

$a = $b % $c;  # Division reminder operator, $a equals remainder of division $b by $c
$a %= $b;      # The same as $a = $a % $b;

$a = $b ** $c; # Power operator, $a equals $b raised into the power of $c
$a **= $b;     # The same as $a = $a ** $b;

Bitwise operators

When using bitwise operators, a given values should be integers, or else they will be converted into integers.

$a = $b & $c;  # Bitwise AND
$a = $b | $c;  # Bitwise OR
$a = $b ^ $c;  # Bitwise XOR
$a = ~$b;      # Bitwise NOT
$a = $b << $c; # Bit shift left
$a = $b >> $c; # Bit shift right

Short versions are also available:

$a &= $b;
$a |= $b;
$a ^= $b;
$a >>= $b;
$a <<= $b;

Logical operators

These operators are used to check if some condition is true or false, usually to use the result it in control flow structures like if, for or while. Logical operators deal with boolean values. If they applied to non-boolean values then given values converted to booleans first.

$a = $b && $c; # Logical AND
$a = $b || $c; # Logical OR
$a = !$b;      # Logical NOT

if($a and $b)  # Logical AND
{
}

if($a or $b)   # Logical OR
{
}

if($a xor $b)  # Logical XOR
{
}

A special note about last three operators: they have the lowest priority there is. Even lower than assignment operator has. So the result of the following code may surprise you:

$b = true;
$c = false;
$a = $b and $c;
var_dump($a); # Surprise! $a is true

That happens because assignment goes first, and and goes after.

Comparison operators

These operators are used to compare values for equality or inequality.

$a = $b == $c;  # $a is true if $b is equal to $c
$a = $b === $c; # $a is true if $b is equal to $c and they both are of the same underlying type
$a = $b != $c;  # $a is true if $b is not equal to $c
$a = $b !== $c; # $a is true if $b is not equal to $c or they are of different underlying types

You can use operators above to compare arrays as well. Comparison of arrays is a comparison of all their key-value pairs. Arrays are not equal if they have different size.

Operators below work with values only, not arrays:

$a = $b < $c;   # $a is true if $b is less than $c
$a = $b > $c;   # $a is true if $b is greater than $c
$a = $b <= $c;  # $a is true if $b is less than or equal to $c
$a = $b >= $c;  # $a is true if $b is greater than or equal to $c
$a = $b <=> $c; # $a is an integer which is: negative if $b < $c, positive is $b > $c, and 0 if $b is equal to $c

Increment and decrement operators

Both of operators behave in the same way so we will look at the increment operator only. Increment means “plus one” and decrement means “minus one”:

$b = 0;
$a = $b++; # Post-increment $a = 0, $b = 1
# $a = $b--; Post-decrement

$b = 0;
$a = ++$b; # Pre-increment $a = 1, $b = 1
# $a = $b--; Pre-decrement

Post-increment (decrement) returns a previous value of a variable before incrementation while pre-increment (decrement) returns a current value of a variable.

Conditional assignment operators

There are two operators of a special kind: ternary operator and null coalescing operator.

Ternary operator:

$a = $condition ? $value1 : $value2; # If $condition is true then $a = $value1, else $a = $value2

This is the same as:

if($condition)
{
    $a = $value1;
}
else
{
    $a = $value2;
}

Null coalescing operator:

$a = $array[$key] ?? $value; # If the value before ?? exists and not null then $a = this value, else $a = value after ??

This is the same as:

if(isset($array[$key]))
{
    $a = $array[$key];
}
else
{
    $a = $value;
}
Lesson 15
Share this page:

Learning plan

The second part of basics of PHP arrays
Things you should know to configure error reporting properly
How to split PHP script into pieces and include them into each other
14. Operators
We should learn all PHP operators in details before we could continue
15. stdClass
The very basics of stdClass PHP class
16. JSON
How to work with JSON in PHP
17. YAML
How to work with YAML in PHP