+= is allowed in PHP. It is, however, only for mathematical operations and has nothing to do with the script above. (unlike JS, you would use .= for strings.)
One major problem here, and I don't know why it isn't giving you an error, is that you're not using implode('|',$arr);
implode, I believe, requires a first operator of the "glue" to reconnect the strings. Feel free to use "" as an empty string if you want.
However, overall, that doesn't do what you want. Using a for loop generally requires that you know how many splits you have first, or at least that you're using a value inside the loop to at some point change and terminate the loop. Neither is the case. It's surprising it isn't an infinite loop.
Anyway, here's something that will work:
Version 1:
PHP Code:
<?php
$str = 'abc|def|ghi';
//simplest, just replace:
$str = str_replace('|',"\n",$str);
echo $str;
///OR
//easy foreach loop:
$str = explode('|',$str);
foreach ($str as $ln) {
echo $ln."\n";
}
///OR
//a more complex for loop:
$str = explode('|',$str);
for ($i=0;$i<count($str);$i++) {
echo $str[$i]."\n";
}
///OR
//if you must avoid using explode:
while (strpos($str,'|')!==FALSE) {
echo substr($str,0,strpos($str,'|'))."\n"; //echo everything before the first |
$str = substr($str,strpos($str,'|')+1); //replace the string with everything after the first |
}
echo $str."\n"; //echo the last bit, outside of the loop, since there is no | now
Bookmarks