Fair enough, I will try to break them down together with your original code that doesn't work, and the changes I made to your code (and to use your FOREACH principle (that works) to show you the differences.
Your original code
<?
if ($string == NULL){
print <<<HERE
<form>Please type 5 words to get your list
<textarea name = "string"
rows = 1
cols = 40></textarea>
<input type = "submit"
value = "submit">
</form>
HERE;
} else {
$words = split(" ", $string);
foreach ($words as $theWord){
$theWord = sort($theWord);
$newPhrase = $newPhrase . $theWord . " ";
}
print $newPhrase;
} // end if
?>
My changes to your original code
<?
extract($_GET);
if ($string == NULL){
print <<<HERE
<form>Please type 5 words to get your list
<textarea name = "string"
rows = 1
cols = 40></textarea>
<input type = "submit"
value = "submit">
</form>
HERE;
} else {
$words = split(" ", $string);
$theWord = sort($words);
$newphrase = "";
foreach ($words as $theWord){
$newphrase .= $theWord." ";
}
echo $newphrase;
} // end if
?>
1st things 1st. I put the extract($_GET); to get the $string variable from the URL. (My register globals is off, so I put it there to ensure it works either way).
Secondly, split the string into the $words array.
Thirdly, sort it while it is still an array.
Fourthly, define the $newphrase variable so you can concatenate the values to it later.
Fifthly, use your foreach loop to append each sorted value to the $newphrase variable.
Sixthly, echo your $newphrase string.
It is a bit more code then my original changes, but you wanted your code to work, and to see how to make it work.
This is how it is done, using your thought process. It's up to you to grasp and learn the whys... Have fun and good luck in your learning adventures.