62

Is it possible to concatenate strings, as follows? And if not, what is the alternative of doing so?

while ($personCount < 10) {
    $result += $personCount . "person ";
}

echo $result;

It should appear like 1 person 2 person 3 person, etc.

You can’t use the + sign in concatenation, so what is the alternative?

3
  • 8
    You are actually using the "alternative" in your example code.
    – lanzz
    Jul 11, 2012 at 20:59
  • 2
    I find it hard to believe that no-one has pointed out that you are using "people" instead of "person". Jul 4, 2017 at 8:43
  • 2
    The bigger question is WHY does php use "." instead of "+" for string concatenation? I mean.. Seriously? Mar 4, 2018 at 0:14

6 Answers 6

99

Just use . for concatenating. And you missed out the $personCount increment!

while ($personCount < 10) {
    $result .= $personCount . ' people';
    $personCount++;
}

echo $result;
10

One step (IMHO) better

$result .= $personCount . ' people';
7

This should be faster.

while ($personCount < 10) {
    $result .= "{$personCount} people ";
    $personCount++;
}

echo $result;
5
  • 4
    Care to cite any evidence that "{$personCount} people" is faster than $personCount . ' people'? Otherwise it just seems like wild speculation...
    – Jake
    Dec 9, 2017 at 23:33
  • PHP is forced to re-concatenate with every '.' operator. It is better to use double quotes to concatenate. Apr 5, 2018 at 3:43
  • 1
    @Abdul Alim Shakir: But there is only one concatenation, so it shouldn't make any difference(?). May 16, 2021 at 9:48
  • Using . instead .= (the first instance) does affect performance (probably due to the Schlemiel the Painter's algorithm - this can happen in any language or system), but it is already using .=. May 16, 2021 at 10:20
  • cont': From Assignment Operators: "Using $text .= "additional text"; instead of $text = $text . "additional text"; can seriously enhance performance due to memory allocation efficiency. I reduced execution time from 5 seconds to 0.5 seconds (10 times) by simply switching to the first pattern for a loop with 900 iterations over a string $text that reaches 800K by the end." May 16, 2021 at 10:26
6
while ($personCount < 10) {
    $result .= ($personCount++)." people ";
}

echo $result;
0

I think this code should work fine:

while ($personCount < 10) {
    $result = $personCount . "people ';
    $personCount++;
}
# I do not understand why you need the (+) with the result.
echo $result;
2
  • 5
    See that you have "people ' instead of 'people '.
    – PhoneixS
    Jan 21, 2014 at 11:55
  • 1
    You will probably get a load of errors because of what @PhoneixS has pointed out: mismatched quotes. Jul 4, 2017 at 8:45
0
$personCount = 1;
while ($personCount < 10) {
    $result = 0;
    $result .= $personCount . "person ";
    $personCount++;
    echo $result;
}
1
  • 6
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
    – Nic3500
    Aug 9, 2018 at 10:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.