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?
Just use .
for concatenating.
And you missed out the $personCount
increment!
while ($personCount < 10) {
$result .= $personCount . ' people';
$personCount++;
}
echo $result;
One step (IMHO) better
$result .= $personCount . ' people';
This should be faster.
while ($personCount < 10) {
$result .= "{$personCount} people ";
$personCount++;
}
echo $result;
while ($personCount < 10) {
$result .= ($personCount++)." people ";
}
echo $result;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With