Concatenación de cadenas de PHP


Necesito saber si es posible concatenar cadenas, como sigue ? y si no, ¿cuál es la alternativa de hacerlo ?

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

echo $result;

Debería aparecer como 1 person 2 person 3 persona etc..

No se puede utilizar la concatenación de inicio de sesión + así que, ¿cuál es la alternativa ?

Author: Pradeep Kumar Prabaharan, 2012-07-12

7 answers

Simplemente use . para concatenar. ¡Y te perdiste el incremento $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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-11 20:59:18

Un paso (en mi humilde opinión) mejor

$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/ajaxhispano.com/template/agent.layouts/content.php on line 61
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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-12 02:37:37

Esto debería ser más rápido.

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-18 17:59:11

Creo que este código debería funcionar bien

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-11 21:32:07

Esa es la respuesta adecuada, creo, porque PHP se ve obligado a volver a concatenar con cada'.' operador. Es mejor usar comillas dobles para concatenar.

$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/ajaxhispano.com/template/agent.layouts/content.php on line 61
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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-09 07:02:19