PHP | SplFileObject current( ) Function
The SplFileObject::current() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to get current line of file.
Syntax:
string SplFileObject::current( void )
Parameters: This function does not accept any parameter.
Return values: Returns current line of the file.
Below Programs illustrate the SplFileObject::current() function in PHP.
Note: Program 1 has used gfg.txt file that contains following data.
GeeksforGeeks A Computer Science Portal for Geeks
<?php // Creating SplFile Object $file = new SplFileObject("gfg.txt"); foreach ($file as $gfg => $line) { echo $file->key() + 1 ." Line". ':> ' . $file->current(); } ?> |
Output:
1 Line: GeeksforGeeks 2 Line: A Computer Science 3 Line: Portal for Geeks
Program 2: Print all lines of current file.
<?php // Create SplFileObject object $file = new SplFileObject(__FILE__); // Print all characters of file while (false !== ($gfg = $file->fgetc())) { echo "$gfg"; } ?> |
1 Line:<?php
2 Line:
3 Line: // Creating SplFile Object
4 Line: $file = new SplFileObject(__FILE__);
5 Line:
6 Line: foreach ($file as $k => $line) {
7 Line: echo $file->key() + 1 ." Line".
8 Line: ': ' . $file->current();
9 Line: }
10 Line: ?>
Reference: http://php.net/manual/en/splfileobject.current.php
Recommended Posts:
- PHP | SplFileObject eof() Function
- PHP | SplFileObject setMaxLineLen() Function
- PHP | SplFileObject fgetc() Function
- PHP | SplFileObject fgets() Function
- PHP | SplFileObject fgetss() Function
- PHP | SplFileObject flock() Function
- PHP | SplFileObject fread() Function
- PHP | SplFileObject fputcsv() Function
- PHP | SplFileObject seek() Function
- PHP | SplFileObject rewind() Function
- PHP | SplFileObject fstat() Function
- PHP | SplFileObject ftell() Function
- PHP | SplFileObject getMaxLineLen() Function
- PHP | SplFileObject fwrite() Function
- PHP | SplFileObject ftruncate() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



