
Years ago I wrote a simple random quote script in PHP to put on a website. I ran across it the other day and I thought I would share it with the world.
All you have to do is add or remove quotes from the PHP array to edit it. The tricky part is to make sure you put a backslash in front on any apostrophes or the script will crash.
Simple PHP random quote script
Here is my simple script and I will even throw in 39 quotes to get you started!
<?php
$quoteArray = array(
'"A journey of a thousand miles begins with a single step" - Laozi',
'"The more you sweat in training, the less you bleed in battle."',
'"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid."',
'"It is the greatest of all mistakes to do nothing because you can only do little. Do what you can." - Sydney Smith',
'"If you can solve your problem, then what is the need of worrying? If you cannot solve it, then what is the use of worrying?" - Shantideva',
'"The important thing is not to stop questioning. Curiosity has its own reason for existing." - Albert Einstein',
'"It is difficult to say what is impossible, for the dream of yesterday is the hope of today and the reality of tomorrow." - Robert H. Goddard',
'"A master in the art of living draws no sharp distinction between his work and his play." - L. P. Jacks ',
'"I cannot give you the formula for success, but I can give you the formula for failure - which is: Try to please everybody." - Herbert Bayard Swope',
'"Ninety-nine percent of the failures come from people who have the habit of making excuses" - George Washington Carver',
'"Show me a thoroughly satisfied man and I will show you a failure." - Thomas Edison',
'"All that a man achieves and all that he fails to achieve is the direct result of his own thoughts." - James Allen',
'"Doubt kills more dreams than failure ever will." - Suzy Kassem',
'"Thoughts and words are containers or weapons for carrying creative or destructive power." - Joyce Meyer',
'"Absorb what is useful, discard what is useless, and add what is essentially your own." - Bruce Lee',
'"Every day do something that will inch you closer to a better tomorrow." - Doug Firebaugh',
'"All great things are simple, and many can be expressed in single words: freedom, justice, honor, duty, mercy, hope." - Winston Churchill',
'"Darkness cannot drive out darkness; only light can do that. Hate cannot drive out hate; only love can do that." - Martin Luther King, Jr.',
'"Some people want it to happen, some wish it would happen, others make it happen." - Michael Jordan',
'"There is only one way to avoid criticism: do nothing, say nothing and be nothing." - Aristotle',
'"Talent is a pursued interest. Anything that you\'re willing to practice, you can do." - Bob Ross',
'"Great spirits have always encountered violent opposition from mediocre minds." - Albert Einstein',
'"The only true wisdom is in knowing you know nothing." - Socrates',
'"True wisdom comes to each of us when we realize how little we understand about life, ourselves, and the world around us." - Socrates',
'"We make a living by what we get. We make a life by what we give." - Winston S. Churchill', //This may not be Churchill.
'"Success always demands a greater effort." - Winston S. Churchill',
'"Don\'t cling to a mistake just because you spent a lot of time making it." - Unknown',
'"The master has failed more times than the beginner has even tried" – Stephen McCranie',
'"Never give up on a dream just because of the time it will take to accomplish it. The time will pass anyway." – Earl Nightingale',
'"A nation of well-informed men who have been taught to know and prize the rights which God has given them cannot be enslaved. It is in the region of ignorance that tyranny begins." – Benjamin Franklin',
'"Inspiration exists, but it has to find us working." – Pablo Picasso',
'"Those who never make mistakes work for those of us who do." - Henry Ford',
'"Pride is concerned with who is right. Humility is concerned with what is right." - Ezra Taft Benson',
'"Working hard for something we don\'t care about is called stress; working hard for something we love is called passion." - Simon Sinek',
'"The true test of a man\'s character is what he does when no one is watching." - John Wooden',
'"Not everything that can be counted counts, and not everything that counts can be counted." - 1963 paper by William Bruce Cameron',
'"Humility is not thinking less of yourself, it\'s thinking of yourself less." - CS Lewis',
'"It is better to be a warrior in a garden than to be a gardener in a war."',
'"A Grandmaster doesn\'t say he\'s a Grandmaster. Other people say it for him."',);
$total = count($quoteArray);
$random = (mt_rand()%$total);
$quote = "$quoteArray[$random]";
print("$quote");
?>
Now save the script somewhere on your website. After that, you just need to include where you want it on one of the pages. Now when you load it will add in a random quote. In this case, I saved the script as randomQuote.php into the root folder of the website.
<?php include("randomQuote.php"); ?>
Now the page will load and the script will pick a random quote.
Download
If you would rather just download the script file you can do that here.
Help me fix any errors.
I have used this script for years so code wise I do not think there are any errors. But with the quotes, I am not 100% sure. I attempted to make sure all the quotes here were right and attributed to the right person. If you see any errors please let me know so I can correct them!