• Tutorial List

PHP Assignment Operator & Combined Assignment Operators

Last updated May 15, 2020
PHP Assignment Operator & Combined Assignment Operators

PHP Assignment Operator

PHP Assignment Operator is used to assign value to a variable. The assignment operator is the equal sign =

<?php
  $x = "Hello PHP";
  echo "$x";
?>
Browser Output
Hello PHP

PHP Combined Assignment Operators

Combined Assignment Operators are also used to assign values to variables.

But combined assignment operators first perform operations then assign values to variables.

🤔 In the following example, first, we declare the $x variable with value 6, then in the second line, first, perform the addition operation $x + 2 and then we reassign the addition result into the $x variable.

<?php
  $x = 6;
  $x += 2;
  /*
  Same as
  $x = $x + 2;
  */
  echo $x;
?>
Browser Output
8

List of PHP Combined Assignment Operators

<?php
	$x = 6;
?>
OperatorExampleSame asResult
+=
$x += 5
$x = $x + 5
11
-=
$x -= 2
$x = $x - 2
4
*=
$x *= 3
$x = $x * 3
18
/=
$x /= 2
$x = $x / 2
3
%=
$x %= 3
$x = $x % 3
0
**=
$x **= 2
$x = $x ** 2
36
PHP Combined Assignment Operators

Example –

<?php
$x = 6;
$x += 5;
echo "Addition: $x <br>";

$x = 6;
$x -= 2;
echo "Subtraction: $x <br>";

$x = 6;
$x *= 3;
echo "Multiplication: $x <br>";

$x = 6;
$x /= 2;
echo "Division: $x <br>";

$x = 6;
$x %= 3;
echo "Modulus: $x <br>";

$x = 6;
$x **= 2;
echo "Exponentiation: $x";
?>
Browser Output
Addition: 11
Subtraction: 4
Multiplication: 18
Division: 3
Modulus: 0
Exponentiation: 36