============外部檔=================
< ?php
function ddcheck($m,$d,$y)
{
//判斷所輸入的資料是否是合法的日期資料
if(checkdate($m,$d,$y)){
//將輸入的資料轉成UNIX時間戳記
$dd=mktime(0,0,0,$m,$d,$y);
//以date()函數顯示對應格式
echo date("M-d-Y",$dd);
}else{
echo "所輸入的資料不是合法的日期格式!";
}
}
? >
==============PHP檔======================
< ?php
require "ddfunc.inc";
$mm=8;
$dd=20;
$yy=2005;
echo "< p>輸入的資料是 : < br/>";
echo $mm." , ".$dd." , ".$yy."< br/>";
//呼叫載入檔案中的函數
ddcheck($mm,$dd,$yy);
//改變$dd值
echo "< p>輸入的資料是 : < br/>";
$dd=34;
echo $mm." , ".$dd." , ".$yy."< br/>";
//呼叫載入檔案中的函數
ddcheck($mm,$dd,$yy);
? >
============產生結果================
輸入的資料是 :
8 , 20 , 2005
Aug-20-2005
輸入的資料是 :
8 , 34 , 2005
所輸入的資料不是合法的日期格式!
2008年9月3日 星期三
[PHP]include關鍵字呼叫-無外部檔會警告不會中斷
=============外部檔============
< ?php
function average($a,$b)
{
//先判斷輸入的資料是否是數值
if(is_numeric($a) && is_numeric($b)){
$avg=($a+$b)/2;
//傳回平均值
return $avg;
}else{
return "< font color='red'>所輸入的資料非數值 !< /font>";
}
}
function sum($a,$b)
{
//先判斷輸入的資料是否是數值
if(is_numeric($a) && is_numeric($b)){
$total=$a+$b;
//傳回總計
return $total;
}else{
return "< font color='red'>所輸入的資料非數值 !< /font>";
}
}
? >
=============PHP===================
< ?php
include "funs.inc";
$x=123;
$y=456;
echo "輸入的值為".$x.",".$y."< p>";
echo "平均值為".average($x,$y)."< p>";
echo "總和為".sum($x,$y)."< p>";
?>
< ?php
function average($a,$b)
{
//先判斷輸入的資料是否是數值
if(is_numeric($a) && is_numeric($b)){
$avg=($a+$b)/2;
//傳回平均值
return $avg;
}else{
return "< font color='red'>所輸入的資料非數值 !< /font>";
}
}
function sum($a,$b)
{
//先判斷輸入的資料是否是數值
if(is_numeric($a) && is_numeric($b)){
$total=$a+$b;
//傳回總計
return $total;
}else{
return "< font color='red'>所輸入的資料非數值 !< /font>";
}
}
? >
=============PHP===================
< ?php
include "funs.inc";
$x=123;
$y=456;
echo "輸入的值為".$x.",".$y."< p>";
echo "平均值為".average($x,$y)."< p>";
echo "總和為".sum($x,$y)."< p>";
?>
[PHP]不定名稱函數
======================HTML部份========================
< form name="form1" method="post" action="test18.php">
< p class="style1">請輸入一個數值< /p>
< p>
< input type="text" name="number">
< /p>
< p>
< input name="transform" type="radio" value="bin">
二進位
< input name="transform" type="radio" value="oct">
八進位
< input name="transform" type="radio" value="hex">
十六進位< /p>
< form name="form1" method="post" action="test18.php">
< p class="style1">請輸入一個數值< /p>
< p>
< input type="text" name="number">
< /p>
< p>
< input name="transform" type="radio" value="bin">
二進位
< input name="transform" type="radio" value="oct">
八進位
< input name="transform" type="radio" value="hex">
十六進位< /p>
< input type="submit" name="Submit" value="送出">
< input type="reset" name="Submit2" value="清除">
< /p>
< /form>
=====================PHP部份=======================
< ?php
//轉換成為二進位
function bin($n)
{
echo "二進位表示 : ".decbin($n);
}
//轉為八進位
function oct($n)
{
echo "八進位表示 : ".decoct($n);
}
//轉為十六進位
function hex($n)
{
echo "十六進位表示 : ".dechex($n);
}
//取得表單傳遞過來的資料
$nm=$_POST[number];
$choose=$_POST[transform];
echo "輸入的數值是 : ".$nm."< br/>";
echo "選取的模式是 : ".$choose."< p>";
//呼叫不定名稱函數
//依不同的變數值去呼叫不同的函數
$choose($nm);
?>
[PHP]不定參數函數
< ?php
//不定參數函數
function print_me()
{
$nm=func_num_args();
echo "此次共傳 ".$nm." 個參數。< br/>";
for($x=0;$x<$nm;$x++){
echo func_get_arg($x)."< br/>";
}
}
//呼叫函數,傳遞2參數
print_me("Linda","Peter");
echo "
//不定參數函數
function print_me()
{
$nm=func_num_args();
echo "此次共傳 ".$nm." 個參數。< br/>";
for($x=0;$x<$nm;$x++){
echo func_get_arg($x)."< br/>";
}
}
//呼叫函數,傳遞2參數
print_me("Linda","Peter");
echo "
";
//呼叫函數,傳遞3參數
print_me("Happy","Holiday","Elvin");
? >
========================顯示結果=======================
此次共傳 2 個參數。
Linda
Peter
此次共傳 3 個參數。
Happy
Holiday
Elvin
2008年9月2日 星期二
[PHP]利用巢狀函數轉換進位
===HTML部份語法===
< body >
< h2>請輸入一個整數:< /h2>
< form name="form1" method="post" action="test16.php">
< p>
< input type="text" name="number">
< /p>
< p>
< input type="submit" name="submit" value="送出">
< input type="reset" name="reset" value="reset" id="reset" >
< /p>
< /form>
< /body>
==================PHP部份============
< ?php
$n=$_POST[number];
function nest_transform($n)
{
function bin($n)
{
return decbin($n);
}
function oct($n)
{
return decoct($n);
}
function hex($n)
{
return dechex($n);
}
echo "< table border=2 width=80%>";
echo "< tr>二進位< /th>< td>".bin($n)."< /td>< /tr>";
echo "< tr>< th>八進位< /th>< td>".oct($n)."< /td>< /tr>";
echo "< tr>< th>十六進位< /th>< td>".hex($n)."< /td>< /tr>";
}
echo $n."< /br>";
nest_transform($n);
?>
< body >
< h2>請輸入一個整數:< /h2>
< form name="form1" method="post" action="test16.php">
< p>
< input type="text" name="number">
< /p>
< p>
< input type="submit" name="submit" value="送出">
< input type="reset" name="reset" value="reset" id="reset" >
< /p>
< /form>
< /body>
==================PHP部份============
< ?php
$n=$_POST[number];
function nest_transform($n)
{
function bin($n)
{
return decbin($n);
}
function oct($n)
{
return decoct($n);
}
function hex($n)
{
return dechex($n);
}
echo "< table border=2 width=80%>";
echo "< tr>
echo "< tr>< th>八進位< /th>< td>".oct($n)."< /td>< /tr>";
echo "< tr>< th>十六進位< /th>< td>".hex($n)."< /td>< /tr>";
}
echo $n."< /br>";
nest_transform($n);
?>
[PHP]遞迴呼叫-階層
< ?php
function factory($n)
{//判定是否為整數且大於零
if(is_integer($n) && $n>0){
if($n>=2){
$temp=$n ;
$n=$n-1;
//遞迴呼叫本身
$total=$temp * factory($n);
return $total;
}else{
return 1;
}
}else{
echo "請輸入大於零的整數";
}
}
//呼叫factory 函數
echo "5! = ".factory(5)."< br/>";
echo "6! = ".factory(6)."< br/>";
echo "7! = ".factory(7)."< br/>";
? >
function factory($n)
{//判定是否為整數且大於零
if(is_integer($n) && $n>0){
if($n>=2){
$temp=$n ;
$n=$n-1;
//遞迴呼叫本身
$total=$temp * factory($n);
return $total;
}else{
return 1;
}
}else{
echo "請輸入大於零的整數";
}
}
//呼叫factory 函數
echo "5! = ".factory(5)."< br/>";
echo "6! = ".factory(6)."< br/>";
echo "7! = ".factory(7)."< br/>";
? >
2008年8月26日 星期二
[PHP]傳遞整個陣列
< ?php
//原始陣列資料
$info=array(12,34,56,76,78,55,66,90,40,88);
function sorting($arr)
{//陣列遞減排序
arsort($arr);
//由陣列最後刪除5元素
for($x=0;$x<5;$x++){
array_pop($arr);
}
//傳回最大的5筆資料
return $arr;
}
echo "原始陣列資料:< br/>";
print_r($info);
echo "< p>";
//呼叫函數
$result=sorting($info);
//顯示回傳的陣列資料
echo "顯示回傳的陣列資料 : < br/>";
foreach($result as $value)
{
echo $value." -> ";
}
?>
=================顯示結果============
原始陣列
Array ( [0] => 12 [1] => 34 [2] => 56 [3] => 76 [4] => 78 [5] => 55 [6] => 66 [7] => 90 [8] => 40 [9] => 88 )
傳回值
90=>88=>78=>76=>66=>
//原始陣列資料
$info=array(12,34,56,76,78,55,66,90,40,88);
function sorting($arr)
{//陣列遞減排序
arsort($arr);
//由陣列最後刪除5元素
for($x=0;$x<5;$x++){
array_pop($arr);
}
//傳回最大的5筆資料
return $arr;
}
echo "原始陣列資料:< br/>";
print_r($info);
echo "< p>";
//呼叫函數
$result=sorting($info);
//顯示回傳的陣列資料
echo "顯示回傳的陣列資料 : < br/>";
foreach($result as $value)
{
echo $value." -> ";
}
?>
=================顯示結果============
原始陣列
Array ( [0] => 12 [1] => 34 [2] => 56 [3] => 76 [4] => 78 [5] => 55 [6] => 66 [7] => 90 [8] => 40 [9] => 88 )
傳回值
90=>88=>78=>76=>66=>
訂閱:
文章 (Atom)