試圖讓這個隨機問候生成器工作。起初我沒有 $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;
- 1 回答
- 0 關注
- 174 瀏覽
添加回答
舉報
0/150
提交
取消