PHP實現的英文名字全拼隨機排號指令碼_php執行個體

來源:互聯網
上載者:User
需求:
1、執行指令碼後,想去的同學輸入英文名字全拼,產生隨機數01-99之間的數字,
數字越大就去參加項目實踐,前面已經抓到的數字,下次不能在出現相同數字。
2、第一個輸入名字後,螢幕輸出資訊,並將名字和數字記錄到檔案裡,程式不能退出
繼續等待別的學生輸入。

實現代碼(請在命令列執行,而非WEB環境):

<?php// 號碼庫$num = range(1, 99);// 隨機打亂shuffle($num);$filename = './user.txt';// 開啟記錄檔案$handle = fopen($filename, 'w');// 排序後的使用者列表$user = array();while (true) { echo "\r\nEnter your name:"; $content = read(); // exit 退出指令碼 if ($content == 'exit') {  break; } // 取出隨機值 $n = array_pop($num); // 寫入檔案 fwrite($handle, $n.' '.$content."\r\n"); $user[$n] = $content; // 輸出到控制台 echo "Hi $content, your number is " . $n."\r\n";}// 關閉控制到輸入資料流fclose($GLOBALS['StdinPointer']);fwrite($handle, "\r\n");fwrite($handle, '----------------華麗的分隔線-----------------');fwrite($handle, "\r\n");ksort($user);foreach ($user as $k=>$v) { fwrite($handle, $k.' '.$v."\r\n");}// 關閉檔案fclose($handle);/*** 擷取命令列輸入值* @param string $length* @return string*/function read($length='255'){ if (!isset($GLOBALS['StdinPointer'])){  $GLOBALS['StdinPointer']=fopen("php://stdin","r"); } $line=fgets($GLOBALS['StdinPointer'],$length); return trim($line);}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.