From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Generating random numbers and strings

Generating random numbers and strings

From the course: PHP Tips, Tricks, and Techniques

Generating random numbers and strings

- [David] Hi, I'm David Powers, and welcome to this week's edition of PHP Tips, Tricks, and Techniques designed to help you become a smarter, more productive PHP developer. This time, I'm going to look at generating random numbers and strings. This has several practical uses. Random numbers, for example, can be used to display random images or quotes. Or if you're building a game, they can be used to simulate dealing cards or throwing dice. The main use for random strings is to generate unique tokens to identify a user. For example, when someone forgets their password, you can store the token with their account details in the database then send them a link to a password reset form with the token in a query string. If the token matches the one in the database, you allow the user to reset their password and then delete the token so it's single use only. PHP has several built-in functions to deal with these scenarios. For numbers, the original way of generating a random number was with…

Contents