kevinkee Sat Jun 7 13:40:42 2003 EDT Modified files: /phpdoc/en/language variables.xml Log: sorry...
Index: phpdoc/en/language/variables.xml diff -u phpdoc/en/language/variables.xml:1.60 phpdoc/en/language/variables.xml:1.61 --- phpdoc/en/language/variables.xml:1.60 Sat Jun 7 13:27:52 2003 +++ phpdoc/en/language/variables.xml Sat Jun 7 13:40:41 2003 @@ -1,22 +1,28 @@ -<?xml version="1.0" encoding="big5"?> -<!-- $Revision: 1.60 $ --> +<?xml version="1.0" encoding="iso-8859-1"?> +<!-- $Revision: 1.61 $ --> <chapter id="language.variables"> - <title>各類變數</title> + <title>Variables</title> <sect1 id="language.variables.basics"> - <title>基本知識</title> + <title>Basics</title> <simpara> - 所有 PHP 的變數都是以 "$" 為開端,後加變數名所組成的。變數名稱是有大小寫之分的。 + Variables in PHP are represented by a dollar sign followed by the + name of the variable. The variable name is case-sensitive. </simpara> <para> - PHP [EMAIL PROTECTED]'[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]*' </para> <note> <simpara> - 字母為 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). </simpara> </note> @@ -39,14 +45,33 @@ </para> <para> - PHP 3 [EMAIL PROTECTED]@個變數時,整個原始運算式的值將會[EMAIL PROTECTED]@[EMAIL PROTECTED]|[EMAIL PROTECTED]<link linkend="language.expressions">運算式</link>[EMAIL PROTECTED] + 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 <link + linkend="language.expressions">Expressions</link>. </para> <para> - PHP 4 [EMAIL PROTECTED]<link linkend="language.references">傳址指派</link>[EMAIL PROTECTED]|[EMAIL PROTECTED]@[EMAIL PROTECTED]<link linkend="language.types.array">陣列</link>或<link linkend="language.types.object">物件</link>時才會察覺速度的提升。 + PHP 4 offers another way to assign values to variables: + <link linkend="language.references">assign by reference</link>. + 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 + <link linkend="language.types.array">arrays</link> or + <link linkend="language.types.object">objects</link>. </para> <para> - [EMAIL PROTECTED] (&) 的符號。例如,下列程式碼在使用傳址指派後輸出 '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: + <informalexample> <programlisting role="php"> <![CDATA[ @@ -63,7 +88,8 @@ </para> <para> - 需要注意的是只有真正命名的變數才可以用來進行傳址指派。 + One important thing to note is that only named variables may be + assigned by reference. <informalexample> <programlisting role="php"> <![CDATA[ @@ -86,48 +112,89 @@ </sect1> <sect1 id="language.variables.predefined"> - <title>預設的變數</title> + <title>Predefined variables</title> <simpara> - PHP [EMAIL PROTECTED]<link linkend="features.commandline">指令行</link>的模式運行時是不能使用的。有關這些變數的列表,請參考<link linkend="reserved.variables">保留的先前定義變數</link>[EMAIL PROTECTED] + 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 + <link linkend="features.commandline">command line</link>. + For a listing of these variables, please see the section on + <link linkend="reserved.variables">Reserved Predefined Variables</link>. </simpara> <warning> <simpara> - PHP 4.2.0 以後的版本,<link linkend="ini.register-globals">register_globals</link> 指令的預設值是 <emphasis>off</emphasis>[EMAIL PROTECTED] register_globals 的值預設為 <emphasis>off</emphasis> 會影響到預設變數在全域範圍內的有效性。例如,要得到 <varname>DOCUMENT_ROOT</varname> 的值,應使用 <varname>$_SERVER['DOCUMENT_ROOT']</varname> 而不是 <varname>$DOCUMENT_ROOT</varname>、使用 <varname>$_GET['id']</varname> 以取得 URL <literal>http://www.example.com/test.php?id=3</literal> 中 id 的值,而不是用 <varname>$id</varname>、或使用 <varname>$_ENV['HOME']</varname> 來取代 <varname>$HOME</varname>。 + In PHP 4.2.0 and later, the default value for the PHP directive <link + linkend="ini.register-globals">register_globals</link> is + <emphasis>off</emphasis>. This is a major change in PHP. Having + register_globals <emphasis>off</emphasis> affects the set of predefined + variables available in the global scope. For example, to get + <varname>DOCUMENT_ROOT</varname> you'll use + <varname>$_SERVER['DOCUMENT_ROOT']</varname> instead of + <varname>$DOCUMENT_ROOT</varname>, or <varname>$_GET['id']</varname> from + the URL <literal>http://www.example.com/test.php?id=3</literal> instead + of <varname>$id</varname>, or <varname>$_ENV['HOME']</varname> instead of + <varname>$HOME</varname>. </simpara> <simpara> - 更多與此更改相關的資料可在 <link linkend="ini.register-globals">register_globals</link> [EMAIL PROTECTED]<link linkend="security.registerglobals">使用 Register Globals</link> 中的安全章,或在 <ulink url="&url.php.release4.1.0;">4.1.0</ulink> 及 <ulink url="&url.php.release4.2.0;">4.2.0</ulink> 的發行通告中取得。 + For related information on this change, read the configuration entry for + <link linkend="ini.register-globals">register_globals</link>, the security + chapter on <link linkend="security.registerglobals">Using Register Globals + </link>, as well as the PHP <ulink url="&url.php.release4.1.0;">4.1.0 + </ulink> and <ulink url="&url.php.release4.2.0;">4.2.0</ulink> Release + Announcements. </simpara> <simpara> - 我們建議您使用 PHP 提供的保留預設變數,如 <link linkend="language.variables.superglobals">superglobal 陳列</link>。 + Using the available PHP Reserved Predefined Variables, like the + <link linkend="language.variables.superglobals">superglobal arrays</link>, + is preferred. </simpara> </warning> <simpara> - 自 4.1.0 版本起,PHP [EMAIL PROTECTED]|自動地設為全域,即可以在所有範圍內使用。正因如此,它們也被稱為 'autoglobals' 或 'superglobals'。〈PHP 尚未有使用者自行定義 superglobals 的功能。〉我們將在下面列出 superglobals,但要知道它們的內容和 PHP 預設變數的深入討論,請參考<link linkend="reserved.variables">保留的先前定義變數</link>[EMAIL PROTECTED]|留意舊有的預設變數 (<varname>$HTTP_*_VARS</varname>) 仍然存在。 - + 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 + <link linkend="reserved.variables">Reserved Predefined Variables</link>. + Also, you'll notice how the older predefined variables + (<varname>$HTTP_*_VARS</varname>) still exist. + &avail.register-long-arrays; </simpara> <note> - <title>可變變數</title> + <title>Variable variables</title> <para> - Superglobals 不能當作<link linkend="language.variables.variable">可變變數</link>來使用。 + Superglobals cannot be used as + <link linkend="language.variables.variable">variable variables</link>. </para> </note> <para> - 如果某些 <link linkend="ini.variables-order">variables_order</link> 中的變數沒有被設定,它們相對的 PHP 預設陳列也將會是空的。 + If certain variables in <link + linkend="ini.variables-order">variables_order</link> are not set, their + appropriate PHP predefined arrays are also left empty. </para> <variablelist id="language.variables.superglobals"> - <title>PHP 的 Superglobals</title> + <title>PHP Superglobals</title> <varlistentry> <term><link linkend="reserved.variables.globals">$GLOBALS</link></term> <listitem> <simpara> - [EMAIL PROTECTED]<varname>$GLOBALS</varname> 自 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. + <varname>$GLOBALS</varname> has existed since PHP 3. </simpara> </listitem> </varlistentry> @@ -135,7 +202,10 @@ <term><link linkend="reserved.variables.server">$_SERVER</link></term> <listitem> <simpara> - 由網頁伺服器設定的變數或目前的程式所執行的環境產生的變數。其性質類似舊有的 <varname>$HTTP_SERVER_VARS</varname> 陳列,雖然目前還可以使用,但不被建議。 + Variables set by the web server or otherwise directly related + to the execution environment of the current script. Analogous + to the old <varname>$HTTP_SERVER_VARS</varname> array (which is + still available, but deprecated). </simpara> </listitem> </varlistentry> @@ -143,7 +213,9 @@ <term><link linkend="reserved.variables.get">$_GET</link></term> <listitem> <simpara> - 通過 HTTP GET 提供給程式的變數。其性質類似舊有的 <varname>$HTTP_GET_VARS</varname> 陳列,雖然目前還可以使用,但不被建議。 + Variables provided to the script via HTTP GET. Analogous to the + old <varname>$HTTP_GET_VARS</varname> array (which is still + available, but deprecated). </simpara> </listitem> </varlistentry> @@ -151,7 +223,9 @@ <term><link linkend="reserved.variables.post">$_POST</link></term> <listitem> <simpara> - 通過 HTTP POST 提供給程式的變數。其性質類似舊有的 <varname>$HTTP_POST_VARS</varname>陳列,雖然目前還可以使用,但不被建議。 + Variables provided to the script via HTTP POST. Analogous to the + old <varname>$HTTP_POST_VARS</varname> array (which is still + available, but deprecated). </simpara> </listitem> </varlistentry> @@ -159,7 +233,9 @@ <term><link linkend="reserved.variables.cookies">$_COOKIE</link></term> <listitem> <simpara> - 通過 HTTP cookies 提供給程式的變數。其性質類似舊有的 <varname>$HTTP_COOKIE_VARS</varname>陳列,雖然目前還可以使用,但不被建議。 + Variables provided to the script via HTTP cookies. Analogous to + the old <varname>$HTTP_COOKIE_VARS</varname> array (which is + still available, but deprecated). </simpara> </listitem> </varlistentry> @@ -167,7 +243,12 @@ <term><link linkend="reserved.variables.files">$_FILES</link></term> <listitem> <simpara> - 通過 HTTP POST 檔案上傳提供給程式的變數。其性質類似舊有的 <varname>$HTTP_POST_FILES</varname>陳列,雖然目前還可以使用,但不被建議。詳情請參閱 <link linkend="features.file-upload.post-method">POST 上傳方式</link>。 + Variables provided to the script via HTTP post file + uploads. Analogous to the old + <varname>$HTTP_POST_FILES</varname> array (which is still + available, but deprecated). See <link + linkend="features.file-upload.post-method">POST method + uploads</link> for more information. </simpara> </listitem> </varlistentry> @@ -175,7 +256,9 @@ <term><link linkend="reserved.variables.environment">$_ENV</link></term> <listitem> <simpara> - 執行環境提供給程式的變數。其性質類似舊有的 <varname>$HTTP_ENV_VARS</varname>陳列,雖然目前還可以使用,但不被建議。 + Variables provided to the script via the environment. Analogous + to the old <varname>$HTTP_ENV_VARS</varname> array (which is + still available, but deprecated). </simpara> </listitem> </varlistentry> @@ -183,11 +266,20 @@ <term><link linkend="reserved.variables.request">$_REQUEST</link></term> <listitem> <simpara> - 通過任何使用者輸入途徑而提供給程式的變數,也正因如此不值得信任。此陳列中變數的存在與否及次序是決定於 <link linkend="ini.variables-order">variables_order</link> 的設定。PHP 4.1.0 之前的版本並沒有類似此陳列的變數。請同時參閱 <function>import_request_variables</function>。 + 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 <link + linkend="ini.variables-order">variables_order</link> + configuration directive. This array has no direct analogue in + versions of PHP prior to 4.1.0. See also + <function>import_request_variables</function>. </simpara> <note> <simpara> - 在<link linkend="features.commandline">指令行</link>模式下執行時,它並<emphasis>不</emphasis>包含 <varname>argv</varname> 和 <varname>argc</varname> 項目。此兩項目只在 <varname>$_SERVER</varname> 陳列中出現。 + When running on the <link linkend="features.commandline">command line + </link>, this will <emphasis>not</emphasis> include the + <varname>argv</varname> and <varname>argc</varname> entries; these are + present in the <varname>$_SERVER</varname> array. </simpara> </note> </listitem> @@ -196,7 +288,12 @@ <term><link linkend="reserved.variables.session">$_SESSION</link></term> <listitem> <simpara> - 目前和程式注冊了的 session 變數。其性質類似舊有的 <varname>$HTTP_SESSION_VARS</varname>陳列,雖然目前還可以使用,但不被建議。詳情請參閱 <link linkend="ref.session">Session 處理函數</link>[EMAIL PROTECTED] + Variables which are currently registered to a script's + session. Analogous to the old + <varname>$HTTP_SESSION_VARS</varname> array (which is still + available, but deprecated). See the <link + linkend="ref.session">Session handling functions</link> section + for more information. </simpara> </listitem> </varlistentry> @@ -206,10 +303,13 @@ <sect1 id="language.variables.scope"> - <title>變數範圍</title> + <title>Variable scope</title> <simpara> - 變數的範圍只是在其所定義的空間內存在。在大部份的情況下,PHP [EMAIL PROTECTED]@[EMAIL PROTECTED] 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: </simpara> <informalexample> <programlisting role="php"> @@ -222,7 +322,11 @@ </programlisting> </informalexample> <simpara> - 例子中,變數 <varname>$a</varname> 也存在于被包含入的程式 <filename>b.inc</filename> [EMAIL PROTECTED] + Here the <varname>$a</varname> variable will be available within + the included <filename>b.inc</filename> 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: </simpara> <informalexample> @@ -243,7 +347,16 @@ </informalexample> <simpara> - 這個程式不會輸出任何東西,因為 echo 述句使用了本區域版本的 <varname>$a</varname> [EMAIL PROTECTED]|發現這和 C 語言的做法不同,因為 C 的全域變數是自動的提供給各函數,除非在本域定義中指明撤銷。此做法可能會[EMAIL PROTECTED] PHP 中,全域變數在本域使用前必須先宣佈為全域。例子: + This script will not produce any output because the echo statement + refers to a local version of the <varname>$a</varname> 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: </simpara> <informalexample> @@ -268,11 +381,17 @@ </informalexample> <simpara> - 上述例子將會輸出 "3"。在函數內宣佈 <varname>$a</varname> 及 <varname>$b</varname> 為全域後,所有涉及該兩個變數的使用將自動指向全域的版本。PHP [EMAIL PROTECTED] + The above script will output "3". By declaring + <varname>$a</varname> and <varname>$b</varname> 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. </simpara> <simpara> - [EMAIL PROTECTED] PHP 特別定義的陳列:<varname>$GLOBALS</varname>。前面的例子可以重寫為: + A second way to access variables from the global scope is to use + the special PHP-defined <varname>$GLOBALS</varname> array. The + previous example can be rewritten as: </simpara> <informalexample> @@ -295,7 +414,13 @@ </informalexample> <simpara> - <varname>$GLOBALS</varname> [EMAIL PROTECTED] (associative array),全域變數的名即為索引鍵,而該變數的內容即為陳列的值。有否留意到 <varname>$GLOBALS</varname> [EMAIL PROTECTED] $GLOBALS [EMAIL PROTECTED]<link linkend="language.variables.superglobals">superglobal</link>[EMAIL PROTECTED] superglobals 強大功能的例子: + The <varname>$GLOBALS</varname> 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 <varname>$GLOBALS</varname> exists in any scope, this + is because $GLOBALS is a <link + linkend="language.variables.superglobals">superglobal</link>. + Here's an example demonstrating the power of superglobals: </simpara> <para> @@ -323,7 +448,11 @@ </para> <simpara> - [EMAIL PROTECTED]<emphasis>靜態</emphasis>變數。靜態變數只在本域函數範圍內存在,但是當程式執行離開此範圍時,它並不會喪失它的值。看看下面的例子: + Another important feature of variable scoping is the + <emphasis>static</emphasis> 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: </simpara> <informalexample> @@ -342,7 +471,13 @@ </informalexample> <simpara> - [EMAIL PROTECTED] <varname>$a</varname> 設為 <literal>0</literal> 然後列印出 "0"。<varname>$a</varname>++ 增加了 <varname>$a</varname> [EMAIL PROTECTED] <varname>$a</varname> [EMAIL PROTECTED]|丟失當前計數的計數函數,我們可以將 <varname>$a</varname>宣佈為靜態: + This function is quite useless since every time it is called it + sets <varname>$a</varname> to <literal>0</literal> and prints + "0". The <varname>$a</varname>++ which increments the + variable serves no purpose since as soon as the function exits the + <varname>$a</varname> variable disappears. To make a useful + counting function which will not lose track of the current count, + the <varname>$a</varname> variable is declared static: </simpara> <informalexample> @@ -361,11 +496,18 @@ </informalexample> <simpara> - 現在,每當函數 Test() 被呼叫時,它會列印出 <varname>$a</varname> [EMAIL PROTECTED] + Now, every time the Test() function is called it will print the + value of <varname>$a</varname> and increment it. </simpara> <simpara> - [EMAIL PROTECTED]@種呼叫自己的函數。編寫遞迴函數時必須留意,因為若編寫錯誤,它有可能會無定限地遞迴。您必須確定足夠的方式來終止遞迴。下列簡單的函數將遞迴地數到 10,利用靜態變數 <varname>$count</varname> 來斷定什麼時候停止: + 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 <varname>$count</varname> to know when to stop: </simpara> <informalexample> @@ -389,7 +531,12 @@ </informalexample> <simpara> - 驅動 <literal>PHP 4</literal> 的 Zend Engine 1 是以參照的方式來實現 <literal>static</literal> 和 <literal>global</literal> [EMAIL PROTECTED] <literal>global</literal> [EMAIL PROTECTED]@[EMAIL PROTECTED] + The Zend Engine 1, driving <literal>PHP4</literal>, implements the + <literal>static</literal> and <literal>global</literal> modifier for + variables in terms of references. For example, a true global variable + imported inside a function scope with the <literal>global</literal> + statement actually creates a reference to the global variable. This can + lead to unexpected behaviour which the following example addresses: </simpara> <informalexample> @@ -416,7 +563,7 @@ </informalexample> <simpara> - 執行這個例子將會導致下列的輸出: + Executing this example will result in the following output: </simpara> <screen> @@ -426,7 +573,8 @@ </screen> <simpara> - <literal>static</literal> 陳述式也會導致同樣的輸出。參照並沒有被靜態地儲存: + A similar behaviour applies to the <literal>static</literal> statement. + References are not stored statically: </simpara> <informalexample> @@ -470,7 +618,7 @@ </informalexample> <simpara> - 執行此例子將導致下列的輸出: + Executing this example will result in the following output: </simpara> <screen> @@ -485,17 +633,21 @@ </screen> <simpara> - [EMAIL PROTECTED]@個靜態變數後,當您第二次呼叫 <literal>&get_instance_ref()</literal> 函數時,它是<emphasis>不會</emphasis>記住之前的值的。 + This example demonstrates that when assigning a reference to a static + variable, it's not <emphasis>remembered</emphasis> when you call the + <literal>&get_instance_ref()</literal> function a second time. </simpara> </sect1> <sect1 id="language.variables.variable"> - <title>可變變數</title> + <title>Variable variables</title> <simpara> - 有時候,可以使用可變變數會[EMAIL PROTECTED]@[EMAIL PROTECTED] + 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: </simpara> <informalexample> @@ -509,7 +661,10 @@ </informalexample> <simpara> - [EMAIL PROTECTED]@[EMAIL PROTECTED] $ 在 <emphasis>hello</emphasis> [EMAIL PROTECTED]@個變數的名來使用了。即: + A variable variable takes the value of a variable and treats that + as the name of a variable. In the above example, + <emphasis>hello</emphasis>, can be used as the name of a variable + by using two dollar signs. i.e. </simpara> <informalexample> @@ -523,7 +678,10 @@ </informalexample> <simpara> - 這時,有兩個變數被定義然後儲存在 PHP 的符號表中:含有 "hello" 的 <varname>$a</varname> 及含有 "world" 的 <varname>$hello</varname>。所以,此敘述: + At this point two variables have been defined and stored in the + PHP symbol tree: <varname>$a</varname> with contents "hello" and + <varname>$hello</varname> with contents "world". Therefore, this + statement: </simpara> <informalexample> @@ -537,7 +695,7 @@ </informalexample> <simpara> - 與下列這句有著同樣的輸出: + produces the exact same output as: </simpara> <informalexample> @@ -551,34 +709,48 @@ </informalexample> <simpara> - 即,它們都輸出:<computeroutput>hello world</computeroutput>。 + i.e. they both produce: <computeroutput>hello world</computeroutput>. </simpara> <simpara> - [EMAIL PROTECTED] <varname>$$a[1]</varname>,那剖析器必須知道您是想當 <varname>$a[1]</varname> 是變數來使用,還是當 <varname>$$a</varname> 是變數,然後指向那個變數的第 [1] [EMAIL PROTECTED] <varname>${$a[1]}</varname>、第二個情況使用 <varname>${$a}[1]</varname>。 + In order to use variable variables with arrays, you have to + resolve an ambiguity problem. That is, if you write + <varname>$$a[1]</varname> then the parser needs to know if you + meant to use <varname>$a[1]</varname> as a variable, or if you + wanted <varname>$$a</varname> as the variable and then the [1] + index from that variable. The syntax for resolving this ambiguity + is: <varname>${$a[1]}</varname> for the first case and + <varname>${$a}[1]</varname> for the second. </simpara> <warning> <simpara> - 必須留意的是,可變變數是不能使用於 PHP 的 <link linkend="language.variables.superglobals">Superglobal 陳列</link>。這表示您不能使用 <varname>${$_GET}</varname> 這樣的敘述。如果您真的想使用 superglobals 和舊的 <varname>HTTP_*_VARS</varname>,您可以嘗試以<link linkend="language.references">參照</link>的方式使用。 + Please note that variable variables cannot be used with PHP's + <link linkend="language.variables.superglobals">Superglobal arrays</link>. + This means you cannot do things like <varname>${$_GET}</varname>. If you are + looking for a way to handle availability of superglobals and the old + <varname>HTTP_*_VARS</varname>, you might want to try + <link linkend="language.references">referencing</link> them. </simpara> </warning> </sect1> <sect1 id="language.variables.external"> - <title>來自 PHP 以外的變數</title> + <title>Variables from outside PHP</title> <sect2 id="language.variables.external.form"> - <title>HTML 表單 (GET 和POST)</title> + <title>HTML Forms (GET and POST)</title> <simpara> - [EMAIL PROTECTED] 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: </simpara> <para> <example> - <title>[EMAIL PROTECTED] HTML 表單</title> + <title>A simple HTML form</title> <programlisting role="html"> <![CDATA[ <form action="foo.php" method="post"> @@ -592,12 +764,13 @@ </para> <para> - 根據您的安裝方式和個人偏好,PHP 提供多種方式存取 HTML [EMAIL PROTECTED] + Depending on your particular setup and personal preferences, there + are many ways to access data from your HTML forms. Some examples are: </para> <para> <example> - <title>[EMAIL PROTECTED] POST 方式傳送的簡單 HTML 表單</title> + <title>Accessing data from a simple POST HTML form</title> <programlisting role="html"> <![CDATA[ <?php @@ -609,7 +782,7 @@ import_request_variables('p', 'p_'); print $p_username; -// Available since PHP 3. As of PHP 5.0.0, these long predefined +// Available since PHP 3. As of PHP 5.0.0, these long predefined // variables can be disabled with the register_long_arrays directive. print $HTTP_POST_VARS['username']; @@ -625,33 +798,55 @@ </example> </para> <para> - 使用 GET 方式傳送的表單用法類似,只不過使用合適的先前定義的函數。GET 也可使用在查詢字串 (QUERY_STRING) 〈URL 中問號 '?' 後面的資訊〉。例如,<literal>http://www.example.com/test.php?id=3</literal> 含有以 <varname>$_GET['id']</varname> 方式存取的 GET 資料。請參考 <link linkend="reserved.variables.request">$_REQUEST</link> 和 <function>import_request_variables()</function>。 + 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, <literal>http://www.example.com/test.php?id=3</literal> + contains GET data which is accessible with <varname>$_GET['id']</varname>. + See also <link linkend="reserved.variables.request">$_REQUEST</link> and + <function>import_request_variables</function>. </para> <note> <para> - <link linkend="language.variables.superglobals">Superglobal 陳列</link>如 <varname>$_POST</varname> 及 <varname>$_GET</varname> 在 PHP 版本 4.1.0 以上才開始提供。 + <link linkend="language.variables.superglobals">Superglobal arrays</link>, + like <varname>$_POST</varname> and <varname>$_GET</varname>, became + available in PHP 4.1.0 </para> </note> <para> - 上面提到,在 PHP 4.2.0 之前的版本中,<link linkend="ini.register-globals">register_globals</link> 的預設值是 <emphasis>on</emphasis>。而在 PHP 3 中,它永遠是 on [EMAIL PROTECTED] <emphasis>off</emphasis> 來設計您的程式。 + As shown, before PHP 4.2.0 the default value for <link + linkend="ini.register-globals">register_globals</link> + was <emphasis>on</emphasis>. 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 <emphasis>off</emphasis> and code + accordingly. </para> <note> <para> - <link linkend="ini.magic-quotes-gpc">magic_quotes_gpc</link> 組態指令會影響到 Get,Post 和 Cookie 的值。如果開啟此指令,(It's "PHP!") 的值將變成 (It\'s \"PHP!\")[EMAIL PROTECTED] <function>addslashes</function>,<function>stripslashes</function> 和 <link linkend="ini.magic-quotes-sybase">magic_quotes_sybase</link>。 + The <link linkend="ini.magic-quotes-gpc">magic_quotes_gpc</link> + 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 + <function>addslashes</function>, <function>stripslashes</function> and + <link linkend="ini.magic-quotes-sybase">magic_quotes_sybase</link>. </para> </note> <simpara> - PHP 也懂得處理陳列式的表單變數〈請參觀相關的 <link linkend="faq.html">FAQ</link>〉。您可以將相關的變數歸類,或用此功能來取回多元選擇輸入表單的值。在下列範例中,我們將表單張貼到它自己,並在傳送後把資料顯示出來: - + PHP also understands arrays in the context of form variables + (see the <link linkend="faq.html">related faq</link>). 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: </simpara> <para> <example> - <title>更複雜的表單變數</title> + <title>More complex form variables</title> <programlisting role="php"> <![CDATA[ <?php @@ -685,14 +880,16 @@ </para> <para> - PHP 3 [EMAIL PROTECTED] PHP 4 則沒有此限制。 + In PHP 3, the array form variable usage is limited to + single-dimensional arrays. In PHP 4, no such restriction applies. </para> <sect3 id="language.variables.external.form.submit"> - <title>IMAGE SUBMIT 變數名稱</title> + <title>IMAGE SUBMIT variable names</title> <simpara> - 在傳送表單時,我們可以使用下列標籤將標準的提交鍵以圖像來取代: + When submitting a form, it is possible to use an image instead + of the standard submit button with a tag like: </simpara> <informalexample> @@ -704,7 +901,13 @@ </informalexample> <simpara> - [EMAIL PROTECTED]@下,伴隨的表單就會傳送到伺服器,隨其而來還附加了兩個變數,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. </simpara> </sect3> @@ -714,12 +917,24 @@ <title>HTTP Cookies</title> <simpara> - PHP 絕對支援 <ulink - url="&spec.cookies;">Netscape規格</ulink>的 HTTP cookies。Cookies 是用來將資料儲存在用戶端的瀏覽器中,以便日後追蹤或識別返回的使用者。您可以用 <function>setcookie()</function> 函數來設定 cookies。Cookies 是 HTTP header [EMAIL PROTECTED] SetCookie [EMAIL PROTECTED] <function>header()</function> 函數有著相同的限定。Cookie 資料可以在 <varname>$_COOKIE</varname>,<varname>$HTTP_COOKIE_VARS</varname> 及 <varname>$_REQUEST</varname> 陳列中供使用。詳情和範例請參考手冊的 <function>setcookie()</function> 部份。 + PHP transparently supports HTTP cookies as defined by <ulink + url="&spec.cookies;">Netscape's Spec</ulink>. Cookies are a + mechanism for storing data in the remote browser and thus + tracking or identifying return users. You can set cookies using + the <function>setcookie</function> 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 <function>header</function> function. Cookie data + is then available in the appropriate cookie data arrays, such + as <varname>$_COOKIE</varname>, <varname>$HTTP_COOKIE_VARS</varname> + as well as in <varname>$_REQUEST</varname>. See the + <function>setcookie</function> manual page for more details and + examples. </simpara> <simpara> - [EMAIL PROTECTED] cookie 變數,那您就要使用陳列來指派了。範例: + If you wish to assign multiple values to a single cookie variable, you + may assign it as an array. For example: </simpara> <informalexample> @@ -734,15 +949,21 @@ </informalexample> <simpara> - 這將會建立兩個個別的 cookies,儘管現在 [EMAIL PROTECTED]@個 cookie 建立多個值,那您可先對那些值使用 <function>serialize()</function> 或 <function>explode()</function> 函數。 + 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 <function>serialize</function> or + <function>explode</function> on the value first. </simpara> <simpara> - [EMAIL PROTECTED]@個 cookie 將會[EMAIL PROTECTED] cookie,除非該 cookie 的路徑[EMAIL PROTECTED] + 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. </simpara> <example> - <title>[EMAIL PROTECTED] <function>setcookie()</function> 的範例</title> + <title>A <function>setcookie</function> example</title> <programlisting role="php"> <![CDATA[ <?php @@ -757,11 +978,13 @@ </sect2> <sect2 id="language.variables.external.dot-in-names"> - <title>在變數名字中的句號 (dots)</title> + <title>Dots in incoming variable names</title> <para> - [EMAIL PROTECTED] 並不會更改傳入程式的變數名稱。然而,您必須留意句號 (.) [EMAIL PROTECTED] 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: <programlisting role="php"> <![CDATA[ <?php @@ -769,20 +992,34 @@ ?> ]]> </programlisting> - [EMAIL PROTECTED] <varname>$varname</varname> 的變數,跟隨著字串連結運算子 (.)[EMAIL PROTECTED]|帶來您想要的結果。 + Now, what the parser sees is a variable named + <varname>$varname</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. </para> <para> - 正因如此,PHP 會自動的將傳入的變數名稱中的句號 (.) 改為底線 (_)。 + For this reason, it is important to note that PHP will + automatically replace any dots in incoming variable names with + underscores. </para> </sect2> <sect2 id="language.variables.determining-type-of"> - <title>判定變數的類型</title> + <title>Determining variable types</title> <para> - 由於 PHP 會[EMAIL PROTECTED] 提供了幾個判定變數類型的函數:<function>gettype()</function>、<function>is_array()</function>、<function>is_float()</function>、<function>is_int()</function>、<function>is_object()</function> 及 <function>is_string()</function>。請參閱<link linkend="language.types">類型</link>[EMAIL PROTECTED] + 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: + <function>gettype</function>, <function>is_array</function>, + <function>is_float</function>, <function>is_int</function>, + <function>is_object</function>, and + <function>is_string</function>. See also the chapter on + <link linkend="language.types">Types</link>. </para> </sect2> @@ -809,4 +1046,4 @@ 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 +-->
-- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php