How to combine strings inside foreach into single string PHP

后端 未结 8 1374
傲寒
傲寒 2020-12-11 04:35

I have code like this

$word = \'foo\';
$char_buff = str_split($word);

foreach ($char_buff as $chars){
    echo var_dump($chars);
}

The out

相关标签:
8条回答
  • 2020-12-11 05:24

    I'm going to assume that you have a good reason for splitting it up, only to put it back together again:

    $word = 'foo';
    $result = "";
    $char_buff = str_split($word);
    
    foreach ($char_buff as $char){
        $result .= $char;
    }
    
    echo var_dump($result);
    

    Which outputs the following:

    string(3) "foo"
    
    0 讨论(0)
  • 2020-12-11 05:26

    Kind of strange to split a string, and then glue it together again, but here goes:

    $word='foo'
    $char_buff = str_split($word);
    
    // this is what is missing, you have to define a variable first
    $newword = "";
    
    foreach ($char_buff as $chars){
         $newword .= $chars;
    }
    
    echo var_dump($newword);
    
    0 讨论(0)
提交回复
热议问题