lingo
lingo

Reputation: 1908

Using concatenating assignment operator for 2 variables same time

Can I use.= operator to append the same argument to 2 or more variables at the same time?

Like this (not working but example)

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

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

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

Upvotes: 3

Views: 215

Answers (2)

Jordi Martín
Jordi Martín

Reputation: 519

No way to do this with concatenation assignment operator.

Upvotes: 1

Oscar Lidenbrock
Oscar Lidenbrock

Reputation: 816

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);

Upvotes: 2

Related Questions