PHP实用小程序(三)
<HTML> <HEAD> <TITLE>给数组增加元素</TITLE> </HEAD> <? $Cities[] = "<B>北京</B>"; //等同于$Cities[0] = "北京" $Cities[] = "<B>天津</B>"; //等同于$Cities[1] = "天津" $Cities[] = "<B>上海</B>"; //等同于$Cities[2] = "上海" $Cities[] = "<B>深圳</B>"; //等同于$Cities[3] = "深圳" /* ** 统计元素个数 */ $indexLimit = count($Cities); //把数组中元素的个数赋给$indexLimit /* ** 打印所有数组 */ for($index=0; $index < $indexLimit; $index++) { print("第 $index 个城市是 $Cities[$index]。 <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>
<HTML> <HEAD> <TITLE>PHP 4.0实现表格状打印</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>实现背景颜色的改变</TITLE> </HEAD> <BODY> <? print("<HTML>\n"); print("<HEAD>\n"); print("<TITLE>改变背景颜色</TITLE>\n"); print("</HEAD>\n"); /* ** 第一次被浏览的时候 ** 使用白色为默认的颜色。 */ if($inputColor == "") { $inputColor = "FFFFFF"; } /* ** 使用背景颜色打开网页 */ print("<BODY BGCOLOR=\"#$inputColor\">\n"); /* ** 表单提交到本页 */ print("<FORM ACTION=\"37.php\" METHOD=\"post\">\n"); /* ** 得到颜色的数据 */ print("<B>请填写HTML颜色代码:</B> "); print("<INPUT "); print("TYPE=\"text\" "); print("NAME=\"inputColor\" "); print("VALUE=\"$inputColor\">\n"); /* ** 提交的按钮 */ print("<INPUT "); print("TYPE=\"submit\" "); print("NAME=\"Submit_Button\" "); print("VALUE=\"提交颜色\">\n"); print("</FORM>\n"); print("</BODY>\n"); print("</HTML>\n"); ?> </BODY> </HTML>
<HTML> <HEAD> <TITLE>文件上传</TITLE> </HEAD> <BODY> <? //检查是否有文件上传 if(isset($UploadedFile)) { //unlink($UploadedFile); print("本地 File: $UploadedFile <BR>\n"); print("名称: $UploadedFile_name <BR>\n"); print("大小: $UploadedFile_size <BR>\n"); print("类型: $UploadedFile_type <BR>\n"); print("<HR>\n"); } ?> <FORM ENCTYPE="multipart/form-data" ACTION="38.php" METHOD="post"> <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="4096"> <INPUT NAME="UploadedFile" TYPE="file" size='15'> <INPUT TYPE="submit" VALUE="上传文件"> </FORM> </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> <? /* ** 取得月份名称数组 */ include("40-2.php"); //这里包含文件 //print("这里时被包含进来的"); echo("Month 5 is " . $monthName[5] . "<BR>\n"); echo("Month Aug is " . $monthName["Aug"] . "<BR>\n"); print("Month June is " . $monthName["June"] . "<BR>\n"); MyCounter(999)//这里调用包含文件中的函数 ?> </BODY> </HTML>
<? function MyCounter($counter) {//一个简单的函数 print("<hr>"); print($counter);//打印传入的参数 print("<hr>"); } ?>
<HTML> <HEAD> <TITLE>打开本地或者远程文件</TITLE> </HEAD> <BODY> <? print("<H3>通过http协议打开文件</H3>\n"); // 通过 http 协议打开文件 if(!($myFile = fopen("http://xlj/php/jiang/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>PHP判断文件是否存在</TITLE> </HEAD> <BODY> <? print("data.txt文件是否存在?"."<br>"); if(file_exists("data.txt")) { print("这个文件存在"); //文件存在 print(fileinode("data.txt")); } else { print("文件不存在"); //文件不存在 } ?> </BODY> </HTML>