Php randomly outputs the famous saying Code _ php instance

Source: Internet
Author: User
Tags wordpress blog
Just as you can see the famous saying behind the title of "chat blog", every time you refresh it, a random statement will appear. put your favorite words together and it will often appear in front of you, how can we achieve this random celebrity feature with warm power?

In fact, it is very simple. we only need a string variable. Here we put all the famous quotes to be randomly displayed, and then use the explode function to break them into arrays, and then use the rand random number to generate a value, output a sentence in this array.

Directly run the code:
Says. php

The code is as follows:


Function random_str (){
$ Poems = "The value of life is measured not by time, but by depth. -- Lev tolistes
For the threesome, you must have a teacher. From the good, and from the bad. -- Confucius
Life is not a pleasure, but a very heavy job. -- Lev tolistes
Being synonymous with excellence, many people do not need an environment of outstanding quality. -- Steve Jobs
Living is to change the world. Is there any other reason? -- Steve Jobs
Follow yourself. Follow your heart. -- Steve Jobs
Life is unfair; adapt to it. -- Bill Gates
I often remind myself to pay attention to happiness, just as I often look at the sun on cold days, and my heart is warm and bright. -- Bi Shumin
Happiness is a kind of trembling mind. Like listening to music, it requires constant training. -- Bi Shumin
This world does not care about your self-esteem. This world expects you to achieve something before you feel good about yourself. -- Bill Gates
Life is empty and tasteless only in the eyes of dull people. -- Che yevski ";
$ Poems = explode ("\ n", $ poems );
Return $ poems [rand (0, count ($ poems)-1)];
}
Function says (){
$ Says = random_str ();
Echo $ says;
}
?>


The key lies in the following:

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 under the topic root directory, modify the header. php under the topic root directory, and insert a statement to the front:

The code is as follows:




Then insert the following statement at the location where you want to display the random quotes:
Says ();
In this way, you can call it. I am not very familiar with the wordpress system. this method is definitely not the best.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.