經典循環例子
for($counter = 1; $counter <= 6; $counter++) //循環6次
{
print("counter is $counter
n"); //打印6次
}
?>
for的高級運用
/*
** 打印必要的說明文字
*/
print("距離(lí)星期一(yī)還有幾天?n");
print("
-
n");
-
" . date("l", $currentDate) . "n");
}print("
for($currentDate = date("U"); //定義$currentDate時間格式
date("l", $currentDate) != "Monday"; //判斷是不是當前系統時間是Monday
$currentDate += (60 * 60 * 24)) //當前時間加上1天
{
/*
** 打印時間名稱
*/
print("
?>
函數的簡單調用:
function printBold($inputText) //定義function printBold()
{
print("" . $inputText . ""); ////打印$inputText
}
print("這行沒有加重!
n"); //直接打印字符串
printBold("這行加重了!!!"); //調用function printBold()函數
print("
n");
print("這行沒有加重!
n"); //直接打印字符串
?>
有返回值的函數
function makeBold($inputText) //定義function makeBold()函數
{
$boldedText = "";
$boldedText .= $inputText;
$boldedText .= "";
return($boldedText); //返回變量$boldedText
}
print("這行沒有加重!!!
n"); //直接打印字符串
print(makeBold("這行被加重了!!!") . "
n");//調用function makeBold()函數
print("這行沒有加重!!!
n"); //直接打印字符串
?>
有默認參數的函數
function printColored($Text, $Color="black") //定義function函數
{
print("$Text"); //獲取字符串的内容和顔色
}
printColored("這是黑顔色的字!"); //調用function函數
print("
n");
printColored("這是藍(lán)顔色的字!", "blue"); //調用function函數
print("
n");
?>
用的規算法判斷是否是整數
function checkInteger($Number)
{
if($Number > 1)
{
/* 整數減1仍然是整數 */
return(checkInteger($Number-1));
}
elseif($Number < 0)
{
/* 對于一(yī)個負數,*/
/* 可以分(fēn)析它的絕對值*/
return(checkInteger((-1)*$Number-1));//取絕對值,把負數按整數分(fēn)析
}
else
{
if(($Number > 0) AND ($Number < 1))
{
return("當然不是");
}
else
{
/* 0 和 1 是整數 */
/* 根據相關數學定義 */
return("是的");
}
}
}
print("0是整數嗎(ma)?" .
checkInteger(0) . "
n");
print("7是整數嗎(ma)? " .
checkInteger(7) . "
n");
print("3.5呢?" . checkInteger(3.5) . "
n");
print("那麽-5呢?" . checkInteger(-5) . "
n");
print("還有-9.2?" . checkInteger(-9.2) . "
n");
?>