程式碼
//'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>";