PHP random output feature sample code for famous sayings

Source: Internet
Author: User
Tags wordpress blog
As you can see in the "Small Talk blog" behind the title of the famous sayings, every refresh will randomly appear a statement, put your favorite words together, often appear in front of you, give you the strength of warmth

So how does this random celebrity quote function?

In fact, it's very simple, just need a string variable, which put all the famous sayings to be randomly displayed, then use the explode function to decompose the array, and then use the rand random number to generate a value, the output of one of the sentence.

Directly on the code:
says.php

The code is as follows:

<?php function Random_str () {$poems = "The value of life is not measured by time, but by depth." --Leo Tolstoy, I will be a teacher. Choose the good and the poor and change it. Confucius Life is not a kind of pleasure, but a very heavy work. Leo Tolstoy become synonymous with excellence, many people do not need an excellent quality of the environment. --steve Jobs is living to change the world, is there any other reason? --steve Jobs Follow yourself. Follow Your heart. --steve Jobs life is not fair, to get used to it. Bill Gates often reminds himself of happiness, like watching the sun in the cold days, the heart unconsciously warm, bright light. --Bi shumin happiness is a kind of psychic vibration. It needs to be constantly trained, just like the ear that listens to music. Bi shumin the world will not care about your self-esteem. The world expects you to achieve something before you feel good about yourself. Bill Gates ' life is empty and bland only in bland people. --"The snow of the car"; $poems =explode ("\ n", $poems); return $poems [Rand (0,count ($poems)-1)]; } function says () {$says =random_str (); Echo $says;}?>


The key lies in these sentences:

The code is as follows:

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

If you are using a 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:

The code is as follows:

<?php include (dirname (file). " /says.php ");?>

Then insert the following statement where you want to display the random celebrity quotes:
Says ();
This makes it possible to invoke the. The WordPress system is not very understanding, this method is certainly not the best way.

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.