fgetc() function in PHP

PHPProgrammingServer Side Programming

The fgetc() function gets a character from an open file. It returns false on eof, or a string containing a single character read from the file pointed to by file_pointer

Syntax

fgetc(file_pointer)

Parameters

  • file_pointer − The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

Return

The fgetc() function returns false on eof, or a string containing a single character read from the file pointed to by file_pointer.

Example

The following is an example. This reads the first character from the file.

<?php
   $file_pointer= fopen("one.txt", "r");
   echo fgetc($file_pointer);
   fclose($file_pointer);
?>

The following is the output. Let’s say our file “two.txt” has the text “Pirates of the Caribbean”. The fgetc returns the first character.

P

Let us see another example that reads character by character. Let’s say our file “two.txt” has the text “James Gosling developed by Java”.

Example

<?php
   $file_pointer = fopen("two.txt","r");
   while (! feof ($file_pointer)) {
      echo fgetc($file_pointer);
   }
   fclose($file_pointer);
?>

Output

James Gosling developed by Java
raja
Published on 03-Oct-2018 12:28:19
Advertisements