Backend Development 5 min read

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

This article explains PHP's fgetc() function for reading single characters from files or standard input, demonstrates opening files with fopen(), shows example code using while loops and switch statements, and provides practical usage tips for effective file and user input handling.

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

In PHP there are many functions for file operations, one of which is the fgetc() function. The fgetc() function reads a single character from an opened file and moves the pointer to the next character. This article introduces the usage of fgetc() and provides examples to help readers understand and use it.

Before using fgetc() , we first need to open a file. We can use the fopen() function to open a file. Below is a code example for opening a file:

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

After the file is successfully opened, we can use fgetc() to read a character from the file. The syntax of fgetc() is as follows:

<code>fgetc($file)</code>

Here, $file is a pointer to the opened file resource. The following example uses fgetc() to read the file contents and output them:

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

In the example above, a while loop reads each character in the file. Each iteration, fgetc() returns a character and moves the pointer forward. When all characters have been read, fgetc() returns false and the loop ends.

Besides reading characters from a file, fgetc() can also be used to read a character entered by the user. The following example demonstrates how to obtain a character from standard input with fgetc() and perform actions based on the input using a switch statement:

<code>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 "The character you entered is invalid";
}</code>

In this example, fgetc() reads a character from user input into the $input variable, and the switch statement executes different code paths depending on the entered character.

In summary, the fgetc() function is a PHP function for reading a single character from a file or from user input. The examples above show the correct usage of fgetc() and help readers better understand file operations and interactive character input.

phpcode examplesfile 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

login 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.