經典循環範例
<HTML>
<HEAD>
<TITLE>經典循環範例</TITLE>
</HEAD>
<BODY>
<?
for($counter = 1; $counter <= 6; $counter++) //迴圈6次
{
print("<B>counter is $counter</B><BR>n"); //列印6次
}
?>
</BODY>
</HTML>
for的進階運用
<HTML>
<HEAD>
<TITLE>for的高階運用</TITLE>
</HEAD>
<BODY>
<?
/*
** 列印必要的說明文字
*/
print("<B>距離星期一還有幾天?</B>n");
print("<OL>n");
for($currentDate = date("U"); //定義$currentDate時間格式
date("l", $currentDate) != "Monday"; //判斷是不是目前系統時間是Monday
$currentDate += (60 * 60 * 24)) //目前時間加上1天
{
/*
** 列印時間名稱
*/
print("<LI>" . date("l", $currentDate) . "n");
}
print("</OL>n");
?>
</BODY>
</HTML>
函數的簡單呼叫:
<HTML>
<HEAD>
<TITLE>簡單的函數</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function printBold($inputText) //定義function printBold()
{
print("<B>" . $inputText . "</B>"); ////印出$inputText
}
print("這行沒有加重!<BR>n"); //直接列印字串
printBold("這行加重了!!!"); //呼叫function printBold()函數
print("<BR>n");
print("這行沒有加重!<BR>n"); //直接列印字串
?>
</FONT>
</BODY>
</HTML>
有返回值的函數
<HTML>
<HEAD>
<TITLE>有傳回值的函數</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function makeBold($inputText) //定義function makeBold()函數
{
$boldedText = "<B>";
$boldedText .= $inputText;
$boldedText .= "</B>";
return($boldedText); //回傳變數$boldedText
}
print("這行沒有加重!!!<BR>n"); //直接列印字串
print(makeBold("這行加重了!!!") . "<BR>n");//呼叫function makeBold()函數
print("這行沒有加重!!!<BR>n"); //直接列印字串
?>
</SIZE>
</BODY>
</HTML>
有預設參數的函數
<HTML>
<HEAD>
<TITLE>有預設參數的函數</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function printColored($Text, $Color="black") //定義function函數
{
print("<FONT COLOR="$Color">$Text</FONT>"); //取得字串的內容和顏色
}
printColored("這是黑顏色的字!"); //呼叫function函數
print("<BR><BR>n");
printColored("這是藍色的字!", "blue"); //呼叫function函數
print("<BR>n");
?>
</SIZE>
</BODY>
</HTML>
用的規演算法判斷是否為整數
<HTML>
<HEAD>
<TITLE>判斷整數</TITLE>
</HEAD>
<BODY>
<?
function checkInteger($Number)
{
if($Number > 1)
{
/* 整數減1仍是整數*/
return(checkInteger($Number-1));
}
elseif($Number < 0)
{
/* 對於一個負數,*/
/* 可以分析它的絕對值*/
return(checkInteger((-1)*$Number-1));//取絕對值,把負數依整數分析
}
else
{
if(($Number > 0) AND ($Number < 1))
{
return("當然不是");
}
else
{
/* 0 和1 是整數 */
/* 根據相關數學定義*/
return("是的");
}
}
}
print("<B>0是整數嗎?</B>" .
checkInteger(0) . "<BR>n");
print("<B>7是整數嗎?</B> " .
checkInteger(7) . "<BR>n");
print("<B>3.5呢?</B>" . checkInteger(3.5) . "<BR>n");
print("<B>那麼-5呢?</B>" . checkInteger(-5) . "<BR>n");
print("<B>還有-9.2?</B>" . checkInteger(-9.2) . "<BR>n");
?>
</BODY>
</HTML>
初始化數組
<HTML>
<HEAD>
<TITLE>初始化陣列</TITLE>
</HEAD>
<FONT SIZE=5>
<?
$monthName = array(1=>"January", "February", "March",//初始化一個陣列
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
print(" 英文的「5月」是<B> $monthName[5] </B>。<BR>n");//列印陣列中的第6個元素
?>
</FONT>
</BODY>
</HTML>
取得數組中的元素
<HTML>
<HEAD>
<TITLE>取得陣列中的元素</TITLE>
</HEAD>
<?
$monthName = array(
/*定義$monthName[1]到$monthName[12]*/
1=>"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
/*定義$monthName["Jan"]到$monthName["Dec"]*/
"Jan"=>"January", "Feb"=>"February",
"Mar"=>"March", "Apr"=>"April",
"May"=>"May", "Jun"=>"June",
"Jul"=>"July", "Aug"=>"August",
"Sep"=>"September", "Oct"=>"October",
"Nov"=>"November", "Dec"=>"December",
/*定義$monthName["Jan"]到$monthName["Dec"]*/
"January"=>"January", "February"=>"February",
"March"=>"March", "April"=>"April",
"May"=>"May", "June"=>"June",
"July"=>"July", "August"=>"August",
"September"=>"September", "October"=>"October",
"November"=>"November", "December"=>"December"
);
/*列印相關的元素*/
print("Month <B>5</B> is <B>" . $monthName[5]. "</B><BR>n");
print("Month <B>Aug</B> is <B>" . $monthName["Aug"] . "</B><BR>n");
print("Month <B>June</B> is <B>" . $monthName["June"] . "</B><BR>n");
?>
</BODY>
</HTML>
建立一個多維數組
<HTML>
<HEAD>
<TITLE>建立一個多維數組</TITLE>
</HEAD>
<?
$Cities = array( //二維數組array()
"華北地區"=>array(
"北京市",
"天津市",
"石家莊"
),
"西北地區"=>array(
"西安",
"拉薩"
)
);
print("華北地區: ".$Cities["華北地區"][0]); //印出$Cities["華北地區"][0]
?>
</BODY>
</HTML>
PHP 4.0實作表格狀列印
<HTML>
<HEAD>
<TITLE>實作表格狀列印</TITLE>
</HEAD>
<BODY>
<?
/*
** 資料表格化
*/
print("<TABLE bgcolor='ffccoo' BORDER="1">n"); // 表格開始
for($Row=1; $Row <= 12; $Row ++)
{
print("<TR>n"); // 開始行
// do each column
for($Column=1; $Column <= 12; $Column ++)
{
print("<TD>");//開始列
print($Row * $Column);//表格元素乘積
print("</TD>");
}
print("</TR>n"); // 行結束
}
print("</TABLE>n"); // 表格結束
?>
</BODY>
</HTML>
查看系統的一些變數
<HTML>
<HEAD>
<TITLE>查看PHP的環境變數</TITLE>
</HEAD>
<BODY>
<?
print("你正在用文件的名字為: ");
print(__FILE__);
print(" <BR>n");
print("<hr>");
print("你的作業系統為: ");
print(PHP_OS);
print("<hr>");
print("你的php的版本是: ");
print(PHP_VERSION)
?>
</BODY>
</HTML>
開啟本機或遠端檔案
<HTML>
<HEAD>
<TITLE>開啟本機或遠端檔案</TITLE>
</HEAD>
<BODY>
<?
print("<H3>透過http協定開啟檔案</H3>n");
// 透過http 協定開啟文件
if(!($myFile = fopen("d:web/web/php/test/data.txt", "r")))
{
print("檔案不能開啟");
exit;
}
while(!feof($myFile)) //循環
{
// 依行讀取檔案中的內容
$myLine = fgetss($myFile, 255);
print("$myLine <BR>n");
}
// 關閉檔案的句柄
fclose($myFile);
?>
</BODY>
</HTML>
開啟檔案的幾種方式比較
<HTML>
<HEAD>
<TITLE>讀取文件內容</TITLE>
</HEAD>
<BODY>
<?
// 開啟檔案同時列印檔案的每個字符
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myCharacter = fgetc($myFile);
print($myCharacter);
}
fclose($myFile);
}
?>
<? print("<hr>");?>
<?
// 開啟檔案同時列印檔案的每一行
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgets($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>
<? print("<hr>");?>
<?
/* 開啟檔案同時列印文件的每一行,
同時去掉取回字串中的HTML 語言
*/
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgetss($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>
</BODY>
</HTML>
存取檔案常見屬性
<HTML>
<HEAD>
<TITLE>存取檔案常見屬性</TITLE>
</HEAD>
<BODY>
<BR>
<?
print("檔案的擁有者(UID 值):");
print(fileowner("data.txt")."<br>");
print("檔案的大小:");
print(filesize("data.txt")."<br>");
print("文件的類型:");
print(filetype("data.txt")."<br>");
?>
</BODY>
</HTML>
呼叫文字檔內容
<HTML>
<HEAD>
<TITLE>呼叫文字檔內容</TITLE>
</HEAD>
<BODY>
<CENTER>
<?
// 開啟檔案同時,列印每一行
$myFile = file( "data.txt");
for($index = 0; $index < count($myFile); $index++)
{
print($myFile[$index]."<BR>");
}
?>
</CENTER>
</BODY>
</HTML>
建立目錄函數
<HTML>
<HEAD>
<TITLE>建立目錄函數</TITLE>
</HEAD>
<BODY>
<?
if(mkdir("myDir1", 0777)) //建立目錄的函數
{
print("目錄建立成功"); //目錄建立成功
}
else
{
print("目錄建立失敗!"); //目錄建立失敗
}
?>
</BODY>
</HTML>
瀏覽目錄
<HTML>
<HEAD>
<TITLE>瀏覽目錄</TITLE>
</HEAD>
<BODY>
<?
// 使用表格瀏覽目錄的結構
print("<TABLE BORDER="1">n");
// 建立表格的頭
print("<TR><font color='red'>n");
print("<TH>檔名</TH>n");
print("<TH>檔案的大小</TH>n");
print("</font></TR>n");
$myDirectory = opendir("."); // 建立操作目錄的句柄
// 讀出目錄中的每一個子項
while($entryName = readdir($myDirectory))
{
print("<TR>");
print("<TD>$entryName</TD>");
print("<TD ALIGN="right">");
print(filesize($entryName));
print("</TD>");
print("</TR>n");
}
closedir($myDirectory); // 關閉目錄
print("</TABLE>n");
?>
</BODY>
</HTML>
PHP相關資訊
<HTML>
<HEAD>
<TITLE>PHP相關資訊</TITLE>
</HEAD>
<BODY>
<?
phpinfo();
?>
</BODY>
</HTML>
常用的數值判斷函數
<HTML>
<HEAD>
<TITLE>常用的數值判斷函數</TITLE>
</HEAD>
<BODY>
<?
//判斷數組
$colors = array("red", "blue", "green");
if(is_array($colors))
{
print("colors is an array"."<br>");
}
//雙精度數判斷
$Temperature = 15.23;
if(is_double($Temperature))
{
print("Temperature is a double"."<br>");
}
//整數判斷
$PageCount = 2234;
if(is_integer($PageCount))
{
print("$PageCount is an integer"."<br>");
}
//對象判斷
class widget
{
var $name;
var $length;
}
$thing = new widget;
if(is_object($thing))
{
print("thing is an object"."<br>");
}
//字符判斷
$Greeting = "Hello";
if(is_string($Greeting))
{
print("Greeting is a string"."<br>");
}
?>
</BODY>
</HTML>
文件上傳介面
<HTML>
<HEAD>
<TITLE>檔案上傳介面</TITLE>
</HEAD>
<BODY><TABLE><CENTER>
<?
if($UploadAction){
$UploadAction=0;
$TimeLimit=60;
/*設定超時限制時間預設時間為30s,設定為0時為不限時*/
set_time_limit($TimeLimit);
If(($Upfile != "none")&&
($Upfile != ""))
{
$Filepath="d:webwebphptest"; //上載檔案存放路徑
$FileName=$Filepath.$Upfile_name;
if($Upfile_size <1024) //上載檔案大小
{$FileSize = (string)$Upfile_size . "位元組";}
elseif($Upfile_size <(1024 * 1024))
{
$FileSize = number_format((double)($Upfile_size / 1024), 1) . " KB";
}
else
{
$FileSize = number_format((double)($Upfile_size/(1024*1024)),1)."MB";
}
if(!file_exists($FileName))
{
if(copy($Upfile,$FileName))
{unlink($Upfile);
echo "<br><br>n";
echo "檔案$Upfile_name 已上載成功!";
echo "<br><br>n";
echo "檔案位置:$FileName";
echo "<br><br>n";
echo "檔案大小:$FileSize";
echo "<br><br>n";
}
else
{echo "檔案$Upfile_name上載失敗!"; }
}
else
{echo "檔案$Upfile_name已經存在!"; }
}
else
{echo "你沒有選擇任何檔案上載!"; }
set_time_limit(30); //恢復預設逾時設定
}
?>
<FORM ENCTYPE = "multipart/form-data" NAME = "SubmitForm"
ACTION = "default.php" METHOD = "POST">
<INPUT TYPE = "hidden" NAME = "MAX_FILE_SIZE" VALUE ="1000000">
<INPUT TYPE = "hidden" NAME = "UploadAction" VALUE = "1">
<TR><TD><INPUT NAME = "Upfile" TYPE = "file" SIZE = "30"></TD>
</TR><TR><TD><INPUT NAME = "submit" VALUE = "投稿" TYPE = "submit">
<INPUT NAME = "reset" VALUE = "重設" TYPE = "reset"></TD>
</TR></FORM></CENTER></TABLE>
</BODY>
</HTML>