經典循環例子
<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>