Skip to content

Instantly share code, notes, and snippets.

@nethoncho
Last active November 8, 2019 00:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nethoncho/98e7e14e4629e3f46d2a to your computer and use it in GitHub Desktop.
Save nethoncho/98e7e14e4629e3f46d2a to your computer and use it in GitHub Desktop.
PHP: Find Network and Broadcast for given IP and Subnet
#!/usr/bin/php
<?php
$input = new stdClass();
$input->ip = '70.71.72.73';
$input->netmask = '255.255.255.0';
$input->ip_int = ip2long($input->ip);
$input->netmask_int = ip2long($input->netmask);
// Network is a logical AND between the address and netmask
$input->network_int = $input->ip_int & $input->netmask_int;
$input->network = long2ip($input->network_int);
// Broadcast is a logical OR between the address and the NOT netmask
$input->broadcast_int = $input->ip_int | (~ $input->netmask_int);
$input->broadcast = long2ip($input->broadcast_int);
print_r($input);
/*
stdClass Object
(
[ip] => 70.71.72.73
[netmask] => 255.255.255.0
[ip_int] => 1179076681
[netmask_int] => 4294967040
[network_int] => 1179076608
[network] => 70.71.72.0
[broadcast_int] => -3115890433
[broadcast] => 70.71.72.255
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment