Hello World
看你能寫出幾種
<?phpecho "hello world";?><?phpecho 'hello world';?>ex-a3
<?phpprint "hello world";?>練習:請做出 99乘法表,需將乘法表用表格呈現
<!doctype html><html><head> <meta charset="utf-8"></head><body><table border="1"><?phpfor ($i=2;$i<=19;$i++){ echo "<tr>"; for ($j=3;$j<=15;$j++) { echo "<td>$i,$j,".$i*$j."</td>"; } echo "</tr>";}?></table></body>練習:網頁計數器(hit counter)
終極密碼
版本1:表單
<!doctype html><html><head> <meta charset="utf-8"></head><body><?php$answer=rand(2,98);$min =1;$max =99;?><form action="magic2.php" method="post"> 請在<?=$min?> 和 <?=$max?> 之間猜一數字 <input type="text" name="num" > <input type="hidden" name="answer" value="<?=$answer?>"> <input type="hidden" name="min" value="<?=$min?>"> <input type="hidden" name="max" value="<?=$max?>"> <input type="submit" name="ok" value="送出"></form></body></html>magic2<!doctype html><html><head> <meta charset="utf-8"></head><body><?php$num = $_POST["num"];$answer= $_POST["answer"];$min = $_POST["min"];$max = $_POST["max"];if ($num == $answer){?> 猜中<br/> <a href="magic1.php">繼續</a><?php}else{if ($num >$answer)$max=$num;else $min = $num;?><form action="magic2.php" method="post"> 請在<?=$min?> 和 <?=$max?> 之間猜一數字 <input type="text" name="num" > <input type="hidden" name="answer" value="<?=$answer?>"> <input type="hidden" name="min" value="<?=$min?>"> <input type="hidden" name="max" value="<?=$max?>"> <input type="submit" name="ok" value="送出"></form><?php}?></body></html>版本2:session
版本3:檔案
版本3:資料庫
顯示某個資料夾內的檔案
練習:將imges/底下的檔案 x01.jpg ~x10.jpg 改成abc001.jpg ~ abc.jpg
幾個圖片練習,在 images 資料夾,分別有x01.jpg ~x10.jpg
if ($file != "." && $file != "..") { $newfile= "abc0".substr($file,1); rename( __DIR__."\\images\\".$file,__DIR__. "\\images\\".$newfile); echo __DIR__."\\images\\".$file." 改成".__DIR__. "\\images\\".$newfile."<br>";}練習一:利用迴圈,將10張圖片顯示出來
利用練習一的檔案,只想顯示號碼是奇數(或偶數)的圖片
練習二:假設在 images 底下的圖片張數不清楚有幾張,請利用讀取檔案的方式,顯示全部的圖片
練習三:分別將 images 底下的10張圖片取其中2張,存成 base64的格式到陣列中,另外產生名稱為 1 和2 的連結,點1會將陣列0的圖片顯示,點2會將陣列0的圖片顯示(註:陣列由0開始),請勿連結到 imges 底下的圖片
這是圖片1的連結<a href='?img=1>1</a> 提示:在網址列 XXXXXX.php?img=1 ,在 php 內部 用$_GET['img'] 來取得變數的值
作業
1.用遞迴寫出兩個正整數的最大公因數(gcd)
gcd(144,128) 得到 ? gcd (742469137,283209877)得到?,參考網頁
2.分別用for 迴圈,while 迴圈及遞迴寫出階乘計算
並計算出10!=3628800
(進階題:如何算出大數階乘?譬如300!?,提示:因數值很大,無法用實際的乘積來計算,建議用for 迴圈+對數,指數)
3. 一元二次方程式 a*x^2+b*x+c=0(次方^符號是借用basic 的表示方式,無關php)
分別給 係數 a,b,c,請算出x 的解,如為虛數,請在虛數i表示
例 a=1,b=-3,c=2,得 x1=2,x2=1
4.費氏數列是很特別的數列,黃金比例,男星,自然界中的費氏數
請列出1000下內的費氏數列