Tutorials

Learn web design and programming with our free video and text tutorials.

PHP Asignment Operators

The assignment operator (= or equals), in PHP is used to assign a value to a variable or to assign a variable to another variable's value. The assignment operator can also be used in combination with other mathematical operators to change the value of a variable.

= (assigment) operator

The = (assignment) operator sets the value of the first operand to the value of the second operand.

<?php
$x = 1;
echo $x;
?>

The code above outputs:

1

+= (addition-assignment) operator

The += (addition-assigment) operator sets the value of the first operand to its value plus the second value.

<?php
$x = 1;
$x += 2;
echo $x;
?>

The code outputs:

3

-= (subtraction-assignment) operator

The -= (subtraction-assignment) operator sets the value of the first operand to its value minus the secode value.

<?php
$x = 3;
$x -= 2;
echo $x;
?>

The code above outputs:

1

*= (multiplication-assignment) operator

The *= (multiplication-assignment) operator sets the value of the first operand to its value multiplied by the second value.

<?php
$x = 2;
$x *= 2;
echo $x;
?>

The code above outputs:

4

/= (division-assignment) operator

The /= (division-assignment) operator sets the value of the first operancd to its value divided by the second value.

<?php
$x = 4;
$x /= 2;
echo $x;
?>

The code above outputs:

2

.= (concatenation-assignment) operator

The .= (concatenation-assignment) operator sets the value of the first operand to a string containing its value with the second value appended at the end.

<?php
$x = "WebDevelopmentTutorials.com ";
$x .= "is the best.";
echo $x;
?>

The code above outputs:

WebDevelopmentTutorials.com is the best.

%= (modulo-assignment) operator

The %= (modulo-assignment) operator sets the value of the first operand to the remainder of its value divided by the second value.

<?php
$x = 3;
$x %= 2;
echo $x;
?>

The code above outputs:

1