|
<HTML>
<HEAD>
<TITLE>經(jīng)典循環(huán)例子</TITLE>
</HEAD>
<BODY>
<?
for($counter = 1; $counter <= 6; $counter++) //循環(huán)6次
{
print("<B>counter is $counter</B><BR>/n"); //打印6次
}
?>
</BODY>
</HTML>
for的高級(jí)運(yùn)用
<HTML>
<HEAD>
<TITLE>for的高級(jí)運(yùn)用</TITLE>
</HEAD>
<BODY>
<?
/*
** 打印必要的說明文字
*/
print("<B>距離星期一還有幾天?</B>/n");
print("<OL>/n");
for($currentDate = date("U"); //定義$currentDate時(shí)間格式
date("l", $currentDate) != "Monday"; //判斷是不是當(dāng)前系統(tǒng)時(shí)間是Monday
$currentDate += (60 * 60 * 24)) //當(dāng)前時(shí)間加上1天
{
/*
** 打印時(shí)間名稱
*/
print("<LI>" . date("l", $currentDate) . "/n");
}
print("</OL>/n");
?>
</BODY>
</HTML>
函數(shù)的簡(jiǎn)單調(diào)用:
<HTML>
<HEAD>
<TITLE>簡(jiǎn)單的函數(shù)</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function printBold($inputText) //定義function printBold()
{
print("<B>" . $inputText . "</B>"); ////打印$inputText
}
print("這行沒有加重!<BR>/n"); //直接打印字符串
printBold("這行加重了!!!"); //調(diào)用function printBold()函數(shù)
print("<BR>/n");
print("這行沒有加重!<BR>/n"); //直接打印字符串
?>
</FONT>
</BODY>
</HTML>
有返回值的函數(shù)
<HTML>
<HEAD>
<TITLE>有返回值的函數(shù)</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function makeBold($inputText) //定義function makeBold()函數(shù)
{
$boldedText = "<B>";
$boldedText .= $inputText;
$boldedText .= "</B>";
return($boldedText); //返回變量$boldedText
}
print("這行沒有加重!!!<BR>/n"); //直接打印字符串
print(makeBold("這行被加重了!!!") . "<BR>/n");//調(diào)用function makeBold()函數(shù)
print("這行沒有加重!!!<BR>/n"); //直接打印字符串
?>
</SIZE>
</BODY>
</HTML>
有默認(rèn)參數(shù)的函數(shù)
<HTML>
<HEAD>
<TITLE>有默認(rèn)參數(shù)的函數(shù)</TITLE>
</HEAD>
<BODY>
<FONT SIZE=5>
<?
function printColored($Text, $Color="black") //定義function函數(shù)
{
print("<FONT COLOR=/"$Color/">$Text</FONT>"); //獲取字符串的內(nèi)容和顏色
}
printColored("這是黑顏色的字!"); //調(diào)用function函數(shù)
print("<BR><BR>/n");
printColored("這是藍(lán)顏色的字!", "blue"); //調(diào)用function函數(shù)
print("<BR>/n");
?>
</SIZE>
</BODY>
</HTML>
用的規(guī)算法判斷是否是整數(shù)
<HTML>
<HEAD>
<TITLE>判斷整數(shù)</TITLE>
</HEAD>
<BODY>
<?
function checkInteger($Number)
{
if($Number > 1)
{
/* 整數(shù)減1仍然是整數(shù) */
return(checkInteger($Number-1));
}
elseif($Number < 0)
{
/* 對(duì)于一個(gè)負(fù)數(shù),*/
/* 可以分析它的絕對(duì)值*/
return(checkInteger((-1)*$Number-1));//取絕對(duì)值,把負(fù)數(shù)按整數(shù)分析
}
else
{
if(($Number > 0) AND ($Number < 1))
{
return("當(dāng)然不是");
}
else
{
/* 0 和 1 是整數(shù) */
/* 根據(jù)相關(guān)數(shù)學(xué)定義 */
return("是的");
}
}
}
print("<B>0是整數(shù)嗎?</B>" .
checkInteger(0) . "<BR>/n");
print("<B>7是整數(shù)嗎?</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>
初始化數(shù)組
<HTML>
<HEAD>
<TITLE>初始化數(shù)組</TITLE>
</HEAD>
<FONT SIZE=5>
<?
$monthName = array(1=>"January", "February", "March",//初始化一個(gè)數(shù)組
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
print(" 英語的“5月”是<B> $monthName[5] </B>。<BR>/n");//打印數(shù)組中的第6個(gè)元素
?>
</FONT>
</BODY>
</HTML>
獲取數(shù)組中的元素
<HTML>
<HEAD>
<TITLE>獲取數(shù)組中的元素</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"
);
/*打印相關(guān)的元素*/
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>
創(chuàng)建一個(gè)多維數(shù)組
<HTML>
<HEAD>
<TITLE>創(chuàng)建一個(gè)多維數(shù)組</TITLE>
</HEAD>
<?
$Cities = array( //二維數(shù)組array()
"華北地區(qū)"=>array(
"北京市",
"天津市",
"石家莊"
),
"西北地區(qū)"=>array(
"西安",
"拉薩"
)
);
print("華北地區(qū): ".$Cities["華北地區(qū)"][0]); //打印$Cities["華北地區(qū)"][0]
?>
</BODY>
</HTML>
php技術(shù):學(xué)php必收藏的幾個(gè)經(jīng)典代碼第1/2頁,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。