经典循环例子
经典循环例子
for($counter = 1; $counter {
print(counter is $counter
\n); //打印6次
}
?>
for的高级运用
for的高级运用
/*
** 打印必要的说明文字
*/
print(距离星期一还有几天?\n);
print(\n);
for($currentdate = date(u); //定义$currentdate时间格式
date(l, $currentdate) != monday; //判断是不是当前系统时间是monday
$currentdate += (60 * 60 * 24)) //当前时间加上1天
{
/*
** 打印时间名称
*/
print( . date(l, $currentdate) . \n);
}
print(\n);
?>
函数的简单调用:
简单的函数
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(这是蓝颜色的字!, blue); //调用function函数
print(
\n);
?>
用的规算法判断是否是整数
判断整数
function checkinteger($number)
{
if($number > 1)
{
/* 整数减1仍然是整数 */
return(checkinteger($number-1));
}
elseif($number {
/* 对于一个负数,*/
/* 可以分析它的绝对值*/
return(checkinteger((-1)*$number-1));//取绝对值,把负数按整数分析
}
else
{
if(($number > 0) and ($number {
return(当然不是);
}
else
{
/* 0 和 1 是整数 */
/* 根据相关数学定义 */
return(是的);
}
}
}
print(0是整数吗? .
checkinteger(0) .
\n);
print(7是整数吗? .
checkinteger(7) .
\n);
print(3.5呢? . checkinteger(3.5) .
\n);
print(那么-5呢? . checkinteger(-5) .
\n);
print(还有-9.2? . checkinteger(-9.2) .
\n);
?>
初始化数组
初始化数组
$monthname = array(1=>january, february, march,//初始化一个数组
april, may, june, july, august,
september, october, november, december);
print( 英语的“5月”是 $monthname[5] 。
\n);//打印数组中的第6个元素
?>
获取数组中的元素
获取数组中的元素
$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 5 is . $monthname[5].
\n);
print(month aug is . $monthname[aug] .
\n);
print(month june is . $monthname[june] .
\n);
?>
创建一个多维数组
创建一个多维数组
$cities = array( //二维数组array()
华北地区=>array(
北京市,
天津市,
石家庄
),
西北地区=>array(
西安,
拉萨
)
);
print(华北地区: .$cities[华北地区][0]); //打印$cities[华北地区][0]
?>
php 4.0实现表格状打印
实现表格状打印
/*
** 数据表格化
*/
print(\n); // 表格开始
for($row=1; $row {
print(\n); // 开始行
// do each column
for($column=1; $column {
print();//开始列
print($row * $column);//表格元素乘积
print( );
}
print(
\n); // 行结束
}
print(
\n); // 表格结束
?>
查看系统的一些变量
查看php的环境变量
print(你正在用文件的名字为: );
print(__file__);
print(
\n);
print();
print(你的操作系统为: );
print(php_os);
print();
print(你的php的版本为: );
print(php_version)
?>
打开本地或者远程文件
打开本地或者远程文件
print(通过http协议打开文件\n);
// 通过 http 协议打开文件
if(!($myfile = fopen(d:web/web/php/test/data.txt, r)))
{
print(文件不能打开);
exit;
}
while(!feof($myfile)) //循环
{
// 按行读取文件中的内容
$myline = fgetss($myfile, 255);
print($myline
\n);
}
// 关闭文件的句柄
fclose($myfile);
?>
打开文件的几种方式比较
读取文件内容
// 打开文件同时打印文件的每一个字符
if($myfile = fopen(data.txt, r))
{
while(!feof($myfile))
{
$mycharacter = fgetc($myfile);
print($mycharacter);
}
fclose($myfile);
}
?>
print();?>
// 打开文件同时打印文件的每一行
if($myfile = fopen(data.txt, r))
{
while(!feof($myfile))
{
$myline = fgets($myfile, 255);
print($myline);
}
fclose($myfile);
}
?>
print();?>
/* 打开文件同时打印文件的每一行,
同时去掉取回字符串中的 html 语言
*/
if($myfile = fopen(data.txt, r))
{
while(!feof($myfile))
{
$myline = fgetss($myfile, 255);
print($myline);
}
fclose($myfile);
}
?>
访问文件常见属性
访问文件常见属性
print(文件的所有者(uid 值):);
print(fileowner(data.txt).
);
print(文件的大小:);
print(filesize(data.txt).
);
print(文件的类型:);
print(filetype(data.txt).
);
?>
调用文本文件内容
调用文本文件内容
// 打开文件同时,打印每一行
$myfile = file( data.txt);
for($index = 0; $index {
print($myfile[$index].
);
}
?>
创建目录函数
创建目录函数
if(mkdir(mydir1, 0777)) //创建目录的函数
{
print(目录创建成功); //目录建立成功
}
else
{
print(目录建立失败!); //目录建立失败
}
?>
浏览目录
浏览目录
// 使用表格浏览目录的结构
print(\n);
// 创建表格的头
print(\n);
print(文件名\n);
print(文件的大小\n);
print(
\n);
$mydirectory = opendir(.); // 建立操作目录的句柄
// 读出目录中的每一个子项
while($entryname = readdir($mydirectory))
{
print();
print($entryname );
print();
print(filesize($entryname));
print( );
print(
\n);
}
closedir($mydirectory); // 关闭目录
print(
\n);
?>
php相关信息
php相关信息
phpinfo();
?>
常用的数值判断函数
常用的数值判断函数
//判断数组
$colors = array(red, blue, green);
if(is_array($colors))
{
print(colors is an array.
);
}
//双精度数判断
$temperature = 15.23;
if(is_double($temperature))
{
print(temperature is a double.
);
}
//整数判断
$pagecount = 2234;
if(is_integer($pagecount))
{
print($pagecount is an integer.
);
}
//对象判断
class widget
{
var $name;
var $length;
}
$thing = new widget;
if(is_object($thing))
{
print(thing is an object.
);
}
//字符判断
$greeting = hello;
if(is_string($greeting))
{
print(greeting is a string.
);
}
?>
文件上传界面
文件上传界面
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 {$filesize = (string)$upfile_size . 字节;}
elseif($upfile_size {
$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
\n;
echo 文件 $upfile_name 已上载成功!;
echo
\n;
echo 文件位置:$filename;
echo
\n;
echo 文件大小:$filesize;
echo
\n;
}
else
{echo 文件 $upfile_name上载失败!; }
}
else
{echo 文件 $upfile_name已经存在!; }
}
else
{echo 你没有选择任何文件上载!; }
set_time_limit(30); //恢复默认超时设置
}
?>
action = default.php method = post>