PHP 8.5.0 Alpha 4 available for testing

Voting

: min(one, eight)?
(Example: nine)

The Note You're Voting On

Anonymous
20 years ago
I re-wrote the functions from jbothe at hotmail dot com as a little exercise in OO and added a couple of extra functions.

<?php

//--------------
// IPv4 class
class ipv4
{
var
$address;
var
$netbits;

//--------------
// Create new class
function ipv4($address,$netbits)
{
$this->address = $address;
$this->netbits = $netbits;
}

//--------------
// Return the IP address
function address() { return ($this->address); }

//--------------
// Return the netbits
function netbits() { return ($this->netbits); }

//--------------
// Return the netmask
function netmask()
{
return (
long2ip(ip2long("255.255.255.255")
<< (
32-$this->netbits)));
}

//--------------
// Return the network that the address sits in
function network()
{
return (
long2ip((ip2long($this->address))
& (
ip2long($this->netmask()))));
}

//--------------
// Return the broadcast that the address sits in
function broadcast()
{
return (
long2ip(ip2long($this->network())
| (~(
ip2long($this->netmask())))));
}

//--------------
// Return the inverse mask of the netmask
function inverse()
{
return (
long2ip(~(ip2long("255.255.255.255")
<< (
32-$this->netbits))));
}

}

$ip = new ipv4("192.168.2.1",24);
print
"Address: $ip->address()\n";
print
"Netbits: $ip->netbits()\n";
print
"Netmask: $ip->netmask()\n";
print
"Inverse: $ip->inverse()\n";
print
"Network: $ip->network()\n";
print
"Broadcast: $ip->broadcast()\n";
?>

<< Back to user notes page

To Top