socket_read

FunctionParams and return types changed in PHP 8.0

Reads a maximum of length bytes from a socket.

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

socket_read Function synopsis

socket_read(
    Socket $socket,
    int $length,
    int $mode = PHP_BINARY_READ
  ): string|false

Parameters

$socket

TypeSocket

A Socket instance created with socket_create or socket_accept.

$length

Typeint

The maximum number of bytes read is specified by the $length parameter. Otherwise you can use \r, \n, or \0 to end reading (depending on the $mode parameter, see below).

$mode

OptionalTypeintDefault valuePHP_BINARY_READ

Optional $mode parameter is a named constant:

Return value

Typestring|false

socket_read returns the data as a string on success, or false on error (including if the remote host has closed the connection). The error code can be retrieved with socket_last_error. This code may be passed to socket_strerror to get a textual representation of the error.

[`socket_read`](/codex/socket_read) returns a zero length string ("")
when there is no more data to read.

Changes to the socket_read Function

PHP 8.0

  • Return type added: string|false
  • Parameter type added for parameter #1 ($socket): Socket
  • Parameter type added for parameter #2 ($length): int
  • Parameter name of parameter #3 changed: $type to $mode
  • Parameter type added for parameter #3 ($mode): int
  • Parameter default value added for position #3 ($mode): PHP_BINARY_READ
  socket_read(
-     $socket,
+     Socket $socket,
-     $length,
+     int $length,
-     $type
+     int $mode = PHP_BINARY_READ
-   )
+   ): string|false

socket_read 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