cartesianbeef Posted April 10, 2009 Share Posted April 10, 2009 Hi, probably a common problem but here goes: What I want to do is take a word from a form and break it up into letters, then add ':' before and after each letter then save the entire array to the DB. I've got so far, it echoes fine, but the for loop overwrites the variable each time so I'm left with the last letter. Here's some example code: <?php $splitu = "hello World!"; for($i = 0; $i < strlen($splitu); $i++) { $mess = ":".$splitu[$i].":"; echo $mess;//this works fine, result is: :h::e::l::l::: ::W:::r::l:::!: } ?> <html><head> <title><?=$mess?></title> //this just gives me :!: </head> <BODY BGCOLOR="#000000" TEXT="#ffffff" LINK="#ff0000" VLINK="#808080"> </body> </html> any help would be appreciated Thanks Quote Link to comment https://forums.phpfreaks.com/topic/153480-for-loop-overwriting-varible/ Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 You're not concatenating each letter with the surrounding ':' character. You're always overwriting the last $mess with the new split element. Change this line to: (notice the . ) $mess .= ":".$splitu[$i].":"; Quote Link to comment https://forums.phpfreaks.com/topic/153480-for-loop-overwriting-varible/#findComment-806394 Share on other sites More sharing options...
ToonMariner Posted April 10, 2009 Share Posted April 10, 2009 <?php $str = "Hello twitter"; $str = ":" . implode('::',str_split($str)) . ":"; echo $str; ?> that should give you: :h::e::l::l::: ::t::t::w::i::t::t::e::r: Quote Link to comment https://forums.phpfreaks.com/topic/153480-for-loop-overwriting-varible/#findComment-806402 Share on other sites More sharing options...
cartesianbeef Posted April 10, 2009 Author Share Posted April 10, 2009 maq, it worked... thank you! Could I ask what is it that the '.' did exactly? Toonmariner, thanks go to you as well! You're suggestion works just fine also... Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/153480-for-loop-overwriting-varible/#findComment-806420 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 maq, it worked... thank you! Could I ask what is it that the '.' did exactly? It's the same thing as doing: $mess = $mess . ":".$splitu[$i].":"; A better example is with an arithmetic example: $a = 2; $b = 4; $a += $b; This is the same as $a = $a + $b; Quote Link to comment https://forums.phpfreaks.com/topic/153480-for-loop-overwriting-varible/#findComment-806424 Share on other sites More sharing options...
ToonMariner Posted April 10, 2009 Share Posted April 10, 2009 guys - use the native functionality - its much more efficient. looping over a string based on its length to 'split' it is what str_split does. the code is simpler (easier to manage) and far less work to achieve the same goals. Quote Link to comment https://forums.phpfreaks.com/topic/153480-for-loop-overwriting-varible/#findComment-806428 Share on other sites More sharing options...
cartesianbeef Posted April 10, 2009 Author Share Posted April 10, 2009 I tend to use naive functionality but I'm just getting to know php so I'll go with anything that works at the mo! Thanks to you both Quote Link to comment https://forums.phpfreaks.com/topic/153480-for-loop-overwriting-varible/#findComment-806452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.