PHP net_get_interfaces() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, net_get_interfaces() lists the network interfaces on the local machine and returns details such as whether each one is up and which addresses it has.

It needs PHP 7.3 or newer. The result is an associative array keyed by interface name. Each value holds attributes like up, unicast addresses, and on Windows also description, mac, and mtu. On failure it returns false.

Syntax

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

PHP
$interfaces = net_get_interfaces();
CallWhat it does
net_get_interfaces()Returns an array of interfaces, or false on failure.
array_keys(net_get_interfaces())Lists interface names such as lo or Ethernet.
net_get_interfaces()['lo']['up'] ?? nullReads whether the loopback interface is up when that name exists.

There are no parameters. Interface names and extra keys differ between Linux, macOS, and Windows. Always check for false before you loop.

Library declarations
PHP
net_get_interfaces(): array|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayAn associative array of interfaces. Each entry can include up (bool), unicast (list of address info), and platform-specific fields.
falseThe system could not report interfaces; PHP may also raise a warning.

PHP Examples

Example 1 — List Interface Names

Call net_get_interfaces() and print the interface names. Your machine may show different names than this sample.

PHP
<?php
$interfaces = net_get_interfaces();

if ($interfaces === false) {
    echo 'Could not read interfaces', PHP_EOL;
    exit;
}

foreach (array_keys($interfaces) as $name) {
    echo $name, PHP_EOL;
}

How It Works

  1. net_get_interfaces() returns the full map or false.
  2. We exit early if the call failed.
  3. array_keys() gives only the interface names.
  4. The printed names are a typical Linux-style sample for this View Output demo.

Example 2 — Check Whether an Interface Is Up

Read the up flag for each interface and print a short status line.

PHP
<?php
$interfaces = net_get_interfaces();

foreach ($interfaces as $name => $info) {
    $state = !empty($info['up']) ? 'up' : 'down';
    echo $name, ': ', $state, PHP_EOL;
}

How It Works

  1. Each interface value is an associative array of attributes.
  2. The up key is a boolean status flag.
  3. empty() treats missing or false as down.
  4. Sample output shows mixed up and down states.

Example 3 — Print Unicast Addresses

Walk the unicast list and show address and netmask pairs for teaching.

PHP
<?php
$interfaces = net_get_interfaces();
$sample = $interfaces['lo'] ?? reset($interfaces);

foreach ($sample['unicast'] as $row) {
    echo $row['address'], ' / ', $row['netmask'], PHP_EOL;
}

How It Works

  1. Prefer the lo interface when it exists; otherwise take the first entry.
  2. unicast is an array of address records.
  3. Each record includes address and netmask strings.
  4. IPv4 and IPv6 can both appear in the same list.

Frequently Asked Questions

It returns information about the network adapters on the local computer, keyed by interface name.
PHP 7.3 or newer. Older versions do not have this function.
false, and PHP may emit a warning. Always check the result before using it as an array.
No. Linux often uses lo and eth0, while Windows uses longer adapter names. Write code that loops over whatever keys are present.
Inside each interface's unicast array. Look at keys such as address, netmask, family, and flags.

Next: openlog()

openlog() prepares a connection to the system logger and sets a short identity string that.

openlog() 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