試圖讓這個(gè)隨機(jī)問(wèn)候生成器工作。起初我沒(méi)有 $greet 變量的數(shù)組,但他們收到錯(cuò)誤消息說(shuō)我沒(méi)有定義變量?,F(xiàn)在我收到一個(gè)數(shù)組到字符串的轉(zhuǎn)換錯(cuò)誤。有什么想法嗎?<!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貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
您應(yīng)該使用array_rand()從數(shù)組中獲取隨機(jī)鍵。不是開(kāi)關(guān)/外殼。
看看下面的代碼?,F(xiàn)在$greet變量將用數(shù)組中的隨機(jī)問(wèn)候覆蓋自己。
<?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;
- 1 回答
- 0 關(guān)注
- 181 瀏覽
添加回答
舉報(bào)
0/150
提交
取消