Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Filesystem fgetc() Function



The PHP Filesystem fgetc() function is used to read one character from an open file. It takes a file pointer as an argument. The function can return a single character from an open file and gets a character from a given file pointer.

This function can return boolean false, and may also return non-boolean value that evaluates to false such as 0 or " ".

This function processes very large files very slowly, so it can not be used to process large files. If we need to read one character from a large file in sequence, use fgets() function to read one line of data in sequence, then use fgetc() function to process the line of data in sequence.

Syntax

Below is the syntax of the PHP Filesystem fgetc() function −

string fgetc ( resource $handle )

Parameters

The parameters are needed to use the fgetc() function are mentioned below −

Sr.No Parameter & Description
1

handle(Required)

This is a pointer to a FILE object that indicates the handle from which the character is read.

Return Value

It returns String value on success, or FALSE on failure.

PHP Version

The fgetc() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.

Example

This PHP code shows us how we can open a file and read a single character from it using the PHP Filesystem fgetc() function. And after that close the file.

<?php
   $file = fopen("/PhpProject/sample.txt", "r");
   echo fgetc($file);
   fclose($file);
?>

Output

This will produce the following result −

t

Example

This PHP code shows us how we can open a file and read each character until the end of the file using the while loop after that close the file.

<?php
   $file = fopen("/PhpProject/sample.txt", "r");
   while(! feof($file)) {
      echo fgetc($file);
   }
   fclose($file);
?>

Output

This will create the below outcome −

tutorialspoint

Example

Since the fgetc() function returns a String value, or FALSE on failure so you can use conditional expressions to verify this. Check the sample of code below −

<?php
   // Open the file in read mode
   $file = fopen("example.txt", "r"); 

   // Check if the file was opened successfully
   if ($file) { 
      // Read characters until the end of the file
      while (($char = fgetc($file)) !== false) { 
         echo $char; // Print each character
      }
      fclose($file); // Close the file
   } else {
      // Error message if file could not be opened
      echo "Could not open file."; 
   }
?>

Output

This will generate the below result −

Hello World

Tutorialspoint

Information Security

Web Browser

Example

This PHP code counts how many times a particular character or letter appears in a given file. So we have used two variables $charToCount and $count, initialized them with a and 0 respectively.

<?php
   // Open a file 
   $file = fopen("/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt", "r"); // Open in read mode
   $charToCount = 'a';
   $count = 0;

   if ($file) {
      while (!feof($file)) {
         if (fgetc($file) == $charToCount) {
               $count++;
         }
      }
      // Close the file
      fclose($file); 
      echo "The character '$charToCount' appears $count times in the file.";
   } else {
      // Error message if file could not be opened
      echo "Could not open file."; 
   }
?> 

Output

This will generate the following outcome −

The character 'a' appears 22 times in the file.

Example

In this PHP example code we will try to read characters from a file and stop when a particular character is found. We have used $stopChar variable and initialized it with dot (.) for which we have to stop at.

<?php
   // Open text file in read mode
   $file = fopen("/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt", "r"); 

   // Particular Character for which we have to stop at
   $stopChar = '.';

   if ($file) {
      while (!feof($file)) {
         $char = fgetc($file);
         if ($char === $stopChar) {
               // Stop reading when the specific character is found
               break; 
         }
         
         // Print each character
         echo $char; 
      }

      // Close the file
      fclose($file); 
   } else {

      // Error message if file could not be opened
      echo "Could not open file."; 
   }
?> 

Output

Here is the output of the above code −

Hello World!!!!!

Today is 13 June 2024 and I am here to help you to learn PHP functions

Notes

  • When the fgetc() method gets to the end of the file it will return false.
  • Before calling fgetc(), always make sure that the file has been successfully opened. Also, use false correctly to avoid infinite loops or issues.

Summary

PHP's fgetc() function reads one character at a time from an open file. On failure, it returns a string or false. It is inefficient for large files; for better performance, read lines using fgets() and process them with fgetc().

php_function_reference.htm
Advertisements