Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

fgetc()

Example

Read a single character from the open file.

<?php
$file = fopen(“test.txt”,“r”);
echo fgetc($file);
fclose($file);
?>

Definition and Usage

The fgetc() function returns a single character from an open file.

 

Note: This function can be slow for large files. For large files, consider using fgets() to read data line by line and then process each line one character at a time with fgetc().

Syntax

fgetc(file)

Parameter Values

Parameter

Description

file

Mandatory. Specifies the open file from which to return a single character.

Technical Details

Return Value:

Returns a single character read from the file on success, or FALSE if end-of-file (EOF) is reached.

PHP Version:

4.0+

Binary Safe:

Yes

More Examples

Example

Read the open file one character at a time.

<?php
$file = fopen(“test.txt”,“r”);
while (! feof($file)) {
  echo fgetc($file);
  }
fclose($file);
?>