file_get_contents

FunctionParams and return types changed in PHP 8.0

Reads entire file into a string.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

file_get_contents Function synopsis

file_get_contents(
    string $filename,
    bool $use_include_path = false,
    $context = null,
    int $offset = 0,
    ?int $length = null
  ): string|false

Parameters

$filename

Typestring

Name of the file to read.

$use_include_path

OptionalTypeboolDefault valuefalse

The FILE_USE_INCLUDE_PATH constant can be used to trigger includepath search. This is not possible if strict typing is enabled, since FILE_USE_INCLUDE_PATH is an int. Use true instead.

$context

OptionalDefault valuenull

A valid context resource created with stream_context_create. If you don't need to use a custom context, you can skip this parameter by null.

$offset

OptionalTypeintDefault value0

The offset where the reading starts on the original stream. Negative offsets count from the end of the stream.

$length

OptionalType?intDefault valuenull

Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.

Return value

Typestring|false

The function returns the read data or false on failure.

Changes to the file_get_contents Function

PHP 8.0

  • Return type added: string|false
  • Parameter type added for parameter #1 ($filename): string
  • Parameter name of parameter #2 changed: $flags to $use_include_path
  • Parameter type added for parameter #2 ($use_include_path): bool
  • Parameter default value added for position #2 ($use_include_path): false
  • Parameter default value added for position #3 ($context): null
  • Parameter type added for parameter #4 ($offset): int
  • Parameter default value added for position #4 ($offset): 0
  • Parameter name of parameter #5 changed: $maxlen to $length
  • Parameter type added for parameter #5 ($length): ?int
  • Parameter default value added for position #5 ($length): null
  file_get_contents(
-     $filename,
+     string $filename,
-     $flags,
+     bool $use_include_path = false,
-     $context,
+     $context = null,
-     $offset,
+     int $offset = 0,
-     $maxlen
+     ?int $length = null
-   )
+   ): string|false

file_get_contents Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes