经典循环例子
<HTML>
<HEAD>
<TITLE>经典循环例子</TITLE>
</HEAD>
<BODY>
<?
for($counter=1;$counter<=6;$counter++)//循环6次
{
print("<B>counteris$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>
<FONTSIZE=5>
<?
functionprintBold($inputText)//定义functionprintBold()
{
print("<B>".$inputText."</B>");////打印$inputText
}
print("这行没有加重!<BR>n");//直接打印字符串
printBold("这行加重了!!!");//调用functionprintBold()函数
print("<BR>n");
print("这行没有加重!<BR>n");//直接打印字符串
?>
</FONT>
</BODY>
</HTML>
有返回值的函数
<HTML>
<HEAD>
<TITLE>有返回值的函数</TITLE>
</HEAD>
<BODY>
<FONTSIZE=5>
<?
functionmakeBold($inputText)//定义functionmakeBold()函数
{
$boldedText="<B>";
$boldedText.=$inputText;
$boldedText.="</B>";
return($boldedText);//返回变量$boldedText
}
print("这行没有加重!!!<BR>n");//直接打印字符串
print(makeBold("这行被加重了!!!")."<BR>n");//调用functionmakeBold()函数
print("这行没有加重!!!<BR>n");//直接打印字符串
?>
</SIZE>
</BODY>
</HTML>
有默认参数的函数
<HTML>
<HEAD>
<TITLE>有默认参数的函数</TITLE>
</HEAD>
<BODY>
<FONTSIZE=5>
<?
functionprintColored($Text,$Color="black")//定义function函数
{
print("<FONTCOLOR="$Color">$Text</FONT>");//获取字符串的内容和颜色
}
printColored("这是黑颜色的字!");//调用function函数
print("<BR><BR>n");
printColored("这是蓝颜色的字!","blue");//调用function函数
print("<BR>n");
?>
</SIZE>
</BODY>
</HTML>
用的规算法判断是否是整数
<HTML>
<HEAD>
<TITLE>判断整数</TITLE>
</HEAD>
<BODY>
<?
functioncheckInteger($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>
<FONTSIZE=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>
当前1/2页12下一页阅读全文