socket_write

FunctionParams and return types changed in PHP 8.0

Write to 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_write Function synopsis

socket_write(
    Socket $socket,
    string $data,
    ?int $length = null
  ): int|false

Parameters

$socket

TypeSocket

$data

Typestring

The buffer to be written.

$length

OptionalType?intDefault valuenull

The optional parameter $length can specify an alternate length of bytes written to the socket. If this length is greater than $data's length, it is silently truncated to the length of $data.

Return value

Typeint|false

Returns the number of bytes successfully written to the socket or false on failure. The error code can be retrieved with socket_last_error. This code may be passed to socket_strerror to get a textual explanation of the error.

It is perfectly valid for [`socket_write`](/codex/socket_write) to
return zero which means no bytes have been written. Be sure to use the
`===` operator to check for `false` in case of an
error.

Changes to the socket_write Function

PHP 8.0

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

socket_write 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