Forum Moderators: coopster
<?
$file_handle = fopen("list.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode('=', $line_of_text);
$myArray = array($line_of_text);
}
list($var1, $var2, $var3) = array_values($parts); #how do i make this able to change as the list.txt changes?
echo $var1;
echo $var2;
echo $var3;
?>
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.[uk3.php.net...]
$var = file_get_contents('/path/to/file.txt');
echo $var;
$file_handle = fopen("list.txt", "rb"); // Open the file.
$parts = array(); // Declare this variable outside of the loop so it's visible.
while ($parts[] = fgets($file_handle)) {} // Cycle through until the end of the file.
// Store the current line as the next element in $parts[]. You could keep your current conditional statement
// inside the while loop and move what I have down inside of it. But I prefer this method. It's more compact.
// Don't use seperate variables here to store each value. You said the list changes. That's what arrays are for.
// You probably want to cycle through the array and do something. We'll just print it back out with line numbers.
for ($i=0; $i < sizeof($parts); $i++){
echo "($i) $parts[$i]<br />";
}
fclose($file_handle); // Close the file.