1

I was wondering if there is a way to make the following PHP function generate two different string instead of one?

function random_uuid( $length = 25 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $uuid = substr( str_shuffle( $chars ), 0, $length );
    return $uuid;
}

$uuid = random_uuid(25);
3
  • Yes, call the function once again maybe? Commented Dec 6, 2015 at 7:45
  • Yeah, I thought of that but I was hoping to make the function just generate two string instead of me calling it twice. Commented Dec 6, 2015 at 7:55
  • In PHP its best when function does exactly what it is supposed to do. So you already have this function which generates one string. Now you can put it into another function, which would call it twice and generate two strings Commented Dec 6, 2015 at 8:04

2 Answers 2

2

In PHP good practice is when function does exactly what it is supposed to do. So you already have one function:

function random_uuid( $length = 25 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $uuid = substr( str_shuffle( $chars ), 0, $length );
    return $uuid;
}

It perfectly does its job - generates random uuid. Now you can make another function, which will call this function twice:

function generate_two_strings($length) {
    return array(
        random_uuid($length),
        random_uuid($length)
    );
}

$result = generate_two_strings(15);

print_r($result);

Best practices:

  • Function does its one and only job
  • You split your code and keep functions small
Sign up to request clarification or add additional context in comments.

Comments

1
<?php
function random_uuid( $length = 25 ) {
$array=array();
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$uuid1 = substr( str_shuffle( $chars ), 0, $length );
    array_push($array,$uuid1);
$uuid2 = substr( str_shuffle( $chars ), 0, $length );
    array_push($array,$uuid2);
return $array;
}
$stuff = random_uuid();
var_dump($stuff);

Maybe?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.