Skip to main content

Display Random Testimonials in PHP

Lately I have been working on some new projects. I have a client right now that operates a service business and they would like to display testimonials on their site. For this application, I wrote a custom, simple solution to accomplish this with PHP. Here is a preview of the code:


<?php
/*
--------------------------------------------
Random Testimonial Generator Created by:
Ryan McCormick
Ntech Communications
Website: https://www.minnesotaithub.com/
Blog: https://www.minnesotaithub.com/blog/
Twitter: @ntechcomm
--------------------------------------------
*/

//Start Array
$testimonials = array();
$testimonials[0] = "Testimonial 1";
$testimonials[1] = "Testimonial 2";
$testimonials[2] = "Testimonial 3";
$testimonials[3] = "Testimonial 4";
//Automate script by counting all testimonials
$num = count($testimonials);
//randomize target testimonial
$target = rand(0, $num-1);
/*
To display testimonials on site
--------------------------------------------
place the following code in the
display area:
<?php echo $testimonials[$target]; ?>
--------------------------------------------
Use a PHP include to use this code on your
target page.
*/
?>

This code can also be used to display random text, html links, etc… Here is a link to the source file (Right-Click and Save-As to download):
PHP Source File

The example above shows a working/standalone application of the code as it will display HTML. The source file does not include the HTML shown at the bottom of the image.

Please comment with any questions or suggestions with the use or application of this code.

Comments

  1. joshua lowe

    This is a great little script, very simple but extremely effective. Thanks so much for posting it and saving everyone else a little work and time.

    Many Kudos sir!

    Joshua

Leave a Reply

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