Home > Backend Development > PHP Tutorial > PHP code to randomly output celebrity quotes_PHP tutorial

PHP code to randomly output celebrity quotes_PHP tutorial

WBOY
Release: 2016-07-21 15:15:54
Original
1142 people have browsed it

So how is this function of random celebrity quotes implemented?

In fact, it is very simple. You only need a string variable, which contains all the famous quotes to be displayed randomly. Then use the explode function to decompose it into an array, and then use rand random number to generate a value and output it in this array. a certain sentence.

Upload the code directly:
says.php

Copy the code The code is as follows:

< ?php
function random_str () {
$poems="The value of life is not measured by time, but by depth. - Leo Tolstoy
Three people walking together must I have a teacher. Choose what is good and follow it, and change what is bad. ——Confucius
Life is not a pleasure, but a very heavy work. ——Leo Tolstoy.
To be synonymous with excellence, many people do not need an environment of outstanding qualities. —— Steve Jobs
Live to change the world. Is there any other reason? —— Steve Jobs
Follow yourself. —— Steve Jobs
Life is unfair; you have to adapt to it —— Bill Gates
Always remind yourself to pay attention to happiness, just like looking at the sun on a cold day, your heart will not know. Feeling warm and bright. ——Bi Shumin
Happiness is like an ear that can listen to music. It needs constant training. ——Bi Shumin
The world does not care. Your self-esteem. The world expects you to achieve something before you can feel good about yourself. - Bill Gates
Life is empty and boring only to boring people - Chernyshev. "ski";
$poems=explode("n",$poems);
return $poems[rand(0,count($poems)-1)];
}
function says (){
$says=random_str();
echo $says;
}
?>

The key lies in these sentences:
Copy code The code is as follows:

$poems=explode("n",$poems);
return $poems[rand(0 ,count($poems)-1)];

If you are using the wordpress blog system, you can put the file says.php in the theme root directory, and then modify the theme root directory header.php, insert a statement to the front:
Copy the code The code is as follows:



Then insert the following statement where you want to display random famous quotes:
says();
That's it Called. I don’t know much about the wordpress system, so this method is definitely not the best way.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326061.htmlTechArticleSo how is this function of random celebrity quotes implemented? In fact, it is very simple. You only need a string variable, which contains all the famous quotes to be displayed randomly, and then use exp...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template