Using Simultaneous Assignment Operator for Two Variables

Is it possible to use an operator .=

to add the same argument in 2 or more variables at the same time?

How it is (doesn't work, but an example)

$a = "Hello";
$b = "Hi";

$a AND $b .= " World!";

// Now $a = "Hello World!" and $b = "Hi World!"

      

+3


source to share


2 answers


You can use:

$items = array('Hello', 'Hi');
foreach ($items as &$item) $item .= ' World!';
var_dump($items);

      



Or:

$a = "Hello";
$b = "Hi";
foreach (array('a', 'b') as $key) $$key .= ' World';
var_dump($a);
var_dump($b);

      

+2


source


There is no way to do this with the concatenation assignment operator.



+1


source







All Articles