Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 422
Derek Adair Avatar asked Feb 04 '10 18:02

Derek Adair


People also ask

What is the difference between != And !== In PHP?

Operator != returns true, if its two operands have different values. Operator !== returns true, if its two operands have different values or they are of different types.

What is the difference between and and && in PHP?

The difference is the precedence when we compare AND with && operator. The precedence of AND operator is lower than the operator = when the evaluation is performed, therefore even if both the operators do the same work, the result is different.

What is the difference between $message and $$ message in PHP?

$message is used to store variable data. $$message can be used to store variable of a variable. Data stored in $message is fixed while data stored in $$message can be changed dynamically.

What is %s and %D in PHP?

This syntax works outside of classes as well. From the documentation: d - the argument is treated as an integer, and presented as a (signed) decimal number. s - the argument is treated as and presented as a string.


1 Answers

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".

like image 157
Brian Lacy Avatar answered Sep 24 '22 22:09

Brian Lacy