//sample_functions.php
//6、寫壹個函數,求出三個數的最大值
function max3number($n1, $n2, $n3, $phpmax=false) {
$max = false;
if ($phpmax) {
//如果調用php的max,就直接調用
$max = max($n1, $n2, $n3);
} else {
//人工取得最大值
$max = $n1;
if ($n2 > $max) {
$max = $n2;
}
if ($n3 > $max) {
$max = $n3;
}
}
return $max;
}
//7、寫壹個函數打印出壹個m行*n列的表格。
function printtable($m, $n) {
$table = "<table border='1'>";
for ($i=1; $i<=$m; $i++) {
$table .= "<tr>";
for ($j=1; $j<=$n; $j++) {
$table .= "<td>m:$i, n:$j</td>";
}
$table .= "</tr>";
}
$table .= "</table>";
echo $table;
}
//9、有壹個數組$a=array(4,3,8,9,2),將其重新排序,按從小到大的順序排列
function sortarray(& $a, $phpsort=false) {
if ($phpsort) {
//如果調用php的排序,就直接調用
sort($a);
} else {
//人工排序:冒泡排序法
$j = 1; $temp = 0;
while($j < count($a)) {
for ($i=0; $i<count($a)-$j; $i++) {
if($a[$i] > $a[$i+1]) {
$temp = $a[$i];
$a[$i] = $a[$i+1];
$a[$i+1] = $temp;
}
}
$j++;
}
}
}
//TESTS:測試
$max = max3number(3.4, 1.2, 8.9);
echo "MAX: $max <br/>";
printtable(4, 8);
$a = array(4,3,8,9,2);
sortarray($a);
print_r($a);
><?php
//sample_form.php
//10、編程:通過表單獲取用戶的姓名、密碼、愛好、專業等信息
if ($_POST['submit']) {
echo "<h1>表單已經提交</h1>";
echo "姓名:{$_POST['name']}<br/>";
echo "密碼:{$_POST['password']}<br/>";
echo "愛好:{$_POST['aihao']}<br/>";
echo "專業:{$_POST['zhuanye']}<br/>";
}
><html>
<head>
<title>10、編程:通過表單獲取用戶的姓名、密碼、愛好、專業等信息</title>
</head>
<body>
<h1>請填寫以下信息:</h1>
<form method="post">
姓名:<input type="text" name="name"/><br/>
密碼:<input type="password" name="password"/><br/>
愛好:<input type="text" name="aihao"/><br/>
專業:<input type="text" name="zhuanye"/><br/>
<input type="submit" name="submit" value="提交"/>
</form>
</body>
</html>