Home >>PHP Tutorial >PHP Assignment Operators

PHP Assignment Operators

Assignment Operators in PHP

There are a few other Operators that tend to do some arithmetic operations
and store the result in same. for eg the addition-assignment operator, represented
by the symbol +=, lets you simultaneously add and assign a new value to a variable.
Common Assignment Operators
Operatos Description
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign quotient
%= Divide and assign modulus
.= Concatenate and assign(its used only for sting)
and here's example illustrating these operators in action.

Add and assign

 <?php	
	
$x = 500;
	
$x+= 500;
	
echo "sum=".$x."<br/>";

?>
		
Output sum=1000
In the above example
Initialize variable ($x) with value = 500. If we want to add 500 to this value . we don't need a second and third variable to store the sum of value($x+=500) it means ($x=$x+500 ) .
add 500 and re-assign new value(1000) back to same variable ($x).

Subtract and assign

<?php		
	
$x = 1000;
	
$x-= 500;
	
echo "subtraction = ".$x."<br/>";
  
?>
		
Output subtraction = 500
In the above example
Create and initialize variable($x) hold value = 1000. now perform subtraction. 500 is subtract from 1000 using($x-=500) it means($x=$x - 500 ) .
Now assign new value back to the initialize variable($x). so the output will become:

Multiply and assign

<?php	
	
$x = 100;
	
$x*= 10;
	
echo "Multiplication = ".$x."<br/>";

?>
		
Output Multiplication= 1000
In the above example
Variable( $x) with value=100. now perform multiplication. ($x*=10) now value 10 multiply with previous value(100).
and the output will become:1000 and it re-assign back to the variable ($x)
so the output will become : 1000

Divide and assign quotient

<?php	

$x = 1000;

$x/= 500;

echo "Quotient = ".$x."<br/>";
?>

Output Quotient = 2
In the above example.
Declare Variable( $x) with value=1000. now perform divide.($x/=500) now value 500 divide with previous value(1000).
and the output will become:2 and it re-assign value=2, back to the variable ($x).

Divide and assign modulus

<?php	
	
$x = 5;
	
$x%= 2;
	
echo "Remainder = ".$x."<br/>";

?>
		
Output Remainder= 1
In the above example. Variable($x) with value =5. Now calculate the modulus using ($x%=2) .
it gives remainder value="1" and this remainder assign again to variable($x).
and the output will become : 1

Concatenate and assign

<?php	
	
$str = "Welcome ";
	
$str. = "to the world of phptpoint";
	
echo $str."<br/>";

?>
		
Output Welcome to the world of phptpoint
In the above example.
Declare variable($str). With string value="Welcome" .
Now concatenate this string to another string using the same variable
by performing ($str.="to the world of php").
It concatenate "welcome" with "to the world of php" and
The output will become this: Welcome to the world of Phptpoint

Popular Tutorials

PHP Tutorial PHP Introduction For Beginners PHP Advantage PHP Software Requirement PHP WAMP Installation PHP XAMPP Installation PHP Syntax PHP Comments PHP Variables PHP $ and $$ Variables PHP Super global variables How to swap two numbers in PHP PHP Constant PHP:Magic Constant echo and print in PHP PHP Data Types PHP Form GET Method POST Method Form action PHP extract() Function PHP Display result in Textbox Multiple submit buttons php different actions Add two textbox values and display the result in third textbox PHP Operators PHP Arithmetic Operators PHP Assignment Operators PHP Comparative Operators PHP Logical Operators PHP Operators Precedence PHP Conditional Statement PHP if Statement PHP if-else Statement PHP if elseif else Statement PHP Switch Statement PHP calculator Profit and Loss What is Loop PHP For Loop Print star pattern in PHP Create table using for loop in PHP PHP While Loop PHP Do While Loop PHP Foreach Loop PHP String PHP String Functions PHP empty() function PHP str_split() Function PHP Numeric Functions PHP Tutorial PDF For Beginners PHP isset function PHP addcslashes() Function PHP addslashes() Function PHP bin2hex() Function PHP chop() Function PHP chr() Function PHP chunk_split() Function PHP convert_cyr_string() Function PHP convert_uudecode() Function PHP convert_uuencode() Function PHP count_chars() Function PHP crc32() Function PHP crypt() Function PHP echo() Function PHP explode() Function PHP fprintf() Function PHP get_html_translation_table() Function PHP hebrev() Function PHP hebrevc() Function PHP hex2bin() Function PHP html_entity_decode() Function PHP htmlentities() Function PHP htmlspecialchars() Function PHP htmlspecialchars_decode() Function PHP implode() Function PHP join() Function PHP lcfirst() Function PHP levenshtein() Function PHP localeconv() Function PHP ltrim() Function PHP md5() Function PHP md5_file() Function PHP metaphone() Function PHP money_format() Function PHP nl_langinfo() Function PHP nl2br() Function PHP number_format() Function PHP ord() Function PHP parse_str() Function PHP print() Function PHP printf() Function PHP quoted_printable_decode() Function PHP quoted_printable_encode() Function PHP quotemeta() Function PHP rtrim() Function PHP setlocale() Function PHP sha1() Function PHP sha1_file() Function PHP similar_text() Function PHP soundex() Function PHP sprintf() Function PHP sscanf() Function PHP str_getcsv() Function PHP str_ireplace() Function PHP str_pad() Function PHP str_repeat() Function PHP str_replace() Function PHP str_rot13() Function