Sunday, 24 April 2016

PHP string concatenation




I need to know if its 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 cann't use the + sign in concatenation so what is the alternative ?


Answer



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



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


echo $result;

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...