63

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. Commented 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". Commented Jul 4, 2017 at 8:43
  • 2
    The bigger question is WHY does php use "." instead of "+" for string concatenation? I mean.. Seriously? Commented 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;
Sign up to request clarification or add additional context in comments.

Comments

10

One step (IMHO) better

$result .= $personCount . ' people';

Comments

7

This should be faster.

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

echo $result;

5 Comments

Care to cite any evidence that "{$personCount} people" is faster than $personCount . ' people'? Otherwise it just seems like wild speculation...
PHP is forced to re-concatenate with every '.' operator. It is better to use double quotes to concatenate.
@Abdul Alim Shakir: But there is only one concatenation, so it shouldn't make any difference(?).
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 .=.
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."
6
while ($personCount < 10) {
    $result .= ($personCount++)." people ";
}

echo $result;

Comments

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 Comments

See that you have "people ' instead of 'people '.
You will probably get a load of errors because of what @PhoneixS has pointed out: mismatched quotes.
0
$personCount = 1;
while ($personCount < 10) {
    $result = 0;
    $result .= $personCount . "person ";
    $personCount++;
    echo $result;
}

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.