php randomly outputs code for famous quotes

  • 2020-05-24 05:18:03
  • OfStack

So how does this random quote function work?

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

Direct access code:
says.php
 
<?php 
function random_str () { 
$poems=" The value of life is not measured by time, but by depth. -   Leo Tolstoy  
3 Walk, must have my teacher yan. Choose the good and follow it, the bad and change it. - Confucius  
 Life is not a 1 Kind of a pleasure, but 1 The pile 10 Heavy work. -   Leo Tolstoy  
 Be a yardstick of excellence. Many people don't need an environment of excellence. - Steve Jobs 
 We're here to change the world. What else is there to do? - Steve Jobs 
Follow yourself. Follow your heart. - Steve Jobs 
 Life is not fair; Get used to it. -- Bill Gates  
 Often remind yourself of happiness, just like in the cold days often look at the sun, the heart unconsciously warm, bright. - bi shumin  
 Happiness is 1 A vibration of the heart. It's like listening to music 1 You need constant training. - bi shumin  
 The world won't care about your self-esteem. The world will expect you to accomplish something before you feel good about yourself. -- Bill Gates  
 Life is empty and insipid only in the eyes of insipid people. -   chernyshevsky "; 
$poems=explode("\n",$poems); 
return $poems[rand(0,count($poems)-1)]; 
} 
function says(){ 
$says=random_str(); 
echo $says; 
} 
?> 

The key lies in these words:
 
$poems=explode("\n",$poems); 
return $poems[rand(0,count($poems)-1)]; 

If you are using the wordpress blogging system, you can put the file says.php in the theme root directory, then modify the header.php in the theme root directory, and insert a statement to the front:
 
<?php include(dirname(__file__)."/says.php"); ?> 

Then insert the following statement where you want to display a random quote from a famous person:
says();
So you can call it. Not familiar with the wordpress system, this is certainly not the best approach.

Related articles: