PHP stream_get_transports() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_get_transports() returns the names of the socket transports available in the current PHP build, such as tcp, udp, and unix.

Transports are the network protocols you can use with stream_socket_client(), stream_socket_server(), and related socket stream functions. The list depends on the operating system and how PHP was compiled.

Syntax

Call stream_get_transports() with the values you need. Start with this line.

PHP
$transports = stream_get_transports();
CallWhat it does
stream_get_transports()Returns an array of transport name strings.
in_array("tcp", stream_get_transports())Checks whether the TCP transport is available.
in_array("udp", stream_get_transports())Checks whether the UDP transport is available.

Tutorial demos check for common names instead of dumping the full array, so the printed output stays the same on most machines.

Library declarations
PHP
stream_get_transports(): array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayIndexed array of transport names (for example "tcp", "udp", "ssl", "unix").

PHP Examples

Example 1 — Check for TCP and UDP

Ask whether the two most common internet transports are registered.

PHP
<?php
$list = stream_get_transports();

echo "tcp: ", in_array("tcp", $list, true) ? "yes" : "no", PHP_EOL;
echo "udp: ", in_array("udp", $list, true) ? "yes" : "no", PHP_EOL;

How It Works

  1. stream_get_transports() returns the transport name list.
  2. in_array() looks for "tcp" and "udp".
  3. A normal PHP build includes both.
  4. Each line prints yes for this tutorial.

Example 2 — Count Transports Safely

Confirm that at least one transport exists without printing a machine-specific total.

PHP
<?php
$count = count(stream_get_transports());
echo "has transports: ", $count > 0 ? "yes" : "no", PHP_EOL;
echo "count >= 2: ", $count >= 2 ? "yes" : "no", PHP_EOL;

How It Works

  1. count() measures how many transports PHP reported.
  2. We only print whether the count is positive and whether it is at least two.
  3. That keeps the demo stable when builds differ slightly.
  4. Both checks succeed on a typical install.

Example 3 — Relate Transports to Socket URIs

Show how a transport name appears in a stream socket address string.

PHP
<?php
$transports = stream_get_transports();
$canTcp = in_array("tcp", $transports, true);

echo "tcp available: ", $canTcp ? "yes" : "no", PHP_EOL;
echo "example uri: tcp://127.0.0.1:8080", PHP_EOL;

How It Works

  1. We confirm the tcp transport is listed.
  2. Socket helpers expect a URI that starts with the transport name.
  3. tcp://127. 0.0.1:8080 is the usual shape for a local TCP target.
  4. No network call is made; this example only shows naming.

Frequently Asked Questions

It returns an array of socket transport names PHP can use, such as tcp, udp, ssl, or unix.
The address you pass begins with a transport, like tcp://example.com:80. That transport must appear in stream_get_transports().
Only when PHP was built with SSL support. Check with in_array("ssl", stream_get_transports()) before relying on it.
No. It only lists protocol names. Opening a connection is a separate step.
Transports are for socket streams (tcp, udp). Wrappers are for stream URLs like file://, http://, and php://, listed by stream_get_wrappers().

Next: stream_get_wrappers()

stream_get_wrappers() returns the names of the stream wrappers available right now, such a.

stream_get_wrappers() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful