PHP beginner: create a random text generator

General description:

When creating a website, it is often necessary to display dynamic text on the page. This text will change each time the page loads.

To create this solution, a database could be used. However, there is another solution, use PHP’s built-in rand() function and an array().

PHP-Functions

PHP has hundreds of functions to make programming easier and more efficient. A PHP function is a built-in piece of code that, when called, performs a specific ‘function’ or routine and then returns the result.

For example, the strlen() function takes a string (a series of characters) and returns the length or number of characters that string contained. Is that how it works:


$myString = “The long day is over!”;

// create a variable to store the length of the string

$length of string;

// call the function and pass it the string

$stringLength = strlen($myString);

// send the length of the string to the screen

echo $stringlength;

?>

The output would be ’24’, because there are 24 characters in that string (include the white space character).

random() function

PHP has a built in function that will generate a random number from a range. To use it, put a starting number at the location of the first parameter and the ending number at the location of the second parameter. This will create a range.

For example:


rand(1,3);

This would output a number each time, and it could be 1, 2, or 3.

PHP Matrix

When using PHP, an array can be used to store any type of data (strings, numbers, objects, arrays, booleans). We will use an array to store text (a string).

Arrays store information using a KEY – VALUE notation. The key is the location of our text stored in the array. The value is the actual text that we plan to store. This is how it looks.


$myStorageArray[KEY] =VALUE;

The KEY is an integer, such as 1, 200, or 342. The VALUE is a string (a string is a series of characters enclosed in single or double quotes). So the correct usage would look like this:


$myStorageArray[1] = ‘I am six feet tall’;

$myStorageArray[2] = ‘My hair color is brown’;

Do you see how it works? Put a unique number in the KEY and set its VALUE to whatever text you want to store. The key is the location, and we will use it later to retrieve the data (VALUE) and print it to the screen.

random text generator

Putting it all together, we’ll use rand() and array(). rand() will generate a dynamic number and the array will store our data which we will retrieve dynamically.

Our task is then a simple matter of using the random number as the KEY to see the VALUE of the array. Here is the full code.

// store my quotes

$myQuotes[0] = ‘he who works will achieve something – we hope!’;

$myQuotes[1] = ‘the early bird takes the worm, or something else with dirt’;

$myQuotes[2] = ‘what doesn’t kill me hurts a lot’;

// get a random number (0,1,2)

$Random number = rand(0,2);

// display the value of my array based on the random number

echo $myQuotes[$randomNumber];

?>

The $myQuotes array stores 3 of my favorite ‘modified’ quotes. The $randomNumber variable will hold a 0, 1, 2 and is used as a $randomNumber variable and is placed in the KEY of the $myQuotes array. To make sure this works, choose consecutive numbers: 1,2,3 or 4001, 4002, 4003, etc.

Each time the page loads, the echo statement sends one of 3 quotes to the screen.

Conclusion

Using just simple PHP tools, you can quickly create a dynamic text generator using just rand(), echo, and an array. A simple, yet very powerful PHP trick. The array can have as many KEY VALUES as you like, just remember to increase the size of the random number range to see all of its values.

Leave a Reply

Your email address will not be published. Required fields are marked *