Assignment Operators in PHP : Tutorial



"=" is the Assignment Operator in PHP

$a = 5; implies that $a variable is assigned a value 5 ($a holds value 5)
For Arrays "=>" operator is used to assign a value to a named key.
Example :
<?php

$a = 2;  //$a holds value 2
$b = "Two";  //$b holds value Two


echo $a   ."<br>";
echo $b   ."<br>";
}

?>
Output

2 Two


Example :
<?php

$a = 5;

//first $b is assigned the value 4 and then addition is carried out
echo $a + ($b=4);

}

?>
Output

9


Example :
<?php

 $a = 5;
 $a += 10; //is same as $a = $a + 10
 
 $b = 5;
 $b -= 10; //is same as $b = $b - 10

 echo $b;
 
 $c = "I love ";
 $c .= "PHP!"; //is same as $c = $c."PHP!"

 echo $c;

}

?>
Output

15 -5 I love PHP!