fgets working explaination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    fgets working explaination

    [PHP]<?php
    $handle = fopen("welcome. txt", "r");
    if ($handle) {
    while (!feof($handle) ) {
    $buffer = fgets($handle);
    echo $buffer;
    echo "<br>";
    }
    fclose($handle) ;
    }
    ?>
    [/PHP]


    this is working as needed for me but i am not getting how it works.. i know tht fgets gets the string of length specified .i am not able to know here how it goes to next line automatically by itself .can any one please explain this to me plzz...


    Thanks,
    Pradeep
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Would be easier if you just read the PHP documentation on the fgets() command
    Originally posted by PHP manual
    Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.
    Ronald

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      Yea, the documentation pretty much explains it all.

      If it is sill confusing, consider how you code is being interpreted:
      Code:
      while ( we havent reaced the end of the file) {
        Read one line into the buffer;
        Print the buffer;
        Print <br />
      }

      Comment

      • aktar
        New Member
        • Jul 2006
        • 105

        #4
        basically the fgets() command read a new line everytime its called. So if its on line 1 now, it will read line 2 the next time it is called, very similar to navigating through an array with the next() command

        Comment

        Working...