亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

隨機問候生成器

隨機問候生成器

PHP
守著星空守著你 2023-05-26 16:59:06
試圖讓這個隨機問候生成器工作。起初我沒有 $greet 變量的數組,但他們收到錯誤消息說我沒有定義變量?,F在我收到一個數組到字符串的轉換錯誤。有什么想法嗎?<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>hello</title></head><body><?php//store random greetings$greet = array('Hello','Welcome','Greetings!','Salutatons!','Good day!', 'Yo!');switch($greet){case 1:   $greet = 'Hello!';   break;  case 2:   $greet = 'Welcome!';   break;case 3:   $greet = 'Greetings!';   break;case 4:   $greet = 'Salutations!';   break;case 5:   $greet = 'Good day!';   break;case 6:  $greet = 'Yo!';  break;}echo $greet;//set the seed for mtrand with the number of microseconds//since the last full second of the clockmt_srand((double)microtime() * 1000000);//computes a random integer 0-4$number=mt_rand(0,5);echo $number;?></body></html>
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

您應該使用array_rand()從數組中獲取隨機鍵。不是開關/外殼。


看看下面的代碼?,F在$greet變量將用數組中的隨機問候覆蓋自己。


<?php

//store random greetings


$greet = array('Hello','Welcome','Greetings!','Salutatons!','Good day!', 'Yo!');


$greet = $greet[array_rand($greet)];


echo $greet;


//set the seed for mtrand with the number of microseconds

//since the last full second of the clock


mt_srand((double)microtime() * 1000000);

//computes a random integer 0-4


$number=mt_rand(0,5);

echo $number;


查看完整回答
反對 回復 2023-05-26
  • 1 回答
  • 0 關注
  • 174 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號