
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP Filesystem fgetc() Function
The PHP Filesystem fgetc() function is used to read one character from an open file. It takes a file pointer as an argument. The function can return a single character from an open file and gets a character from a given file pointer.
This function can return boolean false, and may also return non-boolean value that evaluates to false such as 0 or " ".
This function processes very large files very slowly, so it can not be used to process large files. If we need to read one character from a large file in sequence, use fgets() function to read one line of data in sequence, then use fgetc() function to process the line of data in sequence.
Syntax
Below is the syntax of the PHP Filesystem fgetc() function −
string fgetc ( resource $handle )
Parameters
The parameters are needed to use the fgetc() function are mentioned below −
Sr.No | Parameter & Description |
---|---|
1 |
handle(Required) This is a pointer to a FILE object that indicates the handle from which the character is read. |
Return Value
It returns String value on success, or FALSE on failure.
PHP Version
The fgetc() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.
Example
This PHP code shows us how we can open a file and read a single character from it using the PHP Filesystem fgetc() function. And after that close the file.
<?php $file = fopen("/PhpProject/sample.txt", "r"); echo fgetc($file); fclose($file); ?>
Output
This will produce the following result −
t
Example
This PHP code shows us how we can open a file and read each character until the end of the file using the while loop after that close the file.
<?php $file = fopen("/PhpProject/sample.txt", "r"); while(! feof($file)) { echo fgetc($file); } fclose($file); ?>
Output
This will create the below outcome −
tutorialspoint
Example
Since the fgetc() function returns a String value, or FALSE on failure so you can use conditional expressions to verify this. Check the sample of code below −
<?php // Open the file in read mode $file = fopen("example.txt", "r"); // Check if the file was opened successfully if ($file) { // Read characters until the end of the file while (($char = fgetc($file)) !== false) { echo $char; // Print each character } fclose($file); // Close the file } else { // Error message if file could not be opened echo "Could not open file."; } ?>
Output
This will generate the below result −
Hello World Tutorialspoint Information Security Web Browser
Example
This PHP code counts how many times a particular character or letter appears in a given file. So we have used two variables $charToCount and $count, initialized them with a and 0 respectively.
<?php // Open a file $file = fopen("/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt", "r"); // Open in read mode $charToCount = 'a'; $count = 0; if ($file) { while (!feof($file)) { if (fgetc($file) == $charToCount) { $count++; } } // Close the file fclose($file); echo "The character '$charToCount' appears $count times in the file."; } else { // Error message if file could not be opened echo "Could not open file."; } ?>
Output
This will generate the following outcome −
The character 'a' appears 22 times in the file.
Example
In this PHP example code we will try to read characters from a file and stop when a particular character is found. We have used $stopChar variable and initialized it with dot (.) for which we have to stop at.
<?php // Open text file in read mode $file = fopen("/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt", "r"); // Particular Character for which we have to stop at $stopChar = '.'; if ($file) { while (!feof($file)) { $char = fgetc($file); if ($char === $stopChar) { // Stop reading when the specific character is found break; } // Print each character echo $char; } // Close the file fclose($file); } else { // Error message if file could not be opened echo "Could not open file."; } ?>
Output
Here is the output of the above code −
Hello World!!!!! Today is 13 June 2024 and I am here to help you to learn PHP functions
Notes
- When the fgetc() method gets to the end of the file it will return false.
- Before calling fgetc(), always make sure that the file has been successfully opened. Also, use false correctly to avoid infinite loops or issues.
Summary
PHP's fgetc() function reads one character at a time from an open file. On failure, it returns a string or false. It is inefficient for large files; for better performance, read lines using fgets() and process them with fgetc().