← Back to PHP Course | Chapter 3: Operators | Lesson 5 of 8

PHP Assignment Operators

Assignment operators put a value into a variable, like dropping a coin into a piggy bank. Shortcuts like adding to it let you say 'add 5 more' without repeating yourself.
Syntax
php
$variable = value;
$variable += value;   // $variable = $variable + value
$variable -= value;
$variable *= value;
$variable /= value;

Basic Assignment (=)

= stores the value on the right into the variable on the left — the most basic operation in PHP, and the one every other assignment operator below is a shorthand for.

Example: Basic Assignment (=)

php
<?php
// Declare `$total`, set to `5`
$total = 5;
// Print `$total` to the output
echo $total;
?>

Add and Subtract Assignment

+= and -= combine an operation with an assignment in one step: $total += 5 is exactly equivalent to $total = $total + 5, just shorter to write and, for many people, easier to read once the pattern becomes familiar.

Example: Add and Subtract Assignment

php
<?php
// Declare `$total`, set to `10`
$total = 10;
$total += 5;
// Print `$total . "\n"` to the output
echo $total . "\n";
$total -= 3;
// Print `$total` to the output
echo $total;
?>

Multiply and Divide Assignment

*= and /= follow the identical pattern for multiplication and division — $price *= 1.1 applies a 10% increase to $price and stores the result back into the same variable in a single statement.

Example: Multiply and Divide Assignment

php
<?php
// Declare `$price`, set to `100`
$price = 100;
$price *= 1.1;
// Print `$price` to the output
echo $price;
?>

Modulo and Concat Assignment

%= reassigns a variable to the remainder of dividing it by another value, and .= appends a string onto the end of an existing string variable — the string-concatenation equivalent of += for numbers, and one of the most common operators in code that builds up HTML output piece by piece.

Example: Modulo and Concat Assignment

php
<?php
// Declare `$n`, set to `10`
$n = 10;
$n %= 3;
// Print `$n . "\n"` to the output
echo $n . "\n";

// Declare `$html`, set to "<p>"
$html = "<p>";
$html .= "Hello";
$html .= "</p>";
// Print `$html` to the output
echo $html;
?>

Null Coalescing Assignment (??=)

??= only assigns the right-hand value if the left-hand variable is currently null or doesn't exist yet — it leaves an already-set variable untouched.

That makes it the concise way to fill in a default value without first writing an explicit isset() check.

Example: Null Coalescing Assignment (??=)

php
<?php
// Declare `$username`, set to `null`
$username = null;
$username ??= "Guest";
// Print `$username` to the output
echo $username;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing =+ instead of +=, which assigns a positive number rather than adding to the variable.
  2. Using .= on an undefined variable, which raises an undefined variable warning; initialize it with $s = ''; first.
  3. Using += to join strings, when it does numeric addition; use .= for concatenation.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.