# PHP7 random number

**URL:** <https://www.phphelp.com/t/php7-random-number/34012>\
**Category:** Code Snippets\
**Created:** [September 28, 2021, 9:10am UTC](https://www.phphelp.com/t/php7-random-number/34012 "2021-09-28T09:10:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![stowman](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@stowman](https://www.phphelp.com/u/stowman)\
**Post date:** [September 28, 2021, 9:10am UTC](https://www.phphelp.com/t/php7-random-number/34012/1 "2021-09-28T09:10:52Z")

</div>

I have the following snippet, using PHP7’s random\_bytes function:  
\<?php $bytes = bin2hex(random\_bytes(11)) ?\>  
\<?= $bytes ?\>  
It works and generates a 22-char string, changing to another string every time I refresh the page.  
But I have two problems:  
(1) a typical output is something like: df17b533def6ab919542a5 ?\>  
How do I get rid of the trailing " ?\>" which appears every time?  
(2) the string is of numbers and lowercase letters.  
I would prefer a mixture of uppercase, lowercase, and numbers.  
How can the code be changed to produce this?

---

<div class="post-metadata">

**Author:** ![whispers](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/858c86/32.png) [@whispers](https://www.phphelp.com/u/whispers)\
**Post date:** [September 28, 2021, 12:23pm UTC](https://www.phphelp.com/t/php7-random-number/34012/2 "2021-09-28T12:23:13Z")

</div>

1.) perhaps try terminating your line(s).

---

<div class="post-metadata">

**Author:** ![stowman](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@stowman](https://www.phphelp.com/u/stowman)\
**Post date:** [September 28, 2021, 6:18pm UTC](https://www.phphelp.com/t/php7-random-number/34012/3 "2021-09-28T18:18:35Z")

</div>

Thank you. I assume by “terminating your lines” you meant to put a “;” before the ?\>

Do you have any ideas for 2)??

---

<div class="post-metadata">

**Author:** ![whispers](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/858c86/32.png) [@whispers](https://www.phphelp.com/u/whispers)\
**Post date:** [September 28, 2021, 6:43pm UTC](https://www.phphelp.com/t/php7-random-number/34012/4 "2021-09-28T18:43:58Z")

</div>

Well not sure if the HEX value returned will EVERY have UPPERCASE letters..

So you would have to maybe maybe make your own functions and randomly select a few characters and make them upprecase.

Or perhaps generate your own:

```auto
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

function generate_string($input, $strength = 16) {
    $input_length = strlen($input);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        $random_character = $input[mt_rand(0, $input_length - 1)];
        $random_string .= $random_character;
    }

    return $random_string;
}

// Output: iNCHNGzByPjhApvn7XBD
echo generate_string($permitted_chars, 20);

// Output: Jp8iVNhZXhUdSlPi1sMNF7hOfmEWYl2UIMO9YqA4faJmS52iXdtlA3YyCfSlAbLYzjr0mzCWWQ7M8AgqDn2aumHoamsUtjZNhBfU

echo generate_string($permitted_chars, 100);

```

More info generating random/secure numbers/strings:

[https://code.tutsplus.com/tutorials/generate-random-alphanumeric-strings-in-php--cms-32132](https://code.tutsplus.com/tutorials/generate-random-alphanumeric-strings-in-php--cms-32132)

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [September 28, 2021, 7:40pm UTC](https://www.phphelp.com/t/php7-random-number/34012/5 "2021-09-28T19:40:39Z")

</div>

What is your use case for doing this?

---

<div class="post-metadata">

**Author:** ![stowman](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@stowman](https://www.phphelp.com/u/stowman)\
**Post date:** [September 28, 2021, 7:52pm UTC](https://www.phphelp.com/t/php7-random-number/34012/6 "2021-09-28T19:52:22Z")

</div>

My “use case” is this:  
I want to generate a “Unique ID number” without using MySQL (or any other RDBM):  
– uniqueness is desirable, but not absolutely essential  
– I found this code on StackOveflow:

\<?php $bytes = bin2hex(random\_bytes(11)) ?\> \<?= $bytes ?\>

which uses PHP7 (I am using PHP7.2) to generate a (fairly) random number, and I am relying on the 22-char length of the generated hex value to make it unlikely to create duplicate values for the few thousands of times I expect it to be used.  
(I could in future add code to check against an existing list of values and regenerate in case of duplication).  
I thought that a mixture of upper & lowercase letters (plus numbers) would look better and maybe give a wider range of values, but i’m not sure if that can be done without greatly complicating what is after all a very simple piece of code which works albeit without uppercase!!

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [September 28, 2021, 8:48pm UTC](https://www.phphelp.com/t/php7-random-number/34012/7 "2021-09-28T20:48:57Z")

</div>

The randomness is provided by `random_bytes`; `bin2hex` - or any other encoding function - is just representation. Using more characters won’t give you more randomness.

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [September 29, 2021, 4:42am UTC](https://www.phphelp.com/t/php7-random-number/34012/8 "2021-09-29T04:42:15Z")

</div>

You didn’t give me a use case. You just told me what you are trying to do which I already knew. What is this random number used for? Why are you trying to do this?

---

<div class="post-metadata">

**Author:** ![stowman](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@stowman](https://www.phphelp.com/u/stowman)\
**Post date:** [September 29, 2021, 5:56pm UTC](https://www.phphelp.com/t/php7-random-number/34012/9 "2021-09-29T17:56:34Z")

</div>

I have some form-to-email php code which sends an email to me when a visitor to my website clicks “Submit” having completed some form fields on my webpage.  
What I need is a unique id which can be generated at the time of “Submit”, which I can use to identify multiple submissions from each other.

---

<div class="post-metadata">

**Author:** ![whispers](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/858c86/32.png) [@whispers](https://www.phphelp.com/u/whispers)\
**Post date:** [September 29, 2021, 9:43pm UTC](https://www.phphelp.com/t/php7-random-number/34012/10 "2021-09-29T21:43:36Z")

</div>

Its not for security reasons/encryption… and just personal, internal tracking issues, then why bother?

or use the similar approach I posted above where you generate your OWN output with the lower and upper case characters.

---

<div class="post-metadata">

**Author:** ![stowman](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@stowman](https://www.phphelp.com/u/stowman)\
**Post date:** [September 30, 2021, 3:24pm UTC](https://www.phphelp.com/t/php7-random-number/34012/11 "2021-09-30T15:24:40Z")

</div>

I was hoping in my ignorance that someone would say “add xxxx” and you’d get what you want.  
Thanks for your help, but I was really trying to keep it simple!!  
Stay safe.
