What is .=
in PHP? Also, I know in JavaScript, you can use +=
to add strings together, but can you do that in PHP?
views:
74answers:
2
+5
A:
Of course you can, .=
operator in PHP is the Java/JavaScript equivalent of +=
, which can be interpreted to, append another variable with itself and assign it back to itself.
$var1 .= $var2
It's equivalent to,
$var1 = $var1 . $var2;
From PHP: String Operators manual along with the provided example,
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
?>
The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.
Anthony Forloney
2010-04-30 00:47:13
equivalent to `+` or equivalent to `.`?
dreamlax
2010-04-30 00:47:43
@dreamlax, Thanks for noticing, I am currently in my Java mode, had to switch over.
Anthony Forloney
2010-04-30 00:49:01
+1
A:
.=
appends the right side of the .=
to the left side.
$something = "hello";
$something .= ", world!";
echo $something; // prints "hello, world!"
dreamlax
2010-04-30 00:47:20