
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
file() function in PHP
The file() function reads a file into an array.
Syntax
file(file_path,flag,context)
Parameters
file − Path of the file.
flag − The optional parameter flags can be one, or more, of the following constants −
FILE_USE_INCLUDE_PATH − Search for the file in the include_path.
FILE_IGNORE_NEW_LINES − Do not add newline at the end of each array element.
FILE_SKIP_EMPTY_LINES − Skip empty lines.
FILE_TEXT − The content is returned in UTF-8 encoding. You can specify a different encoding by creating a custom context. This flag cannot be used with FILE_BINARY. This flag is only available since PHP 6.
FILE_BINARY − The content is read as binary data. This is the default setting and cannot be used with FILE_TEXT. This flag is only available since PHP 6.
context − It modifies the behaviour of the stream.
Return
The file() function returns the file in an array, whereas it returns false on failure.
Example
Let’s say we have a file “continents.txt” with the following content and lines.
The Earth has seven continents. The continents are: Asia, Africa, North America, South America, Antarctica, Europe, and Australia. Asia is the largest in area. Australia is the smallest in terms of area.
Example
<?php print_r(file("continents.txt")); ?>
Output
Array ( [0] => The Earth has seven continents. [1] => The continents are: Asia, Africa, North America, South America, Antarctica, Europe, and Australia. [2] => Asia is the largest in area. [3] => Australia is the smallest in terms of area. )
- Related Articles
- PHP file://
- filter_has_var() function in PHP
- filter_id() function in PHP
- filter_input() function in PHP
- filter_input_array() function in PHP
- filter_list() function in PHP
- filter_var_array() function in PHP
- filter_var() function in PHP
- constant() function in PHP
- define() function in PHP
- defined() function in PHP
- die() function in PHP
- eval() function in PHP
- exit() function in PHP
- get_browser() function in PHP
