Assignment Operators in PHP with Examples of =, +=, and ??=

PHP’s = operator stores the value on the right in the variable on the left. Compound assignment operators such as += and .= perform an operation and store the result back in the variable. Use ??= to fill in a value only when it is missing or null.

Assign and reassign a value

In PHP, a variable is written with a dollar sign before its name. The PHP manual explains how assignment works. $a = 10 stores 10 and a later $a = 20 changes it to 20. If the right-hand expression reads that same variable, the calculation uses its existing value before storing the result.

Save each code block in its own PHP file and run it from a terminal using its filename, for example php assignment.php. The ??= example requires PHP 7.4 or later.

<?php
$a = 10;
echo $a, PHP_EOL;

$a = 20;
echo $a, PHP_EOL;

$a = 10;
$a = $a + 10;
echo $a, PHP_EOL;

The first example prints 10, 20, and 20 on separate lines. The last 20 comes from resetting the variable to 10 and adding 10 to that value.

Store the result of arithmetic or concatenation

For a simple variable like $a, $a += 10 is shorthand for $a = $a + 10. Use .= to append text and store the longer string in the same variable.

<?php
$a = 10;
$a += 10;
$a -= 3;
echo $a, PHP_EOL;

$message = 'Hello';
$message .= ', PHP!';
echo $message, PHP_EOL;

The arithmetic prints 17 after adding 10 and subtracting 3. The string prints Hello, PHP!. Include any separator, such as a space or comma, in the text you append.

Compound assignment Equivalent for a simple variable Operation
$a += $b $a = $a + $b Addition
$a -= $b $a = $a - $b Subtraction
$a *= $b $a = $a * $b Multiplication
$a /= $b $a = $a / $b Division
$a %= $b $a = $a % $b Remainder
$a **= $b $a = $a ** $b Exponentiation
$a .= $b $a = $a . $b String concatenation

Initialize the variable to a number before trying the arithmetic examples. The right-hand operand of division and remainder operations must be nonzero.

Set a default for a missing or null value

Introduced in PHP 7.4, ??= sets an array key to its default when the key is missing or its value is null. An existing 0, false, or empty string is retained. If your application should replace one of those values, test for it explicitly.

<?php
$settings = ['timeout' => 0, 'theme' => null];
$settings['timeout'] ??= 30;
$settings['theme'] ??= 'light';
$settings['language'] ??= 'ko';

var_dump($settings['timeout']);
var_dump($settings['theme']);
var_dump($settings['language']);

The example keeps timeout at the integer 0, sets theme to light, and adds language with the value ko. The var_dump() output shows both the value and its type.

Distinguish assignment from comparison

The === operator in PHP’s comparison operator reference checks both value and type without changing the variable. Assigning with = changes the variable and the assignment expression evaluates to the value it stored.

<?php
$score = 10;

var_dump($score === 20);
var_dump($score);
var_dump($score = 20);
var_dump($score === 20);

The first two lines are bool(false) and int(10). After the assignment, the output continues with int(20) and bool(true).

In a condition, if ($score = 20) stores 20 and the condition evaluates to true. Use if ($score === 20) to check whether the existing value is the integer 20.