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

This article explains the PHP fgetc() function for reading a single character from an opened file or user input, demonstrates how to open files with fopen(), shows syntax and practical examples using loops and switch statements, and provides guidance for proper usage.

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

The fgetc() function in PHP reads one character from an opened file and moves the file pointer to the next character.

Before using fgetc(), you need to open a file with fopen(). Example:

$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 call fgetc($file) to read a character. The syntax is: fgetc($file) In the following example, a while loop reads each character until fgetc() returns false:

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

The loop returns a character on each iteration and moves the pointer forward; when all characters are read, fgetc() returns false and the loop ends.

Besides file reading, fgetc() can read a character from user input. The example below prompts the user, reads a character from STDIN, and uses a switch statement to act 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";
}

This example shows how fgetc() captures a single character from the user, stores it in $input, and then processes it with a switch statement.

In summary, the fgetc() function is a simple way to read one character from a file or from standard input in PHP, and the provided examples demonstrate its correct usage for both file handling and interactive input.

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.

BackendPHPfile-handlingcharacter inputfgetc
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.