Google News
logo
PHP Program to Use fgetc() to read a single character from a file
In the following example of PHP program that uses `fgetc()` to read a single character from a file :
Program :
<?php
// Open file for reading
$file = fopen("example.txt", "r");

// Read file character by character using fgetc() function
while (!feof($file)) {
  $char = fgetc($file);
  echo $char . "\n";
}

// Close file
fclose($file);
?>
In this example, we first use the `fopen()` function to open a file named "example.txt" in read mode ("r").

We then use a while loop to read the contents of the file character by character using the `fgetc()` function.

The loop continues until the `feof()` function returns true, indicating that the end of the file has been reached.

Each character is output using the `echo` statement, along with a newline character to separate the characters.

Finally, we use the `fclose()` function to close the file after we are done reading from it.