In general, the PHP assignment operators can be used to assign new values into PHP variables and that can be done using the equal operator (=) and some other operators else.

In other words, the PHP assignment operators are used to set a new value to a PHP variable. So, the most common operator is the EQUAL operator, and it can be written as the — “=“.

This operator appears in all variables, arithmetic, comparison, and so many others. So, the major of programming languages depend on this operator to assign the values to the variables.

For example, the common uses like the below.

PHP Variable = The Value or the Expression.

For PHP code example.

$value = 5;

Anyway, the following list shows you the assignment operator types.

Let’s explain each one in-depth.

PHP Arithmetic Assignment Operators

The following table shows you the PHP arithmetic assignment operators with a small description to be understandable.

OperatorNameDescripion
+=AdditionTo sum two variables together.
-=SubtractionTo minus two variables
*=MultiplicationTo multiply numbers together
/=DivisionTo divide two digits
%=ModulusThe first variable is divided by the second one.
**=ExponentiationRefers to the calculated exponentiation

Anyway, In the following sections, you will see an example for each one.

An example for addition Operator, $i += $a OR $i = $i + $a.

<?php 
  $i = 1; 
  $i += 1; // 1 + 1
  echo $i; // 2
?>

The Subtraction for example, $i -= $a OR $i = $i - $a.

<?php 
  $i = 10; 
  $i -= 5; // 10 - 5
  echo $i; // 5 
?>

The Multiplication $i *= $a OR $i = $i * $a.

<?php 
  $i = 20; 
  $i *= 2; // 20 * 2
  echo $i; // 40
?>

The Division $i /= $a OR $i = $i / $a.

<?php 
  $i = 20; 
  $i /= 2; // 20 / 2
  echo $i; // 10
?>

The Modulus $i %= $a OR $i = $i % $a.

<?php 
  $i = 10; 
  $i %= 3; // 10 % 2
  echo $i; // 1
?>

The Exponentiation $i **= $a OR $i = $i ** $a.

<?php 
  $i = 10; 
  $i **= 5; // 10 * 10 * 10 * 10 * 10
  echo $i; // 100000
?>

In addition details, you have to read the PHP Arithmetic Operators tutorial.

PHP Bitwise Assignment Operators

In general the bitwise operators are using binary representation for the both operands. So, to see more details about the binary calculation you have to read the binary numeral system from here.

Anyway, the following table shows you the most bitwise operators in PHP.

OperatorNameExample
&=Bitwise And$a = $x & $y;
|=Bitwise Or$a = $x | $y;
^=Bitwise Xor$a = $x ^ $y;
<<=Left Shift$a = $x << $y;
>>=Right Shift$a = $x >> $y;

For examples

Use the Bitwise And for example: $a = $x & $y;

<?php
   $x = 10; // bits =>  00001010 ( 10 = 2 + 8 )
   $y = 15; // bits =>  00001111 ( 15 = 8 + 4 + 2 + 1 ) 
   $a = $x & $y; // bits =>  00001010 ( 10 = 8 + 4 + 2 + 1 ) 
   echo $a; // 10
?>

Another type in the bitwise, Bitwise Or for example: $a = $x | $y;

<?php
   $x = 10; // bits => 00001010 ( 10 = 2 + 8 )
   $y = 15; // bits => 00010100 ( 20 = 4 + 16 )
   $a = $x | $y; // bits => 00011110 ( 2 + 8 + 16 + 4 )
   echo $a; // 
?>

Usage of the Bitwise XOr for example: $a = $x ^ $y;

<?php
   $x = 10; // bits => 00001010 ( 10 = 2 + 8 )
   $y = 50; // bits => 00110010 ( 50 = 32  + 16 + 2 )
   $a = $x ^ $y; // bits => 00111000 ( 56 = 32  + 16 + 8  )
   echo $a; // 56
?>

Left Shift $a = $x << $y;

<?php
   $x = 2; // bits =>  
   $y = 5; // bits => 
   $a = $x << $y; // bits => 0   1   0   0   0   0   -   0 
   echo $a; // 64
?>

Right Shift $a = $x >> $y;

<?php
   $x = 5; // bits =>  00000101
   $y = 2; // bits =>  00000001 
   $a = $x >> $y; 
   echo $a; // 1
?>

To understand how the previous examples calculated, you have to read the PHP bitwise operator tutorial.

Null Coalescing Operator

Generally, The PHP null coalescing operator (??) is a new feature of PHP7. In addition, The purpose of this feature to allow you set a primary default value if main operand display undefined or null value.

For example

<?php
   $email = $_GET["email"] ?? "Email does not exists";
   echo $email; // Email does not exists
?>

To learn more about the null coalescing operator read this article.

Assign a Reference to a PHP Variable

The assign by the reference in PHP is used to set a value for the main point of the PHP variable. For example.

<?php
   $val = "Peter, welcome to our tutorials";
   $wlecome = &$val;
   
   echo $wlecome; // Peter, welcome to our tutorials
   echo "\n";
   
   $wlecome = "Changed Sentence";

   echo $wlecome;  
   echo "\n"; // Changed Sentence

   echo $val; // Changed Sentence
?>

Other Assignment Operator: The Concatenate Operator

To clarify, This operator used to combine two strings together in PHP. Check the following example.

<?php
   $val = "Peter, ";
   $val .= "Welcome to our tutorials";
   echo $val; // => Peter, Welcome to our tutorials
?>

for another example.

<?php
   $val = "Peter, " . "Welcome to our tutorials";
   echo $val; // => Peter, Welcome to our tutorials
?>

Wrapping Up

To summarize that, you understood what are the assignment operators with examples. Also I exposed the common types of this operators such as bitwise operators, arithmetic operators, assign by reference, and null coalescing operator