20090922_php學習筆記

1. 單引號和雙引號區別 [url]http://cuckoosnest.iteye.com/blog/473888[/url]
2. define常量,常量引用不要加$
set_include_path設置include目錄
<?php
// Works as of PHP 4.3.0
set_include_path('/inc');

// Works in all PHP versions
ini_set('include_path', '/inc');
?>
<?php
$path = '/usr/lib/pear';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
// In this example we add /usr/lib/pear to the end of the existing include_path.
?>

3. 路徑相關: dirname( __FILE ) 當前文件所在目錄,被include的文件中調用此方法,也是指被include的文件所在目錄。
3. 字符串函數
str_replace("\\", '/', substr(dirname(__FILE__), 0, -7));  //這裏的 "\\" 不能寫成 '\'


substr()方法
<?php
echo substr('abcdef', 1); // bcdef
echo substr('abcdef', 1, 3); // bcd
echo substr('abcdef', 0, 4); // abcd
echo substr('abcdef', 0, 8); // abcdef
echo substr('abcdef', -1, 1); // f

// Accessing single characters in a string
// can also be achived using "curly braces"
$string = 'abcdef';
echo $string{0}; // a
echo $string{3}; // d
echo $string{strlen($string)-1}; // f

?>


[color=red]string implode ( string glue, array pieces )[/color] 相當於list join
<?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?>

[color=red]string stripslashes ( string str )[/color]
<?php
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);

return $value;
}

// Example
$array = array("f\\'oo", "b\\'ar", array("fo\\'o", "b\\'ar"));
$array = stripslashes_deep($array);

// Output
print_r($array);
?>
//打印出
Array
(
[0] => f'oo
[1] => b'ar
[2] => Array
(
[0] => fo'o
[1] => b'ar
)

)


[quote]The strcasecmp() function compares two strings.
strcasecmp()函數的作用是:對兩個字符串進行比較。

This function returns:
該函數將返回下列值:

0 - if the two strings are equal
0 – 如果字符串相等
<0 - if string1 is less than string2
<0 – 如果string1小於string2 [/quote]

[color=green]int strpos ( string haystack, mixed needle [, int offset] ) [/color] [color=red]查找needle在haystack中的位置,
注意:如果不匹配到needle,則返回false,也有可能返回0,因此判斷這個函數返回值必須用 === 符號![/color]


4. 時間函數 [color=red]microtime() [/color]
對腳本進行計時
<?php
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();

// Sleep for a while
usleep(100);

$time_end = microtime_float();
$time = $time_end - $time_start;

echo "Did nothing in $time seconds\n";
?>


[color=red]int time ( void ) [/color]返回自從 Unix 紀元(格林威治時間 1970 年 1 月 1 日 00:00:00)到當前時間的秒數。


5. php定義變量: var 一般是出現在類對象中,一般的過程和函數不需要 var定義變量。

6. magic_quotes_gpc magic_quotes_runtime 配置的作用: [url]http://cuckoosnest.iteye.com/blog/473950[/url]

7. 關於全局變量 超全局變量,獲取用戶post get參數,獲取服務器信息
[quote]從 PHP 4.1.0 開始,PHP 提供了一套附加的預定數組,這些數組變量包含了來自 web 服務器(如果可用),運行環境,和用戶輸入的數據。這些數組非常特別,它們在全局範圍內自動生效,例如,在任何範圍內自動生效。因此通常被稱爲自動全局變量(autoglobals)或者超全局變量(superglobals)。(PHP 中沒有用戶自定義超全局變量的機制。)超全局變量羅列於下文中;但是爲了得到它們的內容和關於 PHP 預定義變量的進一步的討論以及它們的本質,請參閱預定義變量。而且,你也將注意到舊的預定義數組($HTTP_*_VARS)仍舊存在。自 PHP 5.0.0 起,長格式的 PHP 預定義變量可以通過設置 register_long_arrays 來屏蔽。 [/quote]
[color=red][quote]PHP 4.2.0 以及後續版本中,PHP 指令 register_globals 的默認值爲 off。這是 PHP 的一個主要變化。讓 register_globals 的值爲 off 將影響到預定義變量集在全局範圍內的有效性。例如,爲了得到 DOCUMENT_ROOT 的值,將必須使用 $_SERVER['DOCUMENT_ROOT'] 代替 $DOCUMENT_ROOT,又如,使用 $_GET['id'] 來代替 $id 從 URL http://www.example.com/test.php?id=3 中獲取 id 值,亦或使用 $_ENV['HOME'] 來代替 $HOME 獲取環境變量 HOME 的值。 [/quote][/color]


8. [color=red]void session_set_cookie_params ( int lifetime [, string path [, string domain [, bool secure]]] )[/color]
session_set_cookie_params方法修改cookie參數,覆蓋php.ini中的定義,作用域是頁面,因此每次請求都應該在[color=red]session_start()[/color]調用之前調用此方法。

9. [color=green]int extract ( array var_array [, int extract_type [, string prefix]] )[/color] 把map型數組定義的名值對extract成變量。

這個方法在magic_quotes_gpc配置爲false的時候需要調用:
$_POST = new_addslashes($_POST);
$_GET = new_addslashes($_GET);
$_COOKIE = new_addslashes($_COOKIE);
@extract($_POST);
@extract($_GET);
@extract($_COOKIE);


10. [color=darkblue]string mysql_real_escape_string ( string unescaped_string [, resource link_identifier] )
本函數將 unescaped_string 中的特殊字符轉義,並計及連接的當前字符集,因此可以安全用於 mysql_query()。 [/color]

[color=red]注: mysql_real_escape_string() 並不轉義 % 和 _。 [/color]

11.
發佈了31 篇原創文章 · 獲贊 0 · 訪問量 922
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章