fgets function


Using the <pre> tag, we show the code below which produces the results shown below the code. The code you see below is also run "behind the scenes" in PHP

The fgets function returns a string it reads from a file specified by the file handle, which must have been created with fopen, fsockopen, or popen. The fgets function will attempt to read as many characters as specified by the length argument less one. An end of line marker is treated as a stopping point as is the end of the file.

BEGIN PHP output


^Nelson, Bill^WebDeveloper^54^ Male
^Nelson, Rita^Stocking^47^ Female
^Nelson, Caroline^Student^17^ Female
^Nelson, Sarah^Student^15^ Female
^Nelson, Joel^Construction^24^ Male
^Nelson, Rachel^Singer^26^ Female

END PHP output

<?

/*  open file and print each line  */

if($myFile = fopen("data.txt", "r"))  {
    while(!feof($myFile))  { 
        $myLine = fgets($myFile, 255);
        print("$myLine\n");
     }

     /*  Close directory  */

     fclose($myFile);
}

?>

Back to Bill's PHP