socket_connect

FunctionParams and return types changed in PHP 8.0

Initiates a connection on 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_connect Function synopsis

socket_connect(
    Socket $socket,
    string $address,
    ?int $port = null
  ): bool

Parameters

$socket

TypeSocket

A Socket instance created with socket_create.

$address

Typestring

The $address parameter is either an IPv4 address in dotted-quad notation (e.g. 127.0.0.1) if $socket is AF_INET, a valid IPv6 address (e.g. ::1) if IPv6 support is enabled and $socket is AF_INET6 or the pathname of a Unix domain socket, if the socket family is AF_UNIX.

$port

OptionalType?intDefault valuenull

The $port parameter is only used and is mandatory when connecting to an AF_INET or an AF_INET6 socket, and designates the port on the remote host to which a connection should be made.

Return value

Typebool

Returns true on success, 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.

If the socket is non-blocking then this function returns `false` with an
error `Operation now in progress`.

Changes to the socket_connect Function

PHP 8.0

  • Return type added: bool
  • Parameter type added for parameter #1 ($socket): Socket
  • Parameter name of parameter #2 changed: $addr to $address
  • Parameter type added for parameter #2 ($address): string
  • Parameter type added for parameter #3 ($port): ?int
  • Parameter default value added for position #3 ($port): null
  socket_connect(
-     $socket,
+     Socket $socket,
-     $addr,
+     string $address,
-     $port
+     ?int $port = null
-   )
+   ): bool

socket_connect 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