PHP fgetc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fgetc() reads a single character from an open file. It returns that one character as a string, or false when the end of the file is reached.

Each call moves the file pointer one byte forward, so calling fgetc() in a loop lets you read a file one character at a time. It is handy for small checks, such as looking at the first character of a line.

Syntax

Call fgetc() with the values you need. Start with this line.

PHP
$char = fgetc($handle);
CallWhat it does
fgetc($handle)Returns the next character as a one-character string.
while (($char = fgetc($handle)) !== false) { ... }Reads the whole file one character at a time.
fgetc($handle) === '#'Checks whether the first character is a hash sign.

fgetc() reads one byte, not one visible character. For text with accents or emoji in UTF-8, a single character can use several bytes, so you will get only part of it.

Library declarations
PHP
fgetc(resource $stream): string|false

Parameters

ParameterDescription
streamRequired. An open file stream, such as the one returned by fopen(). The file must be open for reading.

Return Value

Return ValueDescription
stringA string with exactly one byte, which is the next character in the file.
falseReturned when the end of the file is reached.

PHP Examples

Example 1 — Read Characters One by One

Create a tiny file and print each character with a bar after it.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetc.txt';
file_put_contents($path, 'Hi!');

$handle = fopen($path, 'r');

while (($char = fgetc($handle)) !== false) {
    echo $char, '|';
}

echo PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. file_put_contents() creates a file that contains the three characters Hi!.
  2. fgetc() returns the next character each time it is called.
  3. The !== false check keeps the loop running until the end of the file.
  4. Each character is printed followed by a bar, so you can see them one by one.

Example 2 — Reading Past the End

When there is nothing left to read, fgetc() returns false. This is how you detect the end of the file.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetc_edge.txt';
file_put_contents($path, 'A');

$handle = fopen($path, 'r');

echo 'First: ', var_export(fgetc($handle), true), PHP_EOL;
echo 'Second: ', var_export(fgetc($handle), true), PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. The file holds only one character, the letter A.
  2. The first fgetc() call returns that character.
  3. The second call has nothing left to read, so it returns false.
  4. var_export() shows the real value, which makes false easy to see.

Example 3 — Characters Are Really Bytes

The letter é is stored as two bytes in UTF-8. fgetc() returns them one at a time, so each call gives only half of the letter.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fgetc_bytes.txt';
file_put_contents($path, chr(0xC3) . chr(0xA9));

$handle = fopen($path, 'r');
$bytes = [];

while (($char = fgetc($handle)) !== false) {
    $bytes[] = bin2hex($char);
}

echo implode(' ', $bytes), PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. chr(0xC3) and chr(0xA9) together make the UTF-8 letter é, which is two bytes long.
  2. fgetc() returns one byte per call, so the loop runs twice.
  3. bin2hex() turns each byte into hexadecimal so you can see it.
  4. implode() joins the two values, which shows that fgetc() splits the letter.

Frequently Asked Questions

It reads one character (one byte) from an open file and moves the file pointer forward. It returns false when the end of the file is reached.
Use while (($char = fgetc($handle)) !== false) and process each character inside the loop. For large files, fgets() or fread() are much faster.
Not well. It returns one byte at a time, so a multibyte character such as é or an emoji is split into several pieces. Read the whole line with fgets() and use mb_ functions instead.
Because the character "0" is falsy in PHP. A loop written as while ($char = fgetc($handle)) would stop early when it meets a zero in the file.
fgetc() reads one character, while fgets() reads a whole line. Use fgets() when you work with text lines.

Next: fgetcsv()

fgetcsv() reads one line from an open CSV file and splits it into an array of fields. It u.

fgetcsv() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful