Php randomly outputs the famous saying code

Source: Internet
Author: User
Tags wordpress blog

How is the function of this random celebrity famous saying realized?

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
Copy codeThe Code is as follows:
<? Php
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:
Copy codeThe 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:
Copy codeThe Code is as follows:
<? Php include (dirname (_ file _). "/says. php");?>

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.