Actually, here's what I'd suggest doing. (Please note that you won't need to count the lines, the array keeps track of how many lines there are.)
$fp = fopen("test.txt","r"); // This opens the file for reading, fseek()ing to 0 bytes from the beginning of the file.
$info = fread($fp,filesize("test.txt")); // Read the file $fp (defined earlier) using a byte size equal to that of "test.txt", which happens to be the opened file.
$people = explode("\n",$info); // create an array ($people[]) based upon each new line (\n) in the file's contents ($info)
Now, we edit the file. First, you have to know what line you are going to edit. Since you said you wanted to change the first, we'll set that value to 0. The line you want to edit is going to be the line number minus one. This is due to the fact that arrays work by using 0 as the first value. If you are counting your example at the top (first_name, last_name) as merely an example, simply use the current line number, and it will ignore it (don't subtract one in this case).
$line = $people[$linenum]; // Set $line to equal the text on that line. Use $people[$linenum-1] if you don't count the example line.
$personinfo = explode(",",$line); // Create an array ($personinfo[]) with information equal to that in the file, separated by commas.
$personinfo[1] = $newfirstname; // Rewrite the first name to the value of $newfirstname. Change this to a static value if the script is static. Note: $newfirstname is not previously set, so you'll have to set it!
$personinfo[2] = $newlastname; // Rewrite the last name to the value of $newlastname. Change this to a static value if the script is static. Note: $newlastname is not previously set, so you'll have to set it!
$line = implode(",",$personinfo); // Return the array back to the string. (Set the old data to the new data.)
$people[$linenum] = $line; // Return the new line to the old line list.
$info = implode("\n",$people); // Return the list of people back into a single string.
fclose($fp); // Close the file, since we're done with reading.
$fp = fopen("test.txt","w"); // Open the file for writing, truncate file length to 0 (erase all data) and fseek() to 0 bytes from the beginning.
fwrite($fp,$info); // Write to the file all the data you have ($info).
fclose($fp); // Close the file again, since we're completely done with it this time.
This all should work. If you have any problems, post again or PM me. Keep in mind I didn't test this code, and just posted it.