经典循环例子
<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>