PHP字符串串联


PHP string concatenation

是否可以按如下方式连接字符串?如果没有,还有什么其他选择?

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

它应该看起来像1 person 2 person 3人等

您不能在串联中使用+符号,那么有什么替代方案呢?

只需使用.进行连接。你错过了$personCount增量!

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

一步(IMHO)更好的

$result .= $personCount . ' people';

这应该会更快。

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

我认为这段代码应该可以正常工作:

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