PHP Syntax Operators Assignement

 

- Info:

- Assignement operators assign value to variable.

- Shortcut assignment operators, like += and /+, perform some mathematical operation before making the assignment.

- This tutorial is based on http://hr.php.net/manual/en/language.operators.assignment.php.

 

    

Type

Name

Description

 

$a = 5

Assignment

$a equals 5

 

$a += 5

Addition-Assignment

$a equals $a plus 5

 

$a -= 5

Subtraction-Assignment

$a equals $a minus 5

 

$a *= 5

Multiplication-Assignment

$a equals $a multiplied by 5

 

$a /= 5

Division-Assignment

$a equals $a divided by 5

 

$a %= 5

Modulus-Assignment

$a equals $a modulo 5

 

$a .= 5

Concatenation-Assignment

$a equals $a concatenated with 5

 

- Example:

- Creat PHP Console Application Test.php.

 

Test.php

<?php

 

#Test variables.---------------------------------------------------------------------------

$x = 15;

$str = "Hello";

#Assignement operators.--------------------------------------------------------------------

$x += 10; # $x = $x + 10;

$x -= 10; # $x = $x - 10;

$x *= 10; # $x = $x * 10;

$x /= 10; # $x = $x / 10;

$x %= 10; # $x = $x % 10;

$str .= " World"; # $str = $str . " World";

#Display result.---------------------------------------------------------------------------

print($x); # Replace $add with any other variable from above to see its value.

 

?>