Originally you used an editor to create the text file with four names in it. The names were each on a new line. This is achieved by pressing the enter key on your keyboard. On typewriters this is called the carriage return or just return key because it moves the mechanism ready to type on the next line (it returns the typewriter mechanism to the beginning of the line ready to type the next line).
In text-based documents on a computer the same concept is used. Special invisible characters are put into the document to mark the point where the carriage should return to start a new line. Depending on your operating system this may just be done with just a new line character. The character itself is invisible but it is represented as \n when you need to type it in your code. In Windows a new line is a slightly different thing so you need to have a carriage return as well (\r\n). It is best to use the Windows method as other operating systems ignore the \r and so it works as it should on all operating systems.
New lines/carriage returns
You should still have a local copy of names.txt. The server copy now has at least one extra name in it which we do not want. Open your local copy in your editor and delete everything other than the four original names. Now upload it to the server again (it will write over the changed version).
Open phpfilewrite.php and save it as phpfilestructure.php. Amend the existing code by adding \r\n just before the word Pete (but after the first "). This will place the carriage return at the end of the previous line so that Pete now appears on it's own line.
Upload the page and try it. The page in the browser will be blank again but check the contents of names.txt using phpfileread.php. You should find Pete is now not lumped in with Ringo.
Tabs, semi-colons or commas - multiple items on a line
Often you will also want to store more than one thing on a line in a file. Each line can be thought of as a record about one thing in a database and there may be more than one item of information in each record. To extend the current example you could add in surnames so that each line looks like this:
The whole line is one record (his full name) but there are actually two separate items in there (first name and surname). The space could be used to separate the names but it is better to use another character. Traditionally a comma, semi-colon or tab is used. Tab may be the most flexible. To include a tab when writing to the file use \t in the same way as you used \r\n. Delete names.txt on the server (right click on it if you are using Filezilla). Then replace the existing code in phpfilestructure.php with this:
$handle = fopen("./names.txt", "a"); fwrite($handle, "Paul\tMcCartney\r\n"); fwrite($handle, "John\tLennon\r\n"); fwrite($handle, "George\tHarrison\r\n"); fwrite($handle, "Ringo\tStarr\r\n"); fwrite($handle, "Pete\tBest\r\n"); fclose($handle);
Upload and view the page in the browser (remember it is supposed to be blank unless there are errors). Look again at the text file on the server using phpfileread.php. You should now see five full names with invisible tabs between the first name and surname.
Although this data should actually be stored in a database there are times when a text file will do so this is a useful skill to have. It also introduces some of the concepts which you will need to understand when you do start using databases. More on that later.