PHP Assignment Operators

Summary: in this tutorial, you will learn about the most commonly used PHP assignment operators.

Introduction to the PHP assignment operator #

PHP uses the = to represent the assignment operator.

The following shows how to assign a value to a variable using the assignment operator:

$variable_name = expression;Code language: PHP (php)

On the left side of the assignment operator (=) is a variable to which you want to assign a value. And on the right side of the assignment operator (=) is a value or an expression.

When evaluating the assignment operator (=), PHP evaluates the expression on the right side first and assigns the result to the variable on the left side. For example:

<?php

$x = 10;
$y = 20;
$total = $x + $y;

echo $total;Code language: PHP (php)

Try it

In this example, we assign 10 to $x, 20 to $y, and the sum of $x and $y to $total.

The assignment expression returns a value assigned, which is the result of the expression:

$variable_name = expression;Code language: PHP (php)

This allows you to chain assignments in a single statement:

$x = $y = 20;Code language: PHP (php)

In this case, PHP evaluates the right-most expression first:

$y = 20Code language: PHP (php)

The whole expression evaluates to 20:

<?php

echo $y = 20 . '<br>';
echo $y . '<br>';Code language: HTML, XML (xml)

Try it

Output:

20
20

The assignment expression $y = 20 returns 20 so PHP assigns 20 to $x. After the assignments, both $x and $y equal 20.

Arithmetic assignment operators #

Sometimes, you want to increase a variable by a specific value. For example:

<?php

$counter = 1;
$counter = $counter + 1;

echo  $counter;Code language: PHP (php)

Try it

How it works.

  • First, $counter is set to 1.
  • Then, increase the $counter by 1 and assign the result to the $counter.

After the assignments, the value of $counter is 2.

PHP provides the arithmetic assignment operator += that can do the same but with a shorter code. For example:

<?php

$counter = 1;
$counter += 1;

echo  $counter;Code language: PHP (php)

Try it

The expression $counter += 1 is equivalent to the expression $counter = $counter + 1.

Besides the += operator, PHP provides other arithmetic assignment operators.

The following table shows all the arithmetic assignment operators in PHP:

OperatorExampleEquivalentOperation
+=$x += $y$x = $x + $yAddition
-=$x -= $y$x = $x – $ySubtraction
*=$x *= $y$x = $x * $yMultiplication
/=$x /= $y$x = $x / $yDivision
%=$x %= $y$x = $x % $yModulus
**=$x **= $y$x = $x ** $yExponentiation

Concatenation assignment operator #

PHP uses the concatenation operator (.) to concatenate two strings. For example:

<?php 

$greeting = 'Hello ';
$name = 'John';

$greeting = $greeting . $name;

echo $greeting;Code language: PHP (php)

Try it

Output:

Hello JohnCode language: PHP (php)

By using the concatenation assignment operator you can concatenate two strings and assigns the result string to a variable. For example:

<?php 

$greeting = 'Hello ';
$name = 'John';

$greeting .= $name;

echo $greeting;Code language: PHP (php)

Try it

Summary #

  • Use the assignment operator (=) to assign a value to a variable.
  • The assignment expression returns the value assigned.
  • Use arithmetic assignment operators to perform arithmetic operations and assign them at the same time.
  • Use concatenation assignment operator (.=) to concatenate strings and assign the result to a variable in a single statement.
Did you find this tutorial useful?