php assignment operators



PHP Assignment Operators

The PHP assignment operators are used with numeric values to assign value to a variable.

The main assignment operator is “=”, means the right operand value assign to left side operand.

The PHP assignment operators

=      (a=b)                     Means value b assign to value a.
+=    (a+=b)   a=a+b      Add and assign
-=     (a-=b)    a=a-b       Subtract and assign
*=    (a*=b)    a=a*b      Multiply and assign
/=     (a/=b)    a=a/b       Divide and assign
%=   (a/=b)    a=a%b     Modulus and assign


PHP “=” assignment operator example

<?php

$a=5;

$b=$a;

echo $b;

?>

The output is:
5

In above php example we have variable $a with value 5 and we use “=” operator to assign $a to $b variable.


+=  Add and assign php operator example (a+=b)

<?php

$a=5;
$b=2;
$a+=$b;     // OR  $a=$a+$b

echo $a;

?>;

The output is:
7

In above example statement $a+=$b works same as $a=$a+$b.


-= Subtract and assign assignment operator example (a-=b)

<?php

$a=5;
$b=2;
$a-=$b;   // OR $a=$a-$b

echo $a;

?>;

The output is:
3

In above example statement $a-=$b is same as $a=$a-$b.


*= Multiply and assign assignment operator example (a*=b)

<?php

$a=5;
$b=2;
$a*=$b;   // OR  $a=$a*$b

echo $a;

?>

The output is:
10

In above example statement $a*=$b is same as $a=$a*$b.


/= Divide and assign assignment operator example (a/=b)

<?php
$a=10;
$b=2;
$a/=$b;    // OR  $a=$a/$b

echo $a;

?>

The output is:
5

In above example statement $a/=$b is same as $a=$a/$b.


%= Modulus and assign assignment operator example (a%=b)

<?php

$a=5;
$b=2;
$a%=$b;  // OR $a=$a%$b

echo $a;

?>

The output is:
1

In above example statement $a%=$b is same as $a=$a%$b.

Leave a Reply

Your email address will not be published. Required fields are marked *