0

Appending to a single string is easy:

$string = "saucy";
$output = " you smell ". $string;
print $output;
// you smell saucy.

PHP also has a easy way of setting multiple values at once:

$aa = $bb = $cc = [];

But what if I want to append a string to multiple strings simultaneously?

For example:

$aa['horses'] = "trolls!";
$stringSomething = "This is better: ";
$stringElse = "This is less worse : ";

foreach($aa as $bb){
   $stringSomething .= $stringElse .= $bb; // THIS line
}

print $stringSomething;
print $stringElse;

This outputs:

This is better: This is less worse : trolls!
This is less worse : trolls!

What I am curious about is if you can have an output like this:

This is better: trolls!
This is less worse : trolls!

Currently all I can see is:

foreach($aa as $bb){
   $stringSomething .= $bb;
   $stringElse .= $bb; 
   $stringEntirely .= $bb;
   // .... 

}

Is there a way of wrapping this all up into a single line of appending the same value to multiple strings at once?

NOTE

The question here is NOT "How can I do this on an array", it is asking if there was a shorthand way of doing this with variables, as is possible with initial variable setting ( ie $a = $b = $c = 67;).

4
  • 2
    No, there is not. But if you stopped using the terrible practice of "numbering" variable names, and used an array instead - then you could loop over the elements in that, and append the value to each. Commented Aug 25, 2021 at 11:14
  • @CBroe this was purely for illustrative purposes. Commented Aug 25, 2021 at 11:23
  • Still, your data must have some sort of logical relation between those values, I suppose, so that processing them in such a combined way would make sense to begin with ... so it could still be grouped in an array, instead of having "independent" variables lingering around. A $strings array with elements under keys Something, Else and Entirely then in this particular instance :-) Commented Aug 25, 2021 at 11:29
  • @CBroe Yes I can see what you're saying but I was curious if there was a shortcut in PHP natively for doing this, and it seems there may not be. Commented Aug 25, 2021 at 11:41

4 Answers 4

1

The way to go would be to have these strings in an array and process like this:

<?php

$aa = [];
$aa['horses'] = "trolls!";
$strings = ["This is better: ", "This is less worse : "];

foreach($aa as $bb){
    foreach ($strings as &$string) {
        $string .= $bb;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But you'd need to use references (or directly access the original array element via its key), otherwise the change inside the foreach loop will only be applied to a copy of the data, and not change the original array.
Thx for feedback, you're right on that. Answer is already updatet with passing by reference.
1

You can make it by using dynamic variable and an array with the name of your variables: dynamic variables

<?php

$aa = [];
$aa['horses'] = "trolls!";
$name1 = "smth";
$name2 = "smthelse";
$strings = ["name1", "name2"];

foreach($aa as $bb){
    foreach ($strings as $string) {
        $$string .= $bb;
    }
}

Comments

0

You can use array_map() by passing references to it:

<?php

$suffix = "trolls!";
$string1 = "This is better: ";
$string2 = "This is less worse : ";

array_map(fn(&$s) => $s .= $suffix, [&$string1, &$string2]);

echo "$string1\n";
echo "$string2\n";

Comments

0

There is currently no syntax of any specific shorthand to do this on pure variables.

There are various ways this can be done with array values and looping but this would involve adding code lines to the script and mitigate the intention to DRY down the original code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.