2012-07-11 24 views
40

Dizeleri birleştirmenin olası olup olmadığını bilmem gerekiyor, aşağıdaki gibi? ve değilse, bunu yapmanın alternatifi nedir?PHP dize birleştirme

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

echo $result; 

o

Çok alternatif nedir birleştirme içinde + işaretini kullanmak cann't .. vb 1 person 2 person 3 kişi gibi görünmelidir?

+8

Örnek kodunuzda "alternatifi" kullanıyorsunuz. – lanzz

+0

olası kopyası [Referans - Bu sembol PHP'de ne anlama geliyor?] (Http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – mario

+2

Bunu zor buluyorum Kimsenin "insan" yerine "insan" kullandığına işaret etmediğine inanmak. –

cevap

71

Birleştirme için sadece . kullanın. Ve $personCount artışını kaçırdınız!

$result .= $personCount . ' people'; 
7

Bir adım (IMHO) Bu kod daha hızlı olmalıdır ince

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

iyi

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

echo $result; 
+3

"İnsanlar" yerine "insanlar" 'a sahip olduğunuzu görün. – PhoneixS

+0

Muhtemelen @PhoneixS'nin işaret ettiği hatalar nedeniyle bir hata yükü alacaksınız: eşleşmeyen alıntılar –

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

echo $result; 
3

Bu çalışması gerektiğini düşünüyorum.

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

echo $result; 
+0

Herhangi bir kanıtın gösterilmesi için özen gösterme "" {$ personCount} kişi '' $ personCount'tan daha hızlıdır '' insanlar '' Aksi halde sadece vahşi spekülasyonlar gibi görünüyor ... – Jake