fgetc()
The fgetc() function in PHP is used to read a single character from a file pointer. It's a crucial function for server administrators and web developers who
Introduction to PHP fgetc() Function
The fgetc() function in PHP reads exactly one character from an open file and advances the file pointer one byte forward. The name stands for "file get character." Each call returns the next character, so by calling it repeatedly you can walk through a file one character at a time.
You will reach for fgetc() when you need fine-grained, character-by-character control — for example, building a tiny parser, counting specific characters, or stopping the moment you hit a particular byte. For most everyday file reading, line-based fgets() or whole-file fread()/file_get_contents() are faster and simpler, because each fgetc() call carries function-call overhead. Knowing when not to use it is as important as knowing how.
This page covers the syntax, parameters, return value, runnable examples, and the common gotchas (especially the "0" end-of-file trap).
Syntax
fgetc(resource $stream): string|falseThe function takes a single argument and returns either the character read or false.
Parameters
| Parameter | Required | Description |
|---|---|---|
$stream | Yes | An open file pointer. It must be a valid resource returned by fopen(), fsockopen(), popen(), or a similar function — not a file name. |
The
resourcetype remains fully supported in PHP 8+. No changes are required for modern compatibility.
Return Values
- On success, returns a string containing a single character read from the file.
- Returns
falsewhen the end of the file (EOF) is reached or on error.
Examples
Example 1: Read a single character from a file
This reads just the first character of the file. Always check that fopen() succeeded before using the handle, and fclose() when you are done.
<?php
$fileHandle = fopen('example.txt', 'r');
if ($fileHandle) {
echo fgetc($fileHandle); // prints the first character
fclose($fileHandle);
}Example 2: Read a whole file character by character
To read the entire file, call fgetc() in a loop and stop when it returns false (EOF).
<?php
$fileHandle = fopen('example.txt', 'r');
if ($fileHandle) {
while (($char = fgetc($fileHandle)) !== false) {
echo $char;
}
fclose($fileHandle);
}This prints every character of the file until EOF.
Why !== false matters (the "0" gotcha)
A very common bug is writing the loop with a loose comparison:
// BUGGED: stops early on the first "0" or "" it reads
while ($char = fgetc($fileHandle)) {
echo $char;
}PHP treats the strings "0" and "" as falsy. If your file contains a 0 character, this loop ends there instead of at EOF. Always use the strict, identity comparison !== false so that only the real EOF value stops the loop. The same rule applies to fgets() and fread().
Example 3: Count occurrences of a character
Because fgetc() gives you one character at a time, it is handy for streaming counts without loading the whole file into memory:
<?php
$fileHandle = fopen('example.txt', 'r');
$vowels = 0;
if ($fileHandle) {
while (($char = fgetc($fileHandle)) !== false) {
if (str_contains('aeiouAEIOU', $char)) {
$vowels++;
}
}
fclose($fileHandle);
echo "Vowels: $vowels";
}fgetc() vs fgets() vs fread()
| Function | Reads | Use when |
|---|---|---|
fgetc() | One character | You need character-level control or to stop at a specific byte |
fgets() | One line (up to a newline) | You process text line by line |
fread() | A fixed number of bytes | You read binary data or large chunks at once |
For binary files, prefer fread() — looping with fgetc() works but is much slower per byte.
Notes and Gotchas
fgetc()works on binary-safe streams: it returns one byte even for non-text characters. Multi-byte UTF-8 characters span several bytes, so a singlefgetc()call may return only part of such a character.- Open the file with the right mode (
'r','rb', etc.) usingfopen(), and remember to close it withfclose(). - You can also test for end-of-file explicitly with
feof(), though comparingfgetc()againstfalseis usually enough.
Conclusion
fgetc() reads one character at a time from an open file pointer and returns false at end-of-file, which makes it ideal for character-level parsing and counting. Just remember the two essentials: pass an open resource from fopen(), and end your read loop with the strict !== false check so a literal 0 never stops you early. For line- or chunk-based work, reach for fgets() or fread() instead.
For more on working with files in PHP, see the PHP File Handling guide.