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.

2
  • 6
    If you want it all together, why are you splitting it? Commented Dec 13, 2012 at 14:37
  • Define $char as an empty string before your loop Commented Dec 13, 2012 at 14:38

8 Answers 8

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"
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

1 Comment

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.
2

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

3 Comments

Empty glue is the default setting, you can omit the empty string parameter in this case.
@mickmackusa Back in 2012 when I wrote the answer, that was not the case...
There is nothing in the changlelog to suggest that that is true. php.net/manual/en/function.implode.php Please site proof of your claim.
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) );

1 Comment

Empty glue is the default setting, you can omit the empty string parameter in this case.
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

Comments

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);

Comments

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);
}
?>

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.