This is an example of how to display random text in PHP by creating an array of strings and using the rand()
function to randomly select an index from the array.
<?php
// Define an array of random texts:
$texts = [
"You are amazing!",
"Believe in yourself and all that you are.",
"The best time for new beginnings is now.",
"Life is short, enjoy every moment!"
];
// Get a random index from the array
$randomIndex = rand(0, count($texts) - 1);
// Display the random text
echo $texts[$randomIndex];
?>
rand(0, count($texts) - 1)
function generates a random index between 0
and the last index of the array (count($texts) - 1)
.$texts[$randomIndex]
.Every time the page is loaded, this script will display a different message randomly chosen from the $texts array.