Master PHP’s fgetc(): Read Files and User Input with Simple Examples

This guide explains how to use PHP’s fgetc() function to read single characters from files and user input, covering file opening with fopen(), reading loops, handling end-of-file, and practical code examples for both file and STDIN scenarios.

php Courses
php Courses
php Courses
Master PHP’s fgetc(): Read Files and User Input with Simple Examples

In PHP, the fgetc() function reads a single character from an opened file and moves the file pointer forward. This article introduces its usage and provides clear examples.

Before calling fgetc(), you must open a file with fopen(). Example:

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

After successfully opening the file, you can read characters using fgetc(). The syntax is: fgetc($file) Here $file is the file resource pointer. The following example reads the entire file character by character and outputs each one:

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

The while loop calls fgetc() repeatedly; each call returns the next character and advances the pointer. When the end of the file is reached, fgetc() returns false, ending the loop.

Beyond file reading, fgetc() can read a single character from user input (STDIN). Example:

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";
        break;
}

In this snippet, fgetc() captures a character from the user, stores it in $input, and a switch statement performs actions based on the value.

In summary, fgetc() is a PHP function for reading a single character from a file or from user input. The provided examples demonstrate proper usage for both scenarios, helping developers handle file operations and interactive input 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.

PHPCode Examplesfile-handlingfgetcUser Input
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.