PHP string concatenation

浪尽此生 提交于 2019-11-26 04:45:57

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';
while ($personCount < 10) {
    $result .= ($personCount++)." people ";
}

echo $result;

This should be faster.

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

echo $result;

I think this code should work fine

while ($personCount < 10) {
$result = $personCount . "people ';
$personCount++;
}
// do not understand why do you need the (+) with the result.
echo $result;

That is the proper answer I think because PHP is forced to re-concatenate with every '.' operator. It is better to use double quotes to concatenate.

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

echo $result;
Kavya Hanumantharaju
$personCount=1;
while ($personCount < 10) {
    $result=0;
    $result.= $personCount . "person ";
    $personCount++;
    echo $result;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!