From 317d45625b235d86739e3bb25bba3368fce2f1f1 Mon Sep 17 00:00:00 2001 From: Kevin Kee Date: Sat, 7 Jun 2003 17:40:41 +0000 Subject: [PATCH] sorry... git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@130573 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/variables.xml | 415 ++++++++++++++++++++++++++++++++--------- 1 file changed, 326 insertions(+), 89 deletions(-) diff --git a/language/variables.xml b/language/variables.xml index ab762ecd6e..e40b99c7ae 100644 --- a/language/variables.xml +++ b/language/variables.xml @@ -1,22 +1,28 @@ - - + + - 各類變數 + Variables - 基本知識 + Basics - 所有 PHP 的變數都是以 "$" 為開端,後加變數名所組成的。變數名稱是有大小寫之分的。 + Variables in PHP are represented by a dollar sign followed by the + name of the variable. The variable name is case-sensitive. - PHP 的變數名稱和其他標籤有著相同的規則。一個有效的變數是由英文字母或底線開始,跟著是任意長短的字母、數字或底線。按正規運算式,它可以表達為:'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' + Variable names follow the same rules as other labels in PHP. A + valid variable name starts with a letter or underscore, followed + by any number of letters, numbers, or underscores. As a regular + expression, it would be expressed thus: + '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' - 字母為 a-z,A-Z,而 ASCII 字符則從 127 至 255〈0x7f-0xff〉。 + For our purposes here, a letter is a-z, A-Z, and the ASCII + characters from 127 through 255 (0x7f-0xff). @@ -39,14 +45,33 @@ $t - PHP 3 中,變數總是由值指派。也就是說,當您將一個運算式的值指派給一個變數時,整個原始運算式的值將會複製到目的變數。例如,當將一個變數的值指派給另一個變數之後,更改當中的任何一個變數的值並不會影響到另一個變數的值。想要知道更多這類的指派,請參閱運算式這一章。 + In PHP 3, variables are always assigned by value. That is to say, + when you assign an expression to a variable, the entire value of + the original expression is copied into the destination + variable. This means, for instance, that after assigning one + variable's value to another, changing one of those variables will + have no effect on the other. For more information on this kind of + assignment, see the chapter on Expressions. - PHP 4 則提供了另一種指派變數值的方法:傳址指派,即新變數只是參照原來變數中的值,或說新的變數成為原來變數的別名。此時,更改兩個變數中任何一個的值都會同時更改另一個的值。由於沒有執行實際的複製工作,指派的操作也因此更加快捷。不過,只有在緊密的迴圈或指派大量的陣列或物件時才會察覺速度的提升。 + PHP 4 offers another way to assign values to variables: + assign by reference. + This means that the new variable simply references (in other words, + "becomes an alias for" or "points to") the original variable. + Changes to the new variable affect the original, and vice versa. + This also means that no copying is performed; thus, the assignment + happens more quickly. However, any speedup will likely be noticed + only in tight loops or when assigning large + arrays or + objects. - 使用傳址指派,只須在被參照的原變數前加上一個 (&) 的符號。例如,下列程式碼在使用傳址指派後輸出 'My name is Bob' 兩次。 - + To assign by reference, simply prepend an ampersand (&) to the + beginning of the variable which is being assigned (the source + variable). For instance, the following code snippet outputs 'My + name is Bob' twice: + - 需要注意的是只有真正命名的變數才可以用來進行傳址指派。 + One important thing to note is that only named variables may be + assigned by reference. - 預設的變數 + Predefined variables - PHP 為它執行的程式提供了大量的預設變數。然而,由于有許多變數因依賴其所執行的伺服器種類、版本、安裝等情況而定,因此難以有完整的被記載下來。更有一些變數在指令行的模式運行時是不能使用的。有關這些變數的列表,請參考保留的先前定義變數一節。 + PHP provides a large number of predefined variables to any script + which it runs. Many of these variables, however, cannot be fully + documented as they are dependent upon which server is running, the + version and setup of the server, and other factors. Some of these + variables will not be available when PHP is run on the + command line. + For a listing of these variables, please see the section on + Reserved Predefined Variables. - PHP 4.2.0 以後的版本,register_globals 指令的預設值是 off。這是一個重大的改變。將 register_globals 的值預設為 off 會影響到預設變數在全域範圍內的有效性。例如,要得到 DOCUMENT_ROOT 的值,應使用 $_SERVER['DOCUMENT_ROOT'] 而不是 $DOCUMENT_ROOT、使用 $_GET['id'] 以取得 URL http://www.example.com/test.php?id=3 中 id 的值,而不是用 $id、或使用 $_ENV['HOME'] 來取代 $HOME。 + In PHP 4.2.0 and later, the default value for the PHP directive register_globals is + off. This is a major change in PHP. Having + register_globals off affects the set of predefined + variables available in the global scope. For example, to get + DOCUMENT_ROOT you'll use + $_SERVER['DOCUMENT_ROOT'] instead of + $DOCUMENT_ROOT, or $_GET['id'] from + the URL http://www.example.com/test.php?id=3 instead + of $id, or $_ENV['HOME'] instead of + $HOME. - 更多與此更改相關的資料可在 register_globals 的設置一節、使用 Register Globals 中的安全章,或在 4.1.04.2.0 的發行通告中取得。 + For related information on this change, read the configuration entry for + register_globals, the security + chapter on Using Register Globals + , as well as the PHP 4.1.0 + and 4.2.0 Release + Announcements. - 我們建議您使用 PHP 提供的保留預設變數,如 superglobal 陳列。 + Using the available PHP Reserved Predefined Variables, like the + superglobal arrays, + is preferred. - 自 4.1.0 版本起,PHP 額外提供了一套預設陣列,當中包含了來自伺服器〈如適用〉、環境、及使用者輸入的變數。這些新陳列比較特別,因此它們會自動地設為全域,即可以在所有範圍內使用。正因如此,它們也被稱為 'autoglobals' 或 'superglobals'。〈PHP 尚未有使用者自行定義 superglobals 的功能。〉我們將在下面列出 superglobals,但要知道它們的內容和 PHP 預設變數的深入討論,請參考保留的先前定義變數一節。此外,您也會留意舊有的預設變數 ($HTTP_*_VARS) 仍然存在。 - + From version 4.1.0 onward, PHP provides an additional set of predefined arrays + containing variables from the web server (if applicable), the + environment, and user input. These new arrays are rather special + in that they are automatically global--i.e., automatically + available in every scope. For this reason, they are often known as + 'autoglobals' or 'superglobals'. (There is no mechanism in PHP for + user-defined superglobals.) The superglobals are listed below; + however, for a listing of their contents and further discussion on + PHP predefined variables and their natures, please see the section + Reserved Predefined Variables. + Also, you'll notice how the older predefined variables + ($HTTP_*_VARS) still exist. + &avail.register-long-arrays; - 可變變數 + Variable variables - Superglobals 不能當作可變變數來使用。 + Superglobals cannot be used as + variable variables. - 如果某些 variables_order 中的變數沒有被設定,它們相對的 PHP 預設陳列也將會是空的。 + If certain variables in variables_order are not set, their + appropriate PHP predefined arrays are also left empty. - PHP 的 Superglobals + PHP Superglobals $GLOBALS - 包含一個指向程式全域範圍內可用變數的索引。此陳列的索引鍵為全域變數的名稱。$GLOBALS 自 PHP 3 開始就已存在。 + Contains a reference to every variable which is currently + available within the global scope of the script. The keys of + this array are the names of the global variables. + $GLOBALS has existed since PHP 3. @@ -135,7 +202,10 @@ $bar = &test(); // Invalid. $_SERVER - 由網頁伺服器設定的變數或目前的程式所執行的環境產生的變數。其性質類似舊有的 $HTTP_SERVER_VARS 陳列,雖然目前還可以使用,但不被建議。 + Variables set by the web server or otherwise directly related + to the execution environment of the current script. Analogous + to the old $HTTP_SERVER_VARS array (which is + still available, but deprecated). @@ -143,7 +213,9 @@ $bar = &test(); // Invalid. $_GET - 通過 HTTP GET 提供給程式的變數。其性質類似舊有的 $HTTP_GET_VARS 陳列,雖然目前還可以使用,但不被建議。 + Variables provided to the script via HTTP GET. Analogous to the + old $HTTP_GET_VARS array (which is still + available, but deprecated). @@ -151,7 +223,9 @@ $bar = &test(); // Invalid. $_POST - 通過 HTTP POST 提供給程式的變數。其性質類似舊有的 $HTTP_POST_VARS陳列,雖然目前還可以使用,但不被建議。 + Variables provided to the script via HTTP POST. Analogous to the + old $HTTP_POST_VARS array (which is still + available, but deprecated). @@ -159,7 +233,9 @@ $bar = &test(); // Invalid. $_COOKIE - 通過 HTTP cookies 提供給程式的變數。其性質類似舊有的 $HTTP_COOKIE_VARS陳列,雖然目前還可以使用,但不被建議。 + Variables provided to the script via HTTP cookies. Analogous to + the old $HTTP_COOKIE_VARS array (which is + still available, but deprecated). @@ -167,7 +243,12 @@ $bar = &test(); // Invalid. $_FILES - 通過 HTTP POST 檔案上傳提供給程式的變數。其性質類似舊有的 $HTTP_POST_FILES陳列,雖然目前還可以使用,但不被建議。詳情請參閱 POST 上傳方式。 + Variables provided to the script via HTTP post file + uploads. Analogous to the old + $HTTP_POST_FILES array (which is still + available, but deprecated). See POST method + uploads for more information. @@ -175,7 +256,9 @@ $bar = &test(); // Invalid. $_ENV - 執行環境提供給程式的變數。其性質類似舊有的 $HTTP_ENV_VARS陳列,雖然目前還可以使用,但不被建議。 + Variables provided to the script via the environment. Analogous + to the old $HTTP_ENV_VARS array (which is + still available, but deprecated). @@ -183,11 +266,20 @@ $bar = &test(); // Invalid. $_REQUEST - 通過任何使用者輸入途徑而提供給程式的變數,也正因如此不值得信任。此陳列中變數的存在與否及次序是決定於 variables_order 的設定。PHP 4.1.0 之前的版本並沒有類似此陳列的變數。請同時參閱 import_request_variables。 + Variables provided to the script via any user input mechanism, + and which therefore cannot be trusted. The presence and order + of variable inclusion in this array is defined according to the variables_order + configuration directive. This array has no direct analogue in + versions of PHP prior to 4.1.0. See also + import_request_variables. - 在指令行模式下執行時,它並包含 argvargc 項目。此兩項目只在 $_SERVER 陳列中出現。 + When running on the command line + , this will not include the + argv and argc entries; these are + present in the $_SERVER array. @@ -196,7 +288,12 @@ $bar = &test(); // Invalid. $_SESSION - 目前和程式注冊了的 session 變數。其性質類似舊有的 $HTTP_SESSION_VARS陳列,雖然目前還可以使用,但不被建議。詳情請參閱 Session 處理函數一節。 + Variables which are currently registered to a script's + session. Analogous to the old + $HTTP_SESSION_VARS array (which is still + available, but deprecated). See the Session handling functions section + for more information. @@ -206,10 +303,13 @@ $bar = &test(); // Invalid. - 變數範圍 + Variable scope - 變數的範圍只是在其所定義的空間內存在。在大部份的情況下,PHP 的變數只有一個單一的範圍。這單一的範圍也包含了以 include 和 require 方式引入的檔案。例如: + The scope of a variable is the context within which it is defined. + For the most part all PHP variables only have a single scope. + This single scope spans included and required files as well. For + example: @@ -222,7 +322,11 @@ include "b.inc"; - 例子中,變數 $a 也存在于被包含入的程式 b.inc 中。但是在使用者自訂的函數中,一個區域性的函數範圍將被引用。任何在函數內使用的變數在預設的情況下只局限於該函數的範圍內。例如: + Here the $a variable will be available within + the included b.inc script. However, within + user-defined functions a local function scope is introduced. Any + variable used inside a function is by default limited to the local + function scope. For example: @@ -243,7 +347,16 @@ Test(); - 這個程式不會輸出任何東西,因為 echo 述句使用了本區域版本的 $a 變數,而該本區域變數並未曾被分配一個值。你會發現這和 C 語言的做法不同,因為 C 的全域變數是自動的提供給各函數,除非在本域定義中指明撤銷。此做法可能會引致一些問題,如使用者不慎地更改了全域變數的值。在 PHP 中,全域變數在本域使用前必須先宣佈為全域。例子: + This script will not produce any output because the echo statement + refers to a local version of the $a variable, + and it has not been assigned a value within this scope. You may + notice that this is a little bit different from the C language in + that global variables in C are automatically available to + functions unless specifically overridden by a local definition. + This can cause some problems in that people may inadvertently + change a global variable. In PHP global variables must be + declared global inside a function if they are going to be used in + that function. An example: @@ -268,11 +381,17 @@ echo $b; - 上述例子將會輸出 "3"。在函數內宣佈 $a$b 為全域後,所有涉及該兩個變數的使用將自動指向全域的版本。PHP 沒有限定一個函數可以使用的全域變數的數量。 + The above script will output "3". By declaring + $a and $b global within the + function, all references to either variable will refer to the + global version. There is no limit to the number of global + variables that can be manipulated by a function. - 第二種存取全域範圍中的變數方法是使用一個由 PHP 特別定義的陳列:$GLOBALS。前面的例子可以重寫為: + A second way to access variables from the global scope is to use + the special PHP-defined $GLOBALS array. The + previous example can be rewritten as: @@ -295,7 +414,13 @@ echo $b; - $GLOBALS 是一個關聯陳列 (associative array),全域變數的名即為索引鍵,而該變數的內容即為陳列的值。有否留意到 $GLOBALS 在任何一個範圍都出現?那是因為 $GLOBALS 是一個superglobal。這是一個示範 superglobals 強大功能的例子: + The $GLOBALS array is an associative array with + the name of the global variable being the key and the contents of + that variable being the value of the array element. + Notice how $GLOBALS exists in any scope, this + is because $GLOBALS is a superglobal. + Here's an example demonstrating the power of superglobals: @@ -323,7 +448,11 @@ function test_global() - 變數範圍的另一個重要功能為靜態變數。靜態變數只在本域函數範圍內存在,但是當程式執行離開此範圍時,它並不會喪失它的值。看看下面的例子: + Another important feature of variable scoping is the + static variable. A static variable exists + only in a local function scope, but it does not lose its value + when program execution leaves this scope. Consider the following + example: @@ -342,7 +471,13 @@ function Test () - 此函數沒有什麼用處,因每一次執行它時,它將 $a 設為 0 然後列印出 "0"。$a++ 增加了 $a 的值,但並沒有實際用途因為一旦離開了該函數,變數 $a 也隨之消失。要設計一個有用的、不會丟失當前計數的計數函數,我們可以將 $a宣佈為靜態: + This function is quite useless since every time it is called it + sets $a to 0 and prints + "0". The $a++ which increments the + variable serves no purpose since as soon as the function exits the + $a variable disappears. To make a useful + counting function which will not lose track of the current count, + the $a variable is declared static: @@ -361,11 +496,18 @@ function Test() - 現在,每當函數 Test() 被呼叫時,它會列印出 $a 的值,然後加上一。 + Now, every time the Test() function is called it will print the + value of $a and increment it. - 靜態變數也提供一種處理遞迴函數的方法。遞迴函數是一種呼叫自己的函數。編寫遞迴函數時必須留意,因為若編寫錯誤,它有可能會無定限地遞迴。您必須確定足夠的方式來終止遞迴。下列簡單的函數將遞迴地數到 10,利用靜態變數 $count 來斷定什麼時候停止: + Static variables also provide one way to deal with recursive + functions. A recursive function is one which calls itself. Care + must be taken when writing a recursive function because it is + possible to make it recurse indefinitely. You must make sure you + have an adequate way of terminating the recursion. The following + simple function recursively counts to 10, using the static + variable $count to know when to stop: @@ -389,7 +531,12 @@ function Test() - 驅動 PHP 4 的 Zend Engine 1 是以參照的方式來實現 staticglobal 的。例如,一個真正的全域變數使用 global 方式引進一個函數範圍時實際上就是建立了一個全域變數的參照。這將導致一些意想不到的行為,正如下列例子所述: + The Zend Engine 1, driving PHP4, implements the + static and global modifier for + variables in terms of references. For example, a true global variable + imported inside a function scope with the global + statement actually creates a reference to the global variable. This can + lead to unexpected behaviour which the following example addresses: @@ -416,7 +563,7 @@ var_dump($obj); - 執行這個例子將會導致下列的輸出: + Executing this example will result in the following output: @@ -426,7 +573,8 @@ object(stdClass)(0) { - static 陳述式也會導致同樣的輸出。參照並沒有被靜態地儲存: + A similar behaviour applies to the static statement. + References are not stored statically: @@ -470,7 +618,7 @@ $still_obj2 = get_instance_noref(); - 執行此例子將導致下列的輸出: + Executing this example will result in the following output: @@ -485,17 +633,21 @@ Static object: object(stdClass)(1) { - 上述例子示範了在指派一個參照給予一個靜態變數後,當您第二次呼叫 &get_instance_ref() 函數時,它是不會記住之前的值的。 + This example demonstrates that when assigning a reference to a static + variable, it's not remembered when you call the + &get_instance_ref() function a second time. - 可變變數 + Variable variables - 有時候,可以使用可變變數會令到工作更加方便。也就是就,一個變數的名可以被動態的設定和使用。一個普通的變數是以下列的方法設定: + Sometimes it is convenient to be able to have variable variable + names. That is, a variable name which can be set and used + dynamically. A normal variable is set with a statement such as: @@ -509,7 +661,10 @@ $a = "hello"; - 一個可變變數是以另一個變數的值作為它的變數名。在上述例子中,只要加兩個 $ 在 hello 的前面就可把它當作是一個變數的名來使用了。即: + A variable variable takes the value of a variable and treats that + as the name of a variable. In the above example, + hello, can be used as the name of a variable + by using two dollar signs. i.e. @@ -523,7 +678,10 @@ $$a = "world"; - 這時,有兩個變數被定義然後儲存在 PHP 的符號表中:含有 "hello" 的 $a 及含有 "world" 的 $hello。所以,此敘述: + At this point two variables have been defined and stored in the + PHP symbol tree: $a with contents "hello" and + $hello with contents "world". Therefore, this + statement: @@ -537,7 +695,7 @@ echo "$a ${$a}"; - 與下列這句有著同樣的輸出: + produces the exact same output as: @@ -551,34 +709,48 @@ echo "$a $hello"; - 即,它們都輸出:hello world。 + i.e. they both produce: hello world. - 為了要在陳列中使用可變變數,您必須首先解決一個模糊的問題。比方說,如果您使用 $$a[1],那剖析器必須知道您是想當 $a[1] 是變數來使用,還是當 $$a 是變數,然後指向那個變數的第 [1] 個索引。解決上述模糊問題的語法是:第一個情況使用 ${$a[1]}、第二個情況使用 ${$a}[1]。 + In order to use variable variables with arrays, you have to + resolve an ambiguity problem. That is, if you write + $$a[1] then the parser needs to know if you + meant to use $a[1] as a variable, or if you + wanted $$a as the variable and then the [1] + index from that variable. The syntax for resolving this ambiguity + is: ${$a[1]} for the first case and + ${$a}[1] for the second. - 必須留意的是,可變變數是不能使用於 PHP 的 Superglobal 陳列。這表示您不能使用 ${$_GET} 這樣的敘述。如果您真的想使用 superglobals 和舊的 HTTP_*_VARS,您可以嘗試以參照的方式使用。 + Please note that variable variables cannot be used with PHP's + Superglobal arrays. + This means you cannot do things like ${$_GET}. If you are + looking for a way to handle availability of superglobals and the old + HTTP_*_VARS, you might want to try + referencing them. - 來自 PHP 以外的變數 + Variables from outside PHP - HTML 表單 (GET 和POST) + HTML Forms (GET and POST) - 當一份表單傳送給 PHP 程式時,表單內的資訊將自動提供給該程式。我們有很多方法存取這些資訊,範例: + When a form is submitted to a PHP script, the information from + that form is automatically made available to the script. There + are many ways to access this information, for example: - 一個簡單的 HTML 表單 + A simple HTML form @@ -592,12 +764,13 @@ echo "$a $hello"; - 根據您的安裝方式和個人偏好,PHP 提供多種方式存取 HTML 表單中的資料。一些例子如: + Depending on your particular setup and personal preferences, there + are many ways to access data from your HTML forms. Some examples are: - 存取一個以 POST 方式傳送的簡單 HTML 表單 + Accessing data from a simple POST HTML form - 使用 GET 方式傳送的表單用法類似,只不過使用合適的先前定義的函數。GET 也可使用在查詢字串 (QUERY_STRING) 〈URL 中問號 '?' 後面的資訊〉。例如,http://www.example.com/test.php?id=3 含有以 $_GET['id'] 方式存取的 GET 資料。請參考 $_REQUEST 和 import_request_variables()。 + Using a GET form is similar except you'll use the appropriate + GET predefined variable instead. GET also applies to the + QUERY_STRING (the information after the '?' in an URL). So, + for example, http://www.example.com/test.php?id=3 + contains GET data which is accessible with $_GET['id']. + See also $_REQUEST and + import_request_variables. - Superglobal 陳列如 $_POST$_GET 在 PHP 版本 4.1.0 以上才開始提供。 + Superglobal arrays, + like $_POST and $_GET, became + available in PHP 4.1.0 - 上面提到,在 PHP 4.2.0 之前的版本中,register_globals 的預設值是 on。而在 PHP 3 中,它永遠是 on 的。我們建議所有使用者不要依賴此指令,並在編寫時將它當作是 off 來設計您的程式。 + As shown, before PHP 4.2.0 the default value for register_globals + was on. And, in PHP 3 it was always on. The PHP + community is encouraging all to not rely on this directive + as it's preferred to assume it's off and code + accordingly. - magic_quotes_gpc 組態指令會影響到 Get,Post 和 Cookie 的值。如果開啟此指令,(It's "PHP!") 的值將變成 (It\'s \"PHP!\")。此跳出工作在將字串插入資料庫前是必須要做的。請參考 addslashesstripslashesmagic_quotes_sybase。 + The magic_quotes_gpc + configuration directive affects Get, Post and Cookie values. If + turned on, value (It's "PHP!") will automagically become (It\'s \"PHP!\"). + Escaping is needed for DB insertion. See also + addslashes, stripslashes and + magic_quotes_sybase. - PHP 也懂得處理陳列式的表單變數〈請參觀相關的 FAQ〉。您可以將相關的變數歸類,或用此功能來取回多元選擇輸入表單的值。在下列範例中,我們將表單張貼到它自己,並在傳送後把資料顯示出來: - + PHP also understands arrays in the context of form variables + (see the related faq). You may, + for example, group related variables together, or use this + feature to retrieve values from a multiple select input. For + example, let's post a form to itself and upon submission display + the data: - 更複雜的表單變數 + More complex form variables - PHP 3 的陳列表單變數只可以使用一維陳列,而 PHP 4 則沒有此限制。 + In PHP 3, the array form variable usage is limited to + single-dimensional arrays. In PHP 4, no such restriction applies. - IMAGE SUBMIT 變數名稱 + IMAGE SUBMIT variable names - 在傳送表單時,我們可以使用下列標籤將標準的提交鍵以圖像來取代: + When submitting a form, it is possible to use an image instead + of the standard submit button with a tag like: @@ -704,7 +901,13 @@ if ($HTTP_POST_VARS['action'] == 'submitted') { - 當使用者在圖像的任何一處按一下,伴隨的表單就會傳送到伺服器,隨其而來還附加了兩個變數,sub_x 和 sub_y。這包含了使用者按在圖像的座標。有經驗的使用者或許知道由瀏覽器傳送來的變數名稱應該包含句號,而不是底線,但 PHP 自動地將句號轉換成為底線。 + When the user clicks somewhere on the image, the accompanying + form will be transmitted to the server with two additional + variables, sub_x and sub_y. These contain the coordinates of the + user click within the image. The experienced may note that the + actual variable names sent by the browser contains a period + rather than an underscore, but PHP converts the period to an + underscore automatically. @@ -714,12 +917,24 @@ if ($HTTP_POST_VARS['action'] == 'submitted') { HTTP Cookies - PHP 絕對支援 Netscape規格的 HTTP cookies。Cookies 是用來將資料儲存在用戶端的瀏覽器中,以便日後追蹤或識別返回的使用者。您可以用 setcookie() 函數來設定 cookies。Cookies 是 HTTP header 的一部份,因此 SetCookie 函數一定要在任何資料傳送去瀏覽器前呼叫。這和 header() 函數有著相同的限定。Cookie 資料可以在 $_COOKIE$HTTP_COOKIE_VARS$_REQUEST 陳列中供使用。詳情和範例請參考手冊的 setcookie() 部份。 + PHP transparently supports HTTP cookies as defined by Netscape's Spec. Cookies are a + mechanism for storing data in the remote browser and thus + tracking or identifying return users. You can set cookies using + the setcookie function. Cookies are part of + the HTTP header, so the SetCookie function must be called before + any output is sent to the browser. This is the same restriction + as for the header function. Cookie data + is then available in the appropriate cookie data arrays, such + as $_COOKIE, $HTTP_COOKIE_VARS + as well as in $_REQUEST. See the + setcookie manual page for more details and + examples. - 若您要將多個值指派給一個 cookie 變數,那您就要使用陳列來指派了。範例: + If you wish to assign multiple values to a single cookie variable, you + may assign it as an array. For example: @@ -734,15 +949,21 @@ if ($HTTP_POST_VARS['action'] == 'submitted') { - 這將會建立兩個個別的 cookies,儘管現在 MyCookie在您的程式中是一個單元陳列。如果您要以一個 cookie 建立多個值,那您可先對那些值使用 serialize()explode() 函數。 + That will create two seperate cookies although MyCookie will now + be a single array in your script. If you want to set just one cookie + with multiple values, consider using serialize or + explode on the value first. - 有一點必須留意的是,一個 cookie 將會替代瀏覽器中另一個同名的 cookie,除非該 cookie 的路徑或網域名稱不同。所以,在設計購物車程式時,您最好保持一個計數器,並將此數據傳遞下去,即: + Note that a cookie will replace a previous cookie by the same + name in your browser unless the path or domain is different. So, + for a shopping cart application you may want to keep a counter + and pass this along. i.e. - 一個 <function>setcookie()</function> 的範例 + A <function>setcookie</function> example - 在變數名字中的句號 (dots) + Dots in incoming variable names - 一般上,PHP 並不會更改傳入程式的變數名稱。然而,您必須留意句號 (.) 並不是一個有效的 PHP 變數名稱字元,正如: - + Typically, PHP does not alter the names of variables when they + are passed into a script. However, it should be noted that the + dot (period, full stop) is not a valid character in a PHP + variable name. For the reason, look at it: ]]> - 這時,剖析器見到的只是一個叫做 $varname 的變數,跟隨著字串連結運算子 (.),再來是一個無引號的字串:ext。顯然地,這並不會帶來您想要的結果。 + Now, what the parser sees is a variable named + $varname, followed by the string concatenation + operator, followed by the barestring (i.e. unquoted string which + doesn't match any known key or reserved words) 'ext'. Obviously, + this doesn't have the intended result. - 正因如此,PHP 會自動的將傳入的變數名稱中的句號 (.) 改為底線 (_)。 + For this reason, it is important to note that PHP will + automatically replace any dots in incoming variable names with + underscores. - 判定變數的類型 + Determining variable types - 由於 PHP 會自行判斷變數的類型,並按需要轉變它們的類型,因此有時也難以明顯地判定一個變數在某個時刻的類型。有鑑於此,PHP 提供了幾個判定變數類型的函數:gettype()is_array()is_float()is_int()is_object()is_string()。請參閱類型一章。 + Because PHP determines the types of variables and converts them + (generally) as needed, it is not always obvious what type a given + variable is at any one time. PHP includes several functions + which find out what type a variable is, such as: + gettype, is_array, + is_float, is_int, + is_object, and + is_string. See also the chapter on + Types. @@ -809,4 +1046,4 @@ End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 ---> \ No newline at end of file +-->