Using PHP fgetc() to Read Characters from Files and Standard Input

This article explains how to use PHP's fgetc() function to read single characters from a file or standard input, covering file opening with fopen(), reading loops, handling end‑of‑file, and practical code examples for both file and user input scenarios.

php Courses
php Courses
php Courses
Using PHP fgetc() to Read Characters from Files and Standard Input

In PHP, many functions are available for file operations, and one of them is the fgetc() function, which reads a single character from an opened file and moves the pointer to the next character.

Before using fgetc(), you first need to open a file with fopen(). Below is an example of opening a file:

$file = fopen("example.txt", "r");
if ($file) {
    // File opened successfully
    // Perform other file operations
} else {
    echo "Unable to open file!";
}

After successfully opening the file, you can use fgetc() to read a character from the file. The syntax is: fgetc($file) Here $file is a pointer to the opened file resource. The following example demonstrates reading the file content character by character and outputting it:

$file = fopen("example.txt", "r");
if ($file) {
    while (($char = fgetc($file)) !== false) {
        echo $char;
    }
    fclose($file);
} else {
    echo "Unable to open file!";
}

In this example, a while loop reads each character using fgetc(). The function returns a character and moves the pointer forward; when all characters have been read, fgetc() returns false, ending the loop.

Besides reading characters from a file, fgetc() can also read a character from user input. The following code shows how to obtain a character from STDIN and act based on the input:

echo "Please enter a character: ";
$input = fgetc(STDIN);

switch ($input) {
    case 'a':
        echo "You entered the letter a";
        break;
    case 'b':
        echo "You entered the letter b";
        break;
    case 'c':
        echo "You entered the letter c";
        break;
    default:
        echo "Invalid character entered";
}

In this example, fgetc() reads a single character from the user, assigns it to $input, and a switch statement executes different actions based on the character.

In summary, the fgetc() function in PHP is used to read one character from a file or from user input. The examples above demonstrate correct usage of fgetc() for file reading and interactive character input, helping readers understand and apply this function effectively.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPfile-handlingcharacter inputfgetcstdin
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.