PHP code example of webpatser / uuid

1. Go to this page and download the library: Download webpatser/uuid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webpatser / uuid example snippets


use Webpatser\Uuid\Uuid;

// Generate UUIDs
$uuid4 = Uuid::v4();                                    // Random (recommended for general use)
$uuid7 = Uuid::v7();                                    // Time-ordered (recommended for databases)
$uuid1 = Uuid::generate(1);                             // Time-based with MAC address
$uuid3 = Uuid::generate(3, 'example.com', Uuid::NS_DNS); // Name-based (MD5)
$uuid5 = Uuid::generate(5, 'example.com', Uuid::NS_DNS); // Name-based (SHA-1)
$uuid6 = Uuid::generate(6);                             // Reordered time-based
$uuid8 = Uuid::generate(8);                             // Custom/vendor-specific

echo (string) $uuid4; // e.g., "550e8400-e29b-41d4-a716-446655440000"

$uuid = Uuid::v7();

$uuid->string;       // "019d05e8-dfa1-7009-a8f7-4e1c868ccfa4"
$uuid->hex;          // "019d05e8dfa17009a8f74e1c868ccfa4"
$uuid->bytes;        // 16-byte binary string
$uuid->urn;          // "urn:uuid:019d05e8-dfa1-7009-a8f7-4e1c868ccfa4"
$uuid->version;      // 7
$uuid->variant;      // 1 (RFC 4122)
$uuid->time;         // 1773920640.936 (Unix timestamp, V1/V6/V7 only)
$uuid->node;         // MAC address hex (V1/V6 only, null for others)

// Import from SQL Server
$uuid = Uuid::importFromSqlServer('825B076B-44EC-E511-80DC-00155D0ABC54');

// Export to SQL Server format
$sqlGuid = $uuid->toSqlServer();          // String format
$sqlBin  = $uuid->toSqlServerBinary();    // 16-byte binary

$result = Uuid::benchmark(10000, 7);
// Returns: version, iterations, total_time_ms, avg_time_us, memory_used_bytes, uuids_per_second

Uuid::NS_DNS;   // 6ba7b810-9dad-11d1-80b4-00c04fd430c8
Uuid::NS_URL;   // 6ba7b811-9dad-11d1-80b4-00c04fd430c8
Uuid::NS_OID;   // 6ba7b812-9dad-11d1-80b4-00c04fd430c8
Uuid::NS_X500;  // 6ba7b814-9dad-11d1-80b4-00c04fd430c8
bash
php examples/benchmark.php 10000