PHP Beginner – Create a Random Text Generator

[ad_1]

Overview:

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

To create this solution, a database could be used. There is however another solution, it uses 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 in a string (a series of characters), and returns the length or how many characters that string contained. Here is how it works:

// my string
$ myString = "The long day is now over!";

// create a variable to store the length of the string
$ stringLength;

// call the function, and pass the string to it
$ stringLength = strlen ($ myString);

// send out 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).

Rand () Function

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

For Example:

rand (1,3);

This would generate one number each time, and it could be either 1, 2 or 3.

PHP Array

When using PHP, an array can be used to store any kind 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 stored text in the array. The Value is the actual text we plan to store. Here is how it looks.

$ myStorageArray [KEY] = VALUE;

The KEY is a whole number, like a 1, 200, or 342. The VALUE is a string (a string is a series of characters wrapped inside 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 it's VALUE to whatever text you want to store. The key is the location, and we will use it later to pull the data (VALUE) back out to print it to the screen.

Random Text Generator

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

Our task is then a simple matter of using the random number as a 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 gets the worm, or something else with dirt';
$ myQuotes [2] = 'that which does not kill me, hurts a lot';

// get a random number (0,1,2)
$ randomNumber = rand (0,2);

// output 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 contain a 0, 1, 2, and is used as a $ randomNumber variable and placed in the KEY of the $ myQuotes array. To make sure this will work, choose consecutive numbers – 1,2,3 or 4001, 4002, 4003, etc.

Every time the page is loaded, the echo statement sends one of the 3 quotes to the screen.

Conclusion

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

[ad_2]

Leave a Reply