PHP 字符串连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11441369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP string concatenation
提问by Illep
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 3person etc..
它应该看起来像1 person 2 person 3人等。
You cann't use the +sign in concatenation so what is the alternative ?
您不能+在连接中使用符号,那么替代方法是什么?
回答by abhshkdz
Just use .for concatenating.
And you missed out the $personCountincrement!
仅.用于连接。而你错过了$personCount增量!
while ($personCount < 10) {
$result .= $personCount . ' people';
$personCount++;
}
echo $result;
回答by Loren Wolsiffer
One step (IMHO) better
一步(恕我直言)更好
$result .= $personCount . ' people';
回答by Farly Taboada
while ($personCount < 10) {
$result .= ($personCount++)." people ";
}
echo $result;
回答by TurKux
This should be faster.
这应该更快。
while ($personCount < 10) {
$result .= "{$personCount} people ";
$personCount++;
}
echo $result;
回答by Kavya Hanumantharaju
$personCount=1;
while ($personCount < 10) {
$result=0;
$result.= $personCount . "person ";
$personCount++;
echo $result;
}
回答by salim
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;

