|
<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技术:学php必收藏的几个经典代码第1/2页,转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。