PHP string concatenation

Muszę wiedzieć, czy możliwe jest łączenie łańcuchów w następujący sposób ? a jeśli nie, to jaka jest alternatywa ?

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

echo $result;

Powinno wyglądać jak 1 person 2 person 3 osoba itp..

Nie możesz użyć + logowania w konkatenacji więc jaka jest alternatywa ?

Author: Pradeep Kumar Prabaharan, 2012-07-12

7 answers

Po prostu użyj . do konkatenacji. I przegapiłeś przyrost $personCount!

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

echo $result;
 80
Author: abhshkdz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-11 20:59:18

One step (IMHO) better

$result .= $personCount . ' people';
 7
Author: Loren Wolsiffer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-11 21:03:34
while ($personCount < 10) {
    $result .= ($personCount++)." people ";
}

echo $result;
 5
Author: Farly Taboada,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-12 02:37:37

To powinno być szybsze.

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

echo $result;
 3
Author: TurKux,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 17:59:11

Myślę, że ten kod powinien działać dobrze

while ($personCount < 10) {
$result = $personCount . "people ';
$personCount++;
}
// do not understand why do you need the (+) with the result.
echo $result;
 0
Author: salim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-11 21:32:07

To jest właściwa odpowiedź, myślę, że ponieważ PHP jest zmuszony do ponownego konkatenacji z każdym'./ centrala. Lepiej jest użyć podwójnych cudzysłowów do konkatenacji.

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

echo $result;
 0
Author: Abdul Alim Shakir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-02 09:47:45
$personCount=1;<br/>
while ($personCount < 10) {<br/>
    $result=0;<br/>
    $result.= $personCount . "person ";<br/>
    $personCount++;<br/>
    echo $result;<br/>
    }
 0
Author: Kavya Hanumantharaju,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-09 07:02:19