Skip to content

php - create uuid in php

If you need a kind of unique id creation inside php (but can not or do not want to use your database for this job), here is a small function for this. I have also implemented a md5 hash compare. You can replace your current md5 hash method with this one (if you are getting errors in your unittests for e.g. ;-) ).

?php $generatedUUIDs = array(); $generatedMd5s = array();

for($i = 0;$i<10000;$i++) { $generatedUUID = createUUID(); $generatedMd5 = md5($generatedUUID); if (inarray($generatedUUID, $generatedUUIDs)) { echo 'matching same uuid in run ' . $i . PHPEOL; exit(); } else { $generatedUUIDs[] = $generatedUUID; } if (inarray($generatedMd5, $generatedMd5s)) { echo 'matching same md5 in run ' . $i . PHPEOL; exit(); } else { $generatedMd5s[] = $generatedMd5; } } echo 'Run ' . $i . ' creations of UUIDs successfully.' . PHP_EOL;

function createUUID() { return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mtrand(0, 0xffff), mtrand(0, 0xffff), mtrand(0, 0xffff), mtrand(0, 0x0fff) | 0x4000, mtrand(0, 0x3fff) | 0x8000, mtrand(0, 0xffff), mtrand(0, 0xffff), mtrand(0, 0xffff)); }

Translate to de es fr it pt ja

callbacks, anonymous or lambda functions - the differences

It just took me a while to really understand the differences between this three. But hopefully (never say never ;-), now i got it. A callback simple describes, that a function awaits another function as parameter. This functions can be an existing one or a "on the fly created on" with the power of create_function.

A lambda function is a function without a function name. The reference is stored in a variable. You can use this variable to handle it over to methods like usort. As everybody i will use a sorting lambda function for showing what i mean.

$lengthOfStringTwo) { $sorter = 1; } elseif ($lengthOfStringOne < $lengthOfStringTwo) { $sorter = -1; } return $sorter; }; $names = array( 'Brian W. Kernighan', 'Dennis Ritchie' ); usort($names, $mySorter); echo xdebug_var_dump($names); ?>

It is also possible to define the $mySorter directly in unsort as second parameter like

usort($names, function($stringOne, $stringTwo { ... });
if you like that.

A closure is a function that surrounds (i do not want to use the word enclose ;-)) the lambda function.

That has the sideeffect that the closure itself conserves their own context. An easy example is a simple logger.

setMessage($message); $myDatabaseInfoLogTable->setTimestamp(mktime()); $status = $myDatabaseInfoLogTable->save(); break; case 'error': $myFileErrorLog = new ErrorLog(); $myFileErrorLog->addMessage(mktime() . "\t" . $message); $status = $myFileErrorLog->save(); break; } return $status; }; } $infoLogger = myClosureLogger('info'); $errorLogger = myClosureLogger('error'); $infoLogger('This is a info log'); $errorLogger('This is an error log'); ?>

The positiv thing about closures is, that the code footprint can be reduced (you even can use closures inside objects - even as static if you do not need to access on the object itself with $this). The negativ thing is, that you can not use interfaces which implies a lose of security.

Translate to de es fr it pt ja