9

I have code like this

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

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

The output was

string(1) "f" 
string(1) "o" 
string(1) "o"

For some reasons, I want to make the output become only 1 string like this:

string(3) "foo"

I tried with this

$char.=$chars;
echo var_dump($char);

But it shows error Undefined variable: char.

Sampson
  • 265,109
  • 74
  • 539
  • 565
Wakanina
  • 592
  • 3
  • 6
  • 20

8 Answers8

17

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"
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • All of you guys have a great answer but I choose Jonathan Sampson as the best answer. My purpose with this is I want to create a key based on the user inputted character. So first I convert each string characters to a variable, like $f, $o, and $o. Then each those variables have a unique string. And then I combine those unique strings, and store in my database. So basically the output will not "foo" again. That's why I need it. – Wakanina Dec 13 '12 at 15:54
2

str_split() converts a string to an array. There's no need to use this function if you want to keep the whole word.

SeanWM
  • 16,789
  • 7
  • 51
  • 83
  • My purpose with this is I want to create a key based on the user inputted character. So first I convert each string characters to a variable, like $f, $o, and $o. Then each those variables have a unique string. And then I combine those unique strings, and store in my database. So basically the output will not "foo" again. That's why I need it. I just want to know the basic method to do this. – Wakanina Dec 13 '12 at 15:58
2

I would just use implode, much like this: $string = implode('', $char_buff);

Rosme
  • 1,049
  • 9
  • 23
  • Empty glue is the default setting, you can omit the empty string parameter in this case. – mickmackusa Apr 08 '21 at 22:53
  • @mickmackusa Back in 2012 when I wrote the answer, that was not the case... – Rosme Apr 12 '21 at 16:21
  • There is nothing in the changlelog to suggest that that is true. https://www.php.net/manual/en/function.implode.php Please site proof of your claim. – mickmackusa Apr 12 '21 at 21:25
1

So, why do you split it just to make it a string again?

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

$final = array();
foreach ($char_buff as $chars){
    $final[] = $chars;
}

var_dump( implode('', $final) );
vectorialpx
  • 587
  • 2
  • 17
0

Sounds like you are looking for implode() http://php.net/manual/en/function.implode.php

As for the code you posted $chars .= $char; is probably what you were trying to do

0

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);
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0
<?php
$word = 'foo';
$char_buff = str_split($word);

// here is the trick
$length = count($char_buff);
$char_buff[$length] = $word;

foreach ($char_buff as $chars)
{
    echo var_dump($chars);
}
?>
Tahir Yasin
  • 11,489
  • 5
  • 42
  • 59
0

Maybe some of you are looking for this answer. I think var_dump() is no longer necessary for this problem.

<?php 
    if(isset($_POST['test'])){
        $result = '';
        for($x=1;$x<=4;$x++){
            $ans = $_POST['ans'.$x];
            $result .= $ans;
        } 
    echo $result; 
    }
?>

Here is the HTML

<form role="form" method="post" action="<?php echo $url;?>">
 <input type="checkbox" name="ans1" value="A">A 
 <input type="checkbox" name="ans2" value="B">B 
 <input type="checkbox" name="ans3" value="C">C 
 <input type="checkbox" name="ans4" value="D">D 
 <input type="submit" name="test" value="Submit Answer">
</form>
xreyc
  • 203
  • 2
  • 8