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();
Call
What 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'] ?? null
Reads 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
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
array
An associative array of interfaces. Each entry can include up (bool), unicast (list of address info), and platform-specific fields.
false
The 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;
}
Output
lo
eth0
wlan0
How It Works
net_get_interfaces() returns the full map or false.
We exit early if the call failed.
array_keys() gives only the interface names.
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.