主页 > 知识库 > 网络编程 > PHP >

PHP

几段值得初学者研究的PHP代码段(2)

来源:未知 作者:admin 发表于:2009-09-29 11:00  点击:
初始化数组 HTML HEAD TITLE初始化数组/TITLE /HEAD FONT SIZE=5 ? $monthName = array(1=January, February, March,//初始化一个数组 April, May, June, July, August, September, October, November, December);

初始化数组

<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>

有帮助
(248)
77%
没帮助
(74)
23%