PHP

Display a random text

You are amazing!

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

<?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];

?>

Explanation:

  • We define an array $texts with multiple string messages.
  • The rand(0, count($texts) - 1) function generates a random index between 0 and the last index of the array (count($texts) - 1).
  • We then echo the randomly selected text using $texts[$randomIndex].

Every time the page is loaded, this script will display a different message randomly chosen from the $texts array.