Assignment Operators


Assignment Operators

The = symbol represents the assignment operator in PHP. The syntax of the assignment operator is as follows:

$variable name = expression; PHP is the programming language used (php)

$variable name = expression; PHP is the programming language used (PHP) A variable to which you wish to assign a value is on the left side of the assignment operator (=). A value or an expression is placed on the right side of the assignment operator (=)

When using the assignment operator (=), PHP evaluates the right-hand expression first and then assigns the result to the left-hand variable. As an example:

<? $x = 10; $y = 20; $total = $x + $y ?>

output:

30

Code language: PHP (php)

We allocated 10 to $x, 20 to $y, and the sum of $x and $y to $total in this example.

In this example, the assignment expression yields a value assigned as the result of the expression:

$variable_name = expression;

Code language: PHP (php)

It implies that in a single sentence like this, you can utilise numerous assignment operators:

$x = $y = 20;

Code language: PHP (php)

PHP evaluates the right-most expression first in this case:

$y = 20

Code language: PHP (php)

$y is a variable with a value of 20.

PHP allocates 20 to $x because the assignment phrase $y = 20 yields 20. Both $x and $y equal 20 after the assignments.

assignment operators Examples:

<?php $a = 40; $b = 18; $c = $a + $b; echo "Addtion : $c <br/>"; $c += $a; echo "Add : $c <br/>"; $c -= $a; echo "Subtract : $c <br/>"; $c *= $a; echo "Multiply : $c <br/>"; $c /= $a; echo "Division : $c <br/>"; $c %= $a; echo "Modulus : $c <br/>"; ?>

output:

Addtion : 58 Add : 98 Subtract : 58 Multiply : 2320 Division : 58 Modulus : 18