Core\Uuid::toString
$uuid->toString(): stringReturns the text of a Uuid, such as f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4. The text is always 36 characters long: 32 hexadecimal digits in groups of 8, 4, 4, 4 and 12, joined by hyphens. The letters are always lower case, even when Core\Uuid::parse read them in upper case. echo $uuid writes the same text.
Because one UUID always gives one text, you can compare two texts from toString(), or use them as array keys. Two texts from different sources may differ only in case. After parse and toString() they are equal. Core\Uuid::parse does the opposite of toString().
The examples below print the text of a UUID, compare two UUIDs written in different case, and count orders per customer with the text as the key.
Return value
Returns string. A string of 36 characters. The letters are always lower case, even when parse read them in upper case.
Examples
Write a uuid as text
<?nvs// Shows how to get the text of a UUID.
var $id = Core\Uuid::parse('F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4');string $text = $id->toString();
// The text is always in lower case. This prints f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4.echo $text, "\n";
// The text is always 36 characters long. This prints 36.echo Core\Str::length($text), "\n";
// `echo` writes the same text when you give it the `Uuid` itself.echo $id, "\n";f9168c5e-ceb2-4faa-b6bf-329bf39fa1e436f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4Compare ids written in different case
<?nvs// Shows how to compare two UUIDs that were written in different case.
string $fromForm = 'f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4';string $fromFile = 'F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4';
// The two texts are not equal. This prints "different".if ($fromForm == $fromFile) { echo "same\n";} else { echo "different\n";}
// `toString()` gives the same text for the same UUID. This prints "same".string $a = Core\Uuid::parse($fromForm)->toString();string $b = Core\Uuid::parse($fromFile)->toString();if ($a == $b) { echo "same\n";} else { echo "different\n";}differentsameCount orders per customer
<?nvs// Shows how to use the text of a UUID as an array key, to count the orders of each customer.
array<string> $customerIds = [ '0190d1e6-8e3c-7c2a-9a4b-1f2e3d4c5b6a', 'F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4', '0190D1E6-8E3C-7C2A-9A4B-1F2E3D4C5B6A', 'f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4', '0190d1e6-8e3c-7c2a-9a4b-1f2e3d4c5b6a',];
// Some rows write the same customer in upper case.// `toString()` gives one text for each customer, so each customer gets one key.array<int> $orders = [];foreach ($customerIds as string $text) { string $key = Core\Uuid::parse($text)->toString(); // `??+=` starts a missing key at 0, then adds 1. $orders[$key] ??+= 1;}
// This prints 3 orders for the first customer and 2 for the second.foreach ($orders as string $key => int $count) { echo $key, ': ', $count, "\n";}0190d1e6-8e3c-7c2a-9a4b-1f2e3d4c5b6a: 3f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4: 2