Tuesday, 4 July 2017

What is the difference between .= and += in PHP?



What are the differences between .= and += in PHP?



Answer



Quite simply, "+=" is a numeric operator and ".=" is a string operator. Consider this example:



$a = 'this is a ';
$a += 'test';


This is like writing:



$a = 'this' + 'test';



The "+" or "+=" operator first converts the values to integers (and all strings evaluate to zero when cast to ints) and then adds them, so you get 0.



If you do this:



$a = 10;
$a .= 5;



This is the same as writing:



$a = 10 . 5;


Since the "." operator is a string operator, it first converts the values to strings; and since "." means "concatenate," the result is the string "105".


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...