PHP 字符串串联


PHP strings concatenation

我的代码循环浏览CSV文件,将其转换为XML:

<?php
for ($i = 1; $i < $arraySize; $i++) {
    $n = 0;
    if (substr($csv[$i][0], 0, 1) == $let) {
        $surName = $dom->createElement('name');
        $name = $csv[$i][0];
        $nameText = $dom->createTextNode($name);
        $surName->appendChild($nameText);
        $text = str_replace(chr(94), ",", $csv[$i][4]);
        $n = $i + 1;
        $next = $csv[$n][0];
        while ($next == 'NULL') {
            $repl = str_replace(chr(94), ",",  $csv[$n][4]);
            $text = $repl;
            $n++;
            $next = $csv[$n][0];
        }
        $bio = $dom->createElement('bio');
        $bioText = $dom->createTextNode($text);
        $bio->appendChild($bioText);
        $person = $dom->createElement('person');
        $person->appendChild($surName);
        $person->appendChild($bio);
        $people->appendChild($person);
    }
}
$xmlString = $dom->saveXML();
echo $xmlString; 
?>

问题是打字$text .= $repl;带来的$text = $repl;

第 1 列第 1 行出错:文档为空。

但是省略.只会给出最后一行文本。备用代码有效: 公共函数测试($let){ $csv = $this->readCSV("data''AlphaIndex1M.csv"); $arraySize=大小($csv); $let = strtoupper($let); 回声"; for($i=1; $i echo $csv[$i][0];//." 回声', -->'.$csv[$i][4]; $n = $i+1; $next = $csv[$n][0]; if($next == 'NULL'){ } while($next == 'NULL'){ 回声$csv[$n][4]。'" '"; $n++; $next=$csv[$n][0]; } 回声 '' 回声"; } } 回声 '' }

您必须先初始化$text,然后才能附加内容!

所以在使用它之前写下这个:

$test = "";

(在while循环之前,如果您希望全部追加,甚至在for循环之前)