Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the .= working in this code?

Tags:

php

I was given this question as part of learning PHP.

What is the final value of $a?

$a .= "a";
$a .= "b";
$a .= "c";

I know it outputs "abc" and I understand that ".=" is a concatenation assignment, but I'm still a little unclear what is actually going on here. When I remove the periods I get "c", which makes sense that it would take the last item.

Thanks for any clarity.

like image 328
justinae Avatar asked May 14 '26 17:05

justinae


1 Answers

$a .= "a" 

...is shorthand for ...

$a = $a . "a"

At the beginning, $a is empty.

$a .= "a"
// now $a == "a"
$a .= "b"
// now $a == "a" . "b" == "ab"
$a .= "c"
// now $a == "ab" . "c" == "abc"
like image 83
slim Avatar answered May 18 '26 23:05

slim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!