Skip navigation.
All programmers make syntax errors

Chapter 6 Hints and Help

QUESTION: I am using \n in my fputs() statements but new lines are NOT showing up in my text file!

ANSWER: Your code is actually correct but you are perhaps using Windows Notepad to look at your text files. Notepad looks for a carriage return character instead of a new line character to display a new line. You can add this carriage return using \r before \n in your fputs statements, for example:

fputs($reservations, "Rome:$numTravelers:$numNights\r\n");

QUESTION: My code appears to work but I get a notice saying "non well formed numeric value encountered". What's up?

ANSWER: This notice is being generated because you are using a different operating system to read the lines of date from the file than the operating system that was used to create the file. macOS and Linux use a single character for end of line characters ('\n') while Windows used two ("\r\n"). As a result the data read from the file includes the extra character and PHP reports that the data is not a well-formed numeric value. So you should be able to avoid this notice by using the trim() function to trim each line that is read from the file, since this will remove any extra whitespace characters from the input, for example in fixit1.php:

$wage1 = trim(fgets($wageFile));
$wage2 = trim(fgets($wageFile));
$wage3 = trim(fgets($wageFile));

QUESTION: I am using the list() and explode() functions but I'm getting an error.

ANSWER: A common error is to send the wrong variable to the explode() function. Do not send the variable associated with the file that you read a line from. Instead send the variable that contains the line that you just read from the file.

For example if a line in a file contains someone's first and second name separated by a colon, and you use a variable named $nextLine to read the line from the file, then $nextLine is the variable that you "explode":

$someFile = fopen("test-file.txt", "r");
$nextLine = trim(gets($someFile));
list($firstName, $secondName) = explode(":", $nextLine);
fclose($someFile);

The important thing is to understand that you use explode() function with the $nextLine variable and not the $someFile variable, because $nextLine contains the line that was read from the file.

QUESTION: But I don't understand WHEN to use the list() and explode() functions!

ANSWER: You don't need to use these functions if each line in the file contains a single value, such as a number or name of a city. You only need list() and explode() if each line contains multiple values separated by a standard separating character such as a colon or dash or comma. See the next question for a more complete explanation.

QUESTION: I don't understand HOW to use the list() and explode() functions!

ANSWER: The explode() function is used to separate values in a character string based on a "separator" character such as a colon or dash or comma. You must send it the separator character, followed by the character string you want to "explode", for example:

explode(":", "Mary:Smith")

This will explode the string "Mary:Smith" into "Mary" and "Smith". You can then assign these two values to separate variables using the list() function, for example:

list($firstName, $secondName) = explode(":", "Mary:Smith");

Now "Mary" will be stored in $firstName and "Smith" will be stored in $lastName.

The list() and explode() functions can be used to extract values from a string that has been previously stored in a variable, for example:

$name = "Mary:Smith";
list($firstName, $secondName) = explode(":", $name);

In this course we are using list() and explode() to extract values that have been read from a line in a file and stored in a variable, for example:

$someFile = fopen("test-file.txt", "r");
$nextLine = trim(fgets($someFile));
list($firstName, $secondName) = explode(":", $nextLine);
fclose($someFile);

Here the explode() function is being used to separate the values stored in $nextLine, which contains the line of text that was just read from the file.

QUESTION: Is it possible to have PHP open and write data to Excel instead of a text file? Or import data from an Excel file into a PHP program?

ANSWER: You can write from PHP to a comma-delimited text file with a .csv extension that can then be imported directly into Excel. and you can export from Excel to a comma-delimited (.csv) text file. In Chapter 6 we learned to create a file for write operations and write data to the file. We can use these methods to write records to a .csv file, using the comma as a separator:

$outFile = fopen("test.csv", "w");
fputs($outFile, "John,Jones,student,2012\n");
fputs($outFile, "Mary,Smith,student,2011\n");
fputs($outFile, "Chris,Jones,student,2010\n");
fclose($outFile);

This will save the records as a .csv file, in this case, test.csv. Now if you choose Open in Excel and select All Files or Text files from the Files of Type list, you can open this file in Excel.

Similarly you can save any Excel file as a .csv file. Then you can write a PHP app to open the .csv file for read operations and read the file using fgets() statements and the list() and explode() functions (just indicate that "," is the separator of the values in each line. Try doing that. Note that formulas and formatting is not preserved when you save Excel files as .csv files.

QUESTION: In the chapter it says to close the file as soon as the program is done using it. Well, where does it keep or save the information in the variables? Because we haven't "fputs" it yet in the .txt or any other file. The server? RAM? Or somewhere in the temp folder on the drive?

ANSWER: Good question! The PHP program creates variables when you first use them in your PHP code and these variables "live" in RAM memory until the program ends. So as long as you read the data from the file into program variables while the file is open, you can then close the file and work with the variables that contain the data after the file has closed. That way the program has the data without any need to keep the file open.

Note that working with the data stored in the variables does not effect the data in the file, unless you actually open the file again for write operations, and use fputs( statements to write the data from the variables into the file.

Right now we're just reading small amounts of data into single variables which doesn't help if you want to work with a lot of data stored in a file. Later on in chapters 11 and 12, we will see how to read much larger amounts of data from a file into array variables.