How to generate random sentence consisted of random words with PHP

In this post I’m going to share a simple PHP script to generate random sentence consisted of random pre-defined words.

For example I want to show a text saying: “Hi, thank you for visiting my website” several times, but I don’t want repetitively saying exactly that sentence. Then the solution is this program, which can randomize some of words in this sentence so somehow it uniquely showing different sentences with same meaning.

To see the example please watch this video:

Here is the code:

<?php

$word1 = array("Hi", "Hello");
$word2 = array("thank you", "thanks", "thanks very much");
$word3 = array("visiting", "opening", "browsing");
$word4 = array("my website", "this website", "this blog");

function randomize($word){
	return $word[rand(0, count($word)-1)];
}

echo randomize($word1) . ", " . randomize($word2) . " for " . randomize($word3) . " " . randomize($word4) . ".";

?>
loading...

Leave a Reply

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