Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/mac-addresses/php$ _

Generate Random MAC Addresses in PHP

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: PHP

What: Generate random MAC addresses in PHP for network testing, device simulation, and development. Covers standard format, locally administered addresses, vendor prefixes, and validation.

Try it: Use our interactive Mac-addresses generator or integrate this code into your PHP application.

Generate random MAC addresses in PHP for network testing, device simulation, and development. Covers standard format, locally administered addresses, vendor prefixes, and validation. Looking for other languages? Check our code examples in JavaScript , Python , Java , C# , C++ , Ruby and Go or use our interactive web generator.

PHP Code Example

<?php
// Generate random MAC address (standard format)
function randomMacAddress() {
    $octets = [];
    for ($i = 0; $i < 6; $i++) {
        $octets[] = sprintf("%02X", mt_rand(0, 255));
    }
    return implode(":", $octets);
}

// Generate locally administered MAC address (bit 1 of first octet set)
function randomLocalMacAddress() {
    $octets = [];
    // First octet: set bit 1 (locally administered), clear bit 0 (unicast)
    $firstOctet = (mt_rand(0, 127) & 0xFE) | 0x02;
    $octets[] = sprintf("%02X", $firstOctet);

    for ($i = 1; $i < 6; $i++) {
        $octets[] = sprintf("%02X", mt_rand(0, 255));
    }
    return implode(":", $octets);
}

// Generate MAC with specific vendor prefix (OUI)
function randomMacWithVendor($vendorPrefix = "00:1A:2B") {
    $vendor = explode(":", $vendorPrefix);
    $octets = $vendor;

    for ($i = count($vendor); $i < 6; $i++) {
        $octets[] = sprintf("%02X", mt_rand(0, 255));
    }
    return implode(":", $octets);
}

// Generate MAC with different separator
function randomMacFormatted($separator = ":") {
    $octets = [];
    for ($i = 0; $i < 6; $i++) {
        $octets[] = sprintf("%02X", mt_rand(0, 255));
    }
    return implode($separator, $octets);
}

// Validate MAC address format
function isValidMacAddress($mac) {
    $patterns = [
        "/^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$/",  // 00:1A:2B:3C:4D:5E
        "/^([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}$/",  // 00-1A-2B-3C-4D-5E
        "/^[0-9A-Fa-f]{12}$/",                      // 001A2B3C4D5E
    ];

    foreach ($patterns as $pattern) {
        if (preg_match($pattern, $mac)) {
            return true;
        }
    }
    return false;
}

// Generate multiple MAC addresses
function generateMacAddresses($count = 10) {
    $macs = [];
    for ($i = 0; $i < $count; $i++) {
        $macs[] = randomMacAddress();
    }
    return $macs;
}

// Usage examples
echo "Random MAC: " . randomMacAddress() . "\n";
echo "Local MAC: " . randomLocalMacAddress() . "\n";
echo "Vendor MAC: " . randomMacWithVendor("00:1A:2B") . "\n";
echo "Dash format: " . randomMacFormatted("-") . "\n";
echo "No separator: " . randomMacFormatted("") . "\n";
echo "Is valid: " . (isValidMacAddress("00:1A:2B:3C:4D:5E") ? "true" : "false") . "\n";
print_r(generateMacAddresses(3));

[EXPLANATION]

This implementation uses mt_rand() for generating random octets (0-255) and sprintf("%02X") for hexadecimal formatting with zero-padding. Locally administered MAC addresses have bit 1 set in the first octet (0x02) and bit 0 cleared (unicast). Vendor prefixes (OUI - Organizationally Unique Identifier) are the first 3 octets. Common formats include colon-separated (00:1A:2B:3C:4D:5E), dash-separated (00-1A-2B-3C-4D-5E), and no separator (001A2B3C4D5E). Validation uses regex patterns for different formats.

Expected Output

Random MAC: A3:45:B7:89:CD:EF
Local MAC: 02:45:B7:89:CD:EF
Vendor MAC: 00:1A:2B:89:CD:EF
Dash format: A3-45-B7-89-CD-EF
No separator: A345B789CDEF
Is valid: true
Array
(
    [0] => 12:34:56:78:9A:BC
    [1] => DE:F0:12:34:56:78
    [2] => 9A:BC:DE:F0:12:34
)

Common Use Cases

  • Generate MAC addresses for virtual machine network configuration
  • Create test data for network device simulation and monitoring
  • Populate databases with network interface identifiers
  • Test MAC address validation and filtering in network applications
  • Generate locally administered MACs for Docker containers

Important Notes

  • Locally administered MACs (bit 1 set) avoid conflicts with real hardware
  • OUI (first 3 octets) identifies the manufacturer in real MACs
  • Multicast MACs have bit 0 set in first octet; avoid for most use cases
  • EUI-48 and MAC-48 are 48-bit identifiers (6 octets × 8 bits)
  • For production VMs, use locally administered range to avoid vendor conflicts

Try Our Interactive Generator

Don't want to write code? Use our free web-based Mac-addresses generator with instant results.

TRY MAC-ADDRESSES GENERATOR →