初學者學習PHP開發應該掌握的幾段精華代碼

  經典循環例子
  <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>