How much of this is required by the assignment?
e.g., is the "QuizAnswers" file required to be formatted in this particular way?
are you required to use particular functions (like [man]explode/man, instead of, say, [man]fgetcsv/man)?
some thoughts:
$rightAnswers=array();
$fileName="QuizFiles/QuizAnswers.txt";
if(file_exists($fileName) == FALSE) {
echo "There was a problem reading the file<br />\n";
}//end of file_exists
else {
if ((is_file($fileName)) && (is_readable($fileName))) $lineInFile=file_get_contents($fileName);
}//end of else
// you did this at the top of your script. why are you doing it again?
$rightAnswers=array();
// explode() creates an array, so you didn't need `$rightAnswers = array()` at all.
$rightAnswers=explode(", ", $lineInFile); //Using ',' as delimiter divide to string (actually numbers).
// ALSO,
// If QuizAnswers.txt does not exist, `$lineInFile` will never have been defined -
// which will cause an error when you try to use it in `explode()`.
// `array_values()` also creates an array
// you're actually replacing `$rightAnswers` with another, identical array - this is completely nonproductive.
$rightAnswers=array_values($rightAnswers);
Say your [font=monospace]QuizAnswers.txt[/font] file looks something like this:
T,T,F,T,F,F,T,F,A,C,C,B,A,C,5,7,1000,"green"
I've got some true/false answers, some multiple-choice, numeric answers, and a string. Everything is formatted as a CSV string - each value is on the same line, separated by a comma. Reading the answers into an array is as simple as this:
<?php
## Example Code. Not a Working Solution. ##
$filename = '/path/to/QuizFiles/QuizAnswers.txt';
if(
file_exists( $filename ) // can we find the file?
&& ( $filehandle = fopen( $filename,'r' ) ) !== false // can we open the file?
){
$rightAnswers = fgetcsv( $filehandle ); // we're only getting one line
fclose( $filehandle ); // we don't need the file anymore
// `$rightAnswers` will be a numerically indexed array that holds the answers provided by the file.
// you might do some validation here, e.g., `is_array( $rightAnswers )`
// or `count( $rightAnswers ) == {however many answers there are *supposed* to be}`
// check answers here.
// (I think you may have more questions once you get to this point.)
}else{
// file couldn't be opened for some reason.
// show an error message (or whatever you prefer).
}
[edit] just caught this:
//Using ',' as delimiter divide to string (actually numbers).
the string in question is a string. The fact that there are commas in it will prevent PHP from automatically treating it as a number.
That may happen after you remove the commas.
Additionally, your code is not using ","(comma) as a delimiter: it's using ", "(comma space).
That may cause problems, depending on how you wrote the file (if you inserted spaces or not).