Overview:
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, 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 a string (an array of characters) and returns the length or number of characters that string contained. Is that how it works:
// my string
$myString = “The long day is over!”;
// create a variable to store the length of the string
$string length;
// call the function and pass 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 whitespace 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 in the first parameter location and the ending number in the second parameter location. This will create a range.
For example:
rd(1,3);
This would output one number at a 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 stored text in the array. The value is the actual text 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’;
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 complete code.
// store my quotes
$myquotes[0] = ‘he who works, will achieve something – we hope!’;
$myquotes[1] = ‘the early bird takes away the worm, or something else with soil’;
$myquotes[2] = ‘what doesn’t kill me, hurts me 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 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 works, choose consecutive numbers: 1,2,3 or 4001, 4002, 4003, etc.
Every time the page loads, the echo statement sends one of the 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 but 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.