PHP randomly outputs famous quotes

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, give you warm power.

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

<? PHP function random_str () {$ poems = "the value of life is measured not by time, but by depth. -- We must have our teacher, the other person of the people of the People's Republic of China, and the other person of the People's Republic of China. From the good, and from the bad. -- Confucius life is not a pleasure, but a very heavy job. -- Lev tolce has become synonymous with excellence. Many people do not need an environment of outstanding quality. -- Steve Jobs lives to change the world. Is there any other reason? -- Steve Jobs follow yourself. Follow your heart. -- Life for Steve Jobs is unfair; adapt to it. -- Bill Gates often remind himself to pay attention to happiness, just as he often looks at the Sun on cold days, and his heart is warm and bright. -- Bi Shumin happiness is a kind of trembling mind. Like listening to music, it requires constant training. -- Bi Shumin will not care about your self-esteem in this world. 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 yuncski "; $ poems = explode (" \ n ", $ poems); return $ poems [rand (0, count ($ poems)-1)];} function says () {$ says = random_str (); echo $ says ;}?>

The key lies in the following:

$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:

<?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.

(This article talks about blog original, http://tanteng.sinaapp.com/2012/10/php-rand)

 

 

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.