PHP-Cookbook-chapte8

程式碼

//'8.5'在函式呼叫間維持變數值
$session_name = $session_txt['8.5'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

function sequence_get_next_value()
{
		static $x =0;
		return $x++;
}

print sequence_get_next_value ()."=1st value <br>"; 
print sequence_get_next_value ()."=2nd value <br>";
print sequence_get_next_value ()."=3rd value <br>";


$message="
<fieldset><legend><font color=red>TIPS</font></legend>
     staric 變數 = 值,設定變數的維持值<br>
	 staic敘述是避免在函式中使用全域變數的良好方法。在PHP程式<br>
	 執行中,它會在函式間記得\$x的值,這表示程式執行完之後,PHP<br>
	 會忘記此靜態變數的值。
<br>
</fieldset>
";
echo $message."<br>";
//'8.8'動態建立匿名函式
$session_name = $session_txt['8.8'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

function greet ($type){
	return create_function ('$greeting',
							"print \"$type:\$greeting <br> \";");
}
$greeting1 =greet("Casual");
$greeting2 =greet("Formal");

$greeting1("How's it going?");
$greeting1("What's up doc?");

$greeting2("Hello");
$greeting2("Hello,my name is Sterling, it is a pleasure to meet you."); 




$message="
<fieldset><legend><font color=red>TIPS</font></legend>
     create_function()函式,會動態建立一個函式,並以第一個參數當作<br>
	 該函式的參數,以第二個參數當作該函式的程式碼。它會傳回該函式名稱,<br>
	 然後你就可以呼叫該函式。這種粗糙的包裝形式有許多用途,包括call_back()<br>
	 函式。
<br>
</fieldset>
";
echo $message."<br>";
//'8.10'取得任意數量的參數
$session_name = $session_txt['8.10'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

$input_record_separator = " ";

function perl_print (){
	$args =func_num_args();
	/*
	for ($idx = 0; $idx <$argc; $idx++){
		$current_arg = func_num_args($idx);
		print $current_arg.$input_record_separator;
	}
	*/
	foreach ($args as $arg){
		print $arg. $input_record_separator;
	}
}
function perl_print1 (){
	
	$argc =func_num_args();
	for ($idx = 0; $idx <$argc; $idx++){
		$current_arg = func_num_args($idx);
		print $current_arg.$input_record_separator;
	}
	
}

perl_print("Hello World \n","My Name is ","Sterling");
perl_print1("Hello World \n","My Name is ","Sterling","234");

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   PHP提供一組方便的函式,可接受任意數量的參數。func_num_args()   
<br>
</fieldset>
";
echo $message."<br>";

PHP傳址呼叫、傳值呼叫、傳遞參考

常常看到函數前會加一個&符號,&到底是什麼意思?

例如

funcion &get_product_list ($department){
    $product_list = array();
    return $product_list;
}

$products = &get_product_list('rd')

這裡的加上&符號是什麼意思呢?查一下知道是用傳遞參考的方法呼叫函數。函數傳遞資料有三種方式:傳址呼叫(pass by adress)、傳值呼叫(pass by value)、傳遞參考(pass by reference)區別在哪裡呢?以下說明

& ->取址運算子

*->取值運算子

函數中資料的傳遞:傳值、傳址、傳參考
以下的說明是每種程式語言都通用的觀念,但不同程式語言有不同的語法,以及不同的程式語言不一定都支援全部的傳遞方式,要看你使用的是哪種程式語言。

PHP-Cookbook-chapter7

程式

//'7.1'處理時間標記
$session_name = $session_txt['7.1'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

$fn = $file_name = ROOT_PATH."/test/6-10.txt";

print "$fn was last accessed on:". fileatime ($fn)."<br>";
print "$fn was last change on:". filectime ($fn)."<br>";
print "$fn was last modified on:". filemtime ($fn)."<br>";

if (touch ($fn,$date)){
	print "Timestamp changed....";
}else{
	print "Modification Failed";
}

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   在PHP中,copy()是更改檔案時間標記的唯一方式,而它唯一變更<br>
   的是檔案修改時間。因此,除非你用exec(),system()跳出PHP之外<br>
   ,否則你只能更改檔案的mtime。
   
<br>
</fieldset>
";
echo $message."<br>";
//'7.2'移除檔案
$session_name = $session_txt['7.2'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";


$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   用unlink()函式處理你要刪除的檔案。若成功傳回1,否則傳回0。<br>
   因此你可以用or運算子或if-else區塊。<br>
   如果你想刪除數個檔案,你可以用forch迴圈讀取陣列元素,然後<br>
   刪除每個檔案。<br>
   foreach (\$file as \$file){ <br>
        unlink(\$file); <br>
		or die ('Waring str');<br>
   }
<br>
</fieldset>
";
echo $message."<br>";
//'7.3'拷貝或移動檔案
$session_name = $session_txt['7.3'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";


$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   在PHP中,copy()函式<br>
   if (copy(\$orginal,\$new)){<br>
      print '成功'; <br>
   }else{  <br>
		print '失敗'; <br>
   }  <br>
   或者如果你需要移動檔案,就用rename()函式 <br>
   rename(\$original,\$new) <br>
      or die ('warmingstr')
<br>
</fieldset>
";
echo $message."<br>";
//'7.5'分析檔名的各個組成部分
$session_name = $session_txt['7.5'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

$fn = ROOT_PATH."/test/6-11.php";

$pinfo = pathinfo ($fn);
print "Directory name is :".$pinfo [dirname ] ."\n<br>";
print "Filename is :".$pinfo [basename ]." \n<br>";
print "Ending is :".$pinfo [extension ]." \n<br>";




$message="
<fieldset><legend><font color=red>TIPS</font></legend>
     參數 dirname 檔案位在的目錄<br>
     參數 basename 檔案名稱<br>
     參數 extension 檔案副檔名,若它有的話
<br>
</fieldset>
";
echo $message."<br>";
//'7.8'循序處裡目錄中的檔案
$session_name = $session_txt['7.8'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

$dir_name = ROOT_PATH."/test";
$dh = dir ($dir_name);
echo "方法一<br>";
while ($entry = $dh ->read()){
	print $entry ."\n";
}
$dh->close();

$dh = opendir ($dir_name);
echo "<br>方法二<br>";
while ($entry = readdir ($dh)){
	print $entry ."\n";
}
closedir();

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
    你可以用dir()函式透過一虛擬的物件處裡目錄或用opendir()<br>
	、readdir()、和closedir()來讀取目錄。
 	 
<br>
</fieldset>
";
echo $message."<br>";

PHP-Cookbook-chapter6

程式

//'6.3'建立暫存檔
$session_name = $session_txt['6.3'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

$tmp = tempnam (ROOT_PATH."/test","tmpFile");

$fp = @fopen ($tmp,"w");
if (!@fp){
	die ("無法開啟 $tmp");
}

fputs ($fp,"Hello World <br>");
fputs ($fp,"Another Line in a Temporary File");

@fclose ($fp);

$fp =@fopen ($tmp, "r");
if(!$fp){
	die ("無法開啟 $tmp");
}

while ($line = @fgets ($fp,1024)){
	print $line;
}
@fclose ($fp);
@unlink ($tmp)
or die ("無法刪除 $tmp");

	
$message="
<fieldset><legend><font color=red>TIPS</font></legend>
   tempnam(位置,前綴詞)函式會在指定的目錄下產生一個指定<br>
   前綴詞唯一檔。檔案建立後,會傳回檔名(本例指定給\$tmp_file_name)<br>
   ,程式節結束後,記得用unlink()刪除檔案。
   
<br>
</fieldset>
";
echo $message."<br>";
//'6.4'將檔案存到程式裡
$session_name = $session_txt['6.4'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

$file_name = ROOT_PATH."/test/3-1.txt";
$fp = @fopen ($file_name,"rb") or
	die ("無法開啟{$file_name}");

clearstatcache(); //清除快取

$f_contents = fread ($fp,filesize ($fp)); //方法一
//echo $f_contents; 

unset ($f_contents);
$f_contents = show_array(file ($file_name));//方法二

unset ($f_contents);
$f_contents = implode ("",file ($file_name));//方法三
echo $f_contents."<br>";

unset ($f_contents);
while ($line = @fgets ($fp,1024)){  //方法四
	$f_contents .=$line; 
}
echo $f_contents;

@fclose ($fp);

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
    fread()讀取資料,並搭配filesize(),將檔案載入變數中<br>
	如果想把檔案一行一行存入陣列,就用file()函式。<br>
	另外要使用filesize()時,最好用clearstrstcache()清除快取。
<br>
</fieldset>
";
echo $message."<br>";
//'6.8'鎖定檔案
$session_name = $session_txt['6.8'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

/*
Lockfile.inc
<?
function lock ($fp,$lock_level=LOCK_EX){
	@flock ($fp,$lock_level)
	or die ("無法以 $lock_level 鎖定檔案指標");
}
function unlock ($fp){
	@flock ($fp,LOCK_UN)
	or die ("無法解除鎖定");
}
?>
*/
include ("Lockfile.inc");

unset ($file_name);
$file_name = ROOT_PATH."/test/3-1.txt";

$fp = @fopen ($file_name,"wb")
		or die ("無法開啟 $file_name 以供寫入");

//寫入檔案前請務必取得獨佔鎖定(exclusive)
//這相當於 flock($fp,LOCK_EX)
lock ($fp);

fwrite ($fp,"Hello");

//解除檔案鎖定,這相當於 flock($fp,LOCK_UN)
unlock ($fp);
@fclose ($fp);



$message="
<fieldset><legend><font color=red>TIPS</font></legend>
    你想確保存檔案期間不會有其他程式存取,使用flock()函式<br>
	參數為 LOCK_EX 鎖定、LOCK_UN 解除鎖定
<br>
</fieldset>
";
echo $message."<br>";
//'6.10'顯示文字檔內容
$session_name = $session_txt['6.10'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

$read_file = ROOT_PATH."/test/6-10.txt";
$fp = @fopen ($read_file,"rb")
		or die (" Cannot read the $read_file !");
$twenty_byes = fread ($fp,20);
fpassthru($fp);

echo "<br>";

readfile ($read_file);

echo "<br>";

print implode ("<br>",file ($read_file));



@fclose ($fp);

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
  有三個函式可以讀取文字檔內容。fpassthru()、readfile()、<br>
  implode(\"區隔符號\",file(檔案位置))
<br>
</fieldset>
";
echo $message."<br>";
//'6.22'隨機排列檔案內容
$session_name = $session_txt['6.22'];
echo $session_name."<br>";
//echo ROOT_PATH."<br>";

$fn = ROOT_PATH."/test/6-10.txt";
$reg_array = file($fn);

srand ((double)microtime()*1000000);
shuffle ($reg_array);

$fp = @fopen($fn,"wb") or die ("無法開啟檔案 $fn");
	fputs ($fp,implode ("",$reg_array));

echo "<br>以下是亂數排列後的內容:<br>";	
readfile ($fn);
echo "<br>";


$fn1 = ROOT_PATH."/test/6-11.txt";
srand ((double)microtime()*1000000);
$fp1 = @fopen($fn1,"rb") or die ("無法開啟檔案 $fn");
foreach (file ($fn1) as $line){
	$words = preg_split ("/\s+/",$line);
	shuffle ($words);
	$data .= implode ("",$words."\n");
}

print "herd".$data; //有問題出不來

@fclose ($fp1) or die ("無法關閉檔案 $fn");
@fclose ($fp) or die ("無法關閉檔案 $fn");

$message="
<fieldset><legend><font color=red>TIPS</font></legend>
  首先用file()函式將檔案載入陣列,現在陣列中已經有檔案的每一行<br>
  內容,我們就可以將陣列個元素隨機排列。接著我們可以為shuffle()<br>
  設定亂數產生器的起點,然後將陣列傳給shuffle()(請注意不需要<br>
  shuffle()的傳回值,因為它直接操控陣列本身。
<br>
</fieldset>
";
echo $message."<br>";