来自:http://hi.baidu.com/dckhello/item/d62b16d8994bf93449e1ddb0
经典循环例子
<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 4.0实现表格状打印
<HTML>
<HEAD>
<TITLE>实现表格状打印</TITLE>
</HEAD>
<BODY>
<?
/*
** 数据表格化
*/
print("<TABLE bgcolor='ffccoo' BORDER=\"1\">\n"); // 表格开始
for($Row=1; $Row <= 12; $Row ++)
{
print("<TR>\n"); // 开始行
// do each column
for($Column=1; $Column <= 12; $Column ++)
{
print("<TD>");//开始列
print($Row * $Column);//表格元素乘积
print("</TD>");
}
print("</TR>\n"); // 行结束
}
print("</TABLE>\n"); // 表格结束
?>
</BODY>
</HTML>
查看系统的一些变量
<HTML>
<HEAD>
<TITLE>查看PHP的环境变量</TITLE>
</HEAD>
<BODY>
<?
print("你正在用文件的名字为: ");
print(__FILE__);
print(" <BR>\n");
print("<hr>");
print("你的操作系统为: ");
print(PHP_OS);
print("<hr>");
print("你的php的版本为: ");
print(PHP_VERSION)
?>
</BODY>
</HTML>
打开本地或者远程文件
<HTML>
<HEAD>
<TITLE>打开本地或者远程文件</TITLE>
</HEAD>
<BODY>
<?
print("<H3>通过http协议打开文件</H3>\n");
// 通过 http 协议打开文件
if(!($myFile = fopen("d:web/web/php/test/data.txt", "r")))
{
print("文件不能打开");
exit;
}
while(!feof($myFile)) //循环
{
// 按行读取文件中的内容
$myLine = fgetss($myFile, 255);
print("$myLine <BR>\n");
}
// 关闭文件的句柄
fclose($myFile);
?>
</BODY>
</HTML>
打开文件的几种方式比较
<HTML>
<HEAD>
<TITLE>读取文件内容</TITLE>
</HEAD>
<BODY>
<?
// 打开文件同时打印文件的每一个字符
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myCharacter = fgetc($myFile);
print($myCharacter);
}
fclose($myFile);
}
?>
<? print("<hr>");?>
<?
// 打开文件同时打印文件的每一行
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgets($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>
<? print("<hr>");?>
<?
/* 打开文件同时打印文件的每一行,
同时去掉取回字符串中的 HTML 语言
*/
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgetss($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>
</BODY>
</HTML>
访问文件常见属性
<HTML>
<HEAD>
<TITLE>访问文件常见属性</TITLE>
</HEAD>
<BODY>
<BR>
<?
print("文件的所有者(UID 值):");
print(fileowner("data.txt")."<br>");
print("文件的大小:");
print(filesize("data.txt")."<br>");
print("文件的类型:");
print(filetype("data.txt")."<br>");
?>
</BODY>
</HTML>
调用文本文件内容
<HTML>
<HEAD>
<TITLE>调用文本文件内容</TITLE>
</HEAD>
<BODY>
<CENTER>
<?
// 打开文件同时,打印每一行
$myFile = file( "data.txt");
for($index = 0; $index < count($myFile); $index++)
{
print($myFile[$index]."<BR>");
}
?>
</CENTER>
</BODY>
</HTML>
创建目录函数
<HTML>
<HEAD>
<TITLE>创建目录函数</TITLE>
</HEAD>
<BODY>
<?
if(mkdir("myDir1", 0777)) //创建目录的函数
{
print("目录创建成功"); //目录建立成功
}
else
{
print("目录建立失败!"); //目录建立失败
}
?>
</BODY>
</HTML>
浏览目录
<HTML>
<HEAD>
<TITLE>浏览目录</TITLE>
</HEAD>
<BODY>
<?
// 使用表格浏览目录的结构
print("<TABLE BORDER=\"1\">\n");
// 创建表格的头
print("<TR><font color='red'>\n");
print("<TH>文件名</TH>\n");
print("<TH>文件的大小</TH>\n");
print("</font></TR>\n");
$myDirectory = opendir("."); // 建立操作目录的句柄
// 读出目录中的每一个子项
while($entryName = readdir($myDirectory))
{
print("<TR>");
print("<TD>$entryName</TD>");
print("<TD ALIGN=\"right\">");
print(filesize($entryName));
print("</TD>");
print("</TR>\n");
}
closedir($myDirectory); // 关闭目录
print("</TABLE>\n");
?>
</BODY>
</HTML>
PHP相关信息
<HTML>
<HEAD>
<TITLE>PHP相关信息</TITLE>
</HEAD>
<BODY>
<?
phpinfo();
?>
</BODY>
</HTML>
常用的数值判断函数
<HTML>
<HEAD>
<TITLE>常用的数值判断函数</TITLE>
</HEAD>
<BODY>
<?
//判断数组
$colors = array("red", "blue", "green");
if(is_array($colors))
{
print("colors is an array"."<br>");
}
//双精度数判断
$Temperature = 15.23;
if(is_double($Temperature))
{
print("Temperature is a double"."<br>");
}
//整数判断
$PageCount = 2234;
if(is_integer($PageCount))
{
print("$PageCount is an integer"."<br>");
}
//对象判断
class widget
{
var $name;
var $length;
}
$thing = new widget;
if(is_object($thing))
{
print("thing is an object"."<br>");
}
//字符判断
$Greeting = "Hello";
if(is_string($Greeting))
{
print("Greeting is a string"."<br>");
}
?>
</BODY>
</HTML>
文件上传界面
<HTML>
<HEAD>
<TITLE>文件上传界面</TITLE>
</HEAD>
<BODY><TABLE><CENTER>
<?
if($UploadAction){
$UploadAction=0;
$TimeLimit=60;
/*设置超时限制时间默认时间为 30s,设置为0时为不限时 */
set_time_limit($TimeLimit);
If(($Upfile != "none")&&
($Upfile != ""))
{
$Filepath="d:\web\web\php\test"; //上载文件存放路径
$FileName=$Filepath.$Upfile_name;
if($Upfile_size <1024) //上载文件大小
{$FileSize = (string)$Upfile_size . "字节";}
elseif($Upfile_size <(1024 * 1024))
{
$FileSize = number_format((double)($Upfile_size / 1024), 1) . " KB";
}
else
{
$FileSize = number_format((double)($Upfile_size/(1024*1024)),1)."MB";
}
if(!file_exists($FileName))
{
if(copy($Upfile,$FileName))
{unlink($Upfile);
echo "<br><br>\n";
echo "文件 $Upfile_name 已上载成功!";
echo "<br><br>\n";
echo "文件位置:$FileName";
echo "<br><br>\n";
echo "文件大小:$FileSize";
echo "<br><br>\n";
}
else
{echo "文件 $Upfile_name上载失败!"; }
}
else
{echo "文件 $Upfile_name已经存在!"; }
}
else
{echo "你没有选择任何文件上载!"; }
set_time_limit(30); //恢复默认超时设置
}
?>
<FORM ENCTYPE = "multipart/form-data" NAME = "SubmitForm"
ACTION = "default.php" METHOD = "POST">
<INPUT TYPE = "hidden" NAME = "MAX_FILE_SIZE" VALUE ="1000000">
<INPUT TYPE = "hidden" NAME = "UploadAction" VALUE = "1">
<TR><TD><INPUT NAME = "Upfile" TYPE = "file" SIZE = "30"></TD>
</TR><TR><TD><INPUT NAME = "submit" VALUE = "提交" TYPE = "submit">
<INPUT NAME = "reset" VALUE = "重置" TYPE = "reset"></TD>
</TR></FORM></CENTER></TABLE>
</BODY>
</HTML>