cvs: phpdoc /en/language variables.xml

From: Date: Sat, 07 Jun 2003 17:40:42 +0000
Subject: cvs: phpdoc /en/language variables.xml
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969354333@lists.php.net to get a copy of this message
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>¦UÃþÅܼÆ</title> + <title>Variables</title> <sect1 id="language.variables.basics"> - <title>°ò¥»ª¾ÃÑ</title> + <title>Basics</title> <simpara> - ©Ò¦³ PHP ªºÅܼƳ£¬O¥H "$" ¬°¶}ºÝ¡A«á¥[ÅܼƦW©Ò²Õ¦¨ªº¡CÅܼƦWºÙ¬O¦³¤j¤p¼g¤§¤Àªº¡C + 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 ªºÅܼƦWºÙ©M¨ä¥L¼ÐÅÒ¦³µÛ¬Û¦Pªº³W«h¡C¤@­Ó¦³®ÄªºÅܼƬO¥Ñ­^¤å¦r¥À©Î©³½u¶}©l¡A¸òµÛ¬O¥ô·Nªøµuªº¦r¥À¡B¼Æ¦r©Î©³½u¡C«ö¥¿³W¹Bºâ¦¡¡A¥¦¥i¥Hªí¹F¬°¡G'[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> - ¦r¥À¬° a-z¡AA-Z¡A¦Ó ASCII ¦r²Å«h±q 127 ¦Ü 255¡q0x7f-0xff¡r¡C + 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 ¤¤¡AÅܼÆÁ`¬O¥Ñ­È«ü¬£¡C¤]´N¬O»¡¡A·í±z±N¤@­Ó¹Bºâ¦¡ªº­È«ü¬£µ¹¤@­ÓÅܼƮɡA¾ã­Ó­ì©l¹Bºâ¦¡ªº­È±N·|½Æ»s¨ì¥ØªºÅܼơC¨Ò¦p¡A·í±N¤@­ÓÅܼƪº­È«ü¬£µ¹¥t¤@­ÓÅܼƤ§«á¡A§ó§ï·í¤¤ªº¥ô¦ó¤@­ÓÅܼƪº­È¨Ã¤£·|¼vÅT¨ì¥t¤@­ÓÅܼƪº­È¡C·Q­nª¾¹D§ó¦h³oÃþªº«ü¬£¡A½Ð°Ñ¾\<link linkend="language.expressions">¹Bºâ¦¡</link>³o¤@³¹¡C + 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 «h´£¨Ñ¤F¥t¤@ºØ«ü¬£ÅܼƭȪº¤èªk¡G<link linkend="language.references">¶Ç§}«ü¬£</link>¡A§Y·sÅܼƥu¬O°Ñ·Ó­ì¨ÓÅܼƤ¤ªº­È¡A©Î»¡·sªºÅܼƦ¨¬°­ì¨ÓÅܼƪº§O¦W¡C¦¹®É¡A§ó§ï¨â­ÓÅܼƤ¤¥ô¦ó¤@­Óªº­È³£·|¦P®É§ó§ï¥t¤@­Óªº­È¡C¥Ñ©ó¨S¦³°õ¦æ¹ê»Úªº½Æ»s¤u§@¡A«ü¬£ªº¾Þ§@¤]¦]¦¹§ó¥[§Ö±¶¡C¤£¹L¡A¥u¦³¦bºò±Kªº°j°é©Î«ü¬£¤j¶qªº<link linkend="language.types.array">°}¦C</link>©Î<link linkend="language.types.object">ª«¥ó</link>®É¤~·|¹îı³t«×ªº´£¤É¡C + 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> - ¨Ï¥Î¶Ç§}«ü¬£¡A¥u¶·¦b³Q°Ñ·Óªº­ìÅܼƫe¥[¤W¤@­Ó (&amp;) ªº²Å¸¹¡C¨Ò¦p¡A¤U¦Cµ{¦¡½X¦b¨Ï¥Î¶Ç§}«ü¬£«á¿é¥X 'My name is Bob' ¨â¦¸¡C - + To assign by reference, simply prepend an ampersand (&amp;) 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> - »Ý­nª`·Nªº¬O¥u¦³¯u¥¿©R¦WªºÅܼƤ~¥i¥H¥Î¨Ó¶i¦æ¶Ç§}«ü¬£¡C + 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>¹w³]ªºÅܼÆ</title> + <title>Predefined variables</title> <simpara> - PHP ¬°¥¦°õ¦æªºµ{¦¡´£¨Ñ¤F¤j¶qªº¹w³]ÅܼơCµM¦Ó¡A¥Ñ¤_¦³³\¦hÅܼƦ]¨Ì¿à¨ä©Ò°õ¦æªº¦øªA¾¹ºØÃþ¡Bª©¥»¡B¦w¸Ëµ¥±¡ªp¦Ó©w¡A¦]¦¹Ãø¥H¦³§¹¾ãªº³Q°O¸ü¤U¨Ó¡C§ó¦³¤@¨ÇÅܼƦb<link linkend="features.commandline">«ü¥O¦æ</link>ªº¼Ò¦¡¹B¦æ®É¬O¤£¯à¨Ï¥Îªº¡C¦³Ãö³o¨ÇÅܼƪº¦Cªí¡A½Ð°Ñ¦Ò<link linkend="reserved.variables">«O¯dªº¥ý«e©w¸qÅܼÆ</link>¤@¸`¡C + 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 ¥H«áªºª©¥»¡A<link linkend="ini.register-globals">register_globals</link> «ü¥Oªº¹w³]­È¬O <emphasis>off</emphasis>¡C³o¬O¤@­Ó­«¤jªº§ïÅÜ¡C±N register_globals ªº­È¹w³]¬° <emphasis>off</emphasis> ·|¼vÅT¨ì¹w³]ÅܼƦb¥þ°ì½d³ò¤ºªº¦³®Ä©Ê¡C¨Ò¦p¡A­n±o¨ì <varname>DOCUMENT_ROOT</varname> ªº­È¡AÀ³¨Ï¥Î <varname>$_SERVER['DOCUMENT_ROOT']</varname> ¦Ó¤£¬O <varname>$DOCUMENT_ROOT</varname>¡B¨Ï¥Î <varname>$_GET['id']</varname> ¥H¨ú±o URL <literal>http://www.example.com/test.php?id=3</literal> ¤¤ id ªº­È¡A¦Ó¤£¬O¥Î <varname>$id</varname>¡B©Î¨Ï¥Î <varname>$_ENV['HOME']</varname> ¨Ó¨ú¥N <varname>$HOME</varname>¡C + 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> - §ó¦h»P¦¹§ó§ï¬ÛÃöªº¸ê®Æ¥i¦b <link linkend="ini.register-globals">register_globals</link> ªº³]¸m¤@¸`¡B<link linkend="security.registerglobals">¨Ï¥Î Register Globals</link> ¤¤ªº¦w¥þ³¹¡A©Î¦b <ulink url="&url.php.release4.1.0;">4.1.0</ulink> ¤Î <ulink url="&url.php.release4.2.0;">4.2.0</ulink> ªºµo¦æ³q§i¤¤¨ú±o¡C + 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> - §Ú­Ì«ØÄ³±z¨Ï¥Î PHP ´£¨Ñªº«O¯d¹w³]ÅܼơA¦p <link linkend="language.variables.superglobals">superglobal ³¯¦C</link>¡C + 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 ª©¥»°_¡APHP ÃB¥~´£¨Ñ¤F¤@®M¹w³]°}¦C¡A·í¤¤¥]§t¤F¨Ó¦Û¦øªA¾¹¡q¦p¾A¥Î¡r¡BÀô¹Ò¡B¤Î¨Ï¥ÎªÌ¿é¤JªºÅܼơC³o¨Ç·s³¯¦C¤ñ¸û¯S§O¡A¦]¦¹¥¦­Ì·|¦Û°Ê¦a³]¬°¥þ°ì¡A§Y¥i¥H¦b©Ò¦³½d³ò¤º¨Ï¥Î¡C¥¿¦]¦p¦¹¡A¥¦­Ì¤]³QºÙ¬° 'autoglobals' ©Î 'superglobals'¡C¡qPHP ©|¥¼¦³¨Ï¥ÎªÌ¦Û¦æ©w¸q superglobals ªº¥\¯à¡C¡r§Ú­Ì±N¦b¤U­±¦C¥X superglobals¡A¦ý­nª¾¹D¥¦­Ìªº¤º®e©M PHP ¹w³]Åܼƪº²`¤J°Q½×¡A½Ð°Ñ¦Ò<link linkend="reserved.variables">«O¯dªº¥ý«e©w¸qÅܼÆ</link>¤@¸`¡C¦¹¥~¡A±z¤]·|¯d·N¦³ªº¹w³]ÅÜ¼Æ (<varname>$HTTP_*_VARS</varname>) ¤´µM¦s¦b¡C - + 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>¥iÅÜÅܼÆ</title> + <title>Variable variables</title> <para> - Superglobals ¤£¯à·í§@<link linkend="language.variables.variable">¥iÅÜÅܼÆ</link>¨Ó¨Ï¥Î¡C + Superglobals cannot be used as + <link linkend="language.variables.variable">variable variables</link>. </para> </note> <para> - ¦pªG¬Y¨Ç <link linkend="ini.variables-order">variables_order</link> ¤¤ªºÅܼƨS¦³³Q³]©w¡A¥¦­Ì¬Û¹ïªº PHP ¹w³]³¯¦C¤]±N·|¬OªÅªº¡C + 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> - ¥]§t¤@­Ó«ü¦Vµ{¦¡¥þ°ì½d³ò¤º¥i¥ÎÅܼƪº¯Á¤Þ¡C¦¹³¯¦Cªº¯Á¤ÞÁ䬰¥þ°ìÅܼƪº¦WºÙ¡C<varname>$GLOBALS</varname> ¦Û PHP 3 ¶}©l´N¤w¦s¦b¡C + 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> - ¥Ñºô­¶¦øªA¾¹³]©wªºÅܼƩΥثeªºµ{¦¡©Ò°õ¦æªºÀô¹Ò²£¥ÍªºÅܼơC¨ä©Ê½èÃþ¦ü¦³ªº <varname>$HTTP_SERVER_VARS</varname> ³¯¦C¡AÁöµM¥Ø«eÁÙ¥i¥H¨Ï¥Î¡A¦ý¤£³Q«ØÄ³¡C + 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> - ³q¹L HTTP GET ´£¨Ñµ¹µ{¦¡ªºÅܼơC¨ä©Ê½èÃþ¦ü¦³ªº <varname>$HTTP_GET_VARS</varname> ³¯¦C¡AÁöµM¥Ø«eÁÙ¥i¥H¨Ï¥Î¡A¦ý¤£³Q«ØÄ³¡C + 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> - ³q¹L HTTP POST ´£¨Ñµ¹µ{¦¡ªºÅܼơC¨ä©Ê½èÃþ¦ü¦³ªº <varname>$HTTP_POST_VARS</varname>³¯¦C¡AÁöµM¥Ø«eÁÙ¥i¥H¨Ï¥Î¡A¦ý¤£³Q«ØÄ³¡C + 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> - ³q¹L HTTP cookies ´£¨Ñµ¹µ{¦¡ªºÅܼơC¨ä©Ê½èÃþ¦ü¦³ªº <varname>$HTTP_COOKIE_VARS</varname>³¯¦C¡AÁöµM¥Ø«eÁÙ¥i¥H¨Ï¥Î¡A¦ý¤£³Q«ØÄ³¡C + 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> - ³q¹L HTTP POST ÀɮפW¶Ç´£¨Ñµ¹µ{¦¡ªºÅܼơC¨ä©Ê½èÃþ¦ü¦³ªº <varname>$HTTP_POST_FILES</varname>³¯¦C¡AÁöµM¥Ø«eÁÙ¥i¥H¨Ï¥Î¡A¦ý¤£³Q«ØÄ³¡C¸Ô±¡½Ð°Ñ¾\ <link linkend="features.file-upload.post-method">POST ¤W¶Ç¤è¦¡</link>¡C + 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> - °õ¦æÀô¹Ò´£¨Ñµ¹µ{¦¡ªºÅܼơC¨ä©Ê½èÃþ¦ü¦³ªº <varname>$HTTP_ENV_VARS</varname>³¯¦C¡AÁöµM¥Ø«eÁÙ¥i¥H¨Ï¥Î¡A¦ý¤£³Q«ØÄ³¡C + 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> - ³q¹L¥ô¦ó¨Ï¥ÎªÌ¿é¤J³~®|¦Ó´£¨Ñµ¹µ{¦¡ªºÅܼơA¤]¥¿¦]¦p¦¹¤£­È±o«H¥ô¡C¦¹³¯¦C¤¤Åܼƪº¦s¦b»P§_¤Î¦¸§Ç¬O¨M©w©ó <link linkend="ini.variables-order">variables_order</link> ªº³]©w¡CPHP 4.1.0 ¤§«eªºª©¥»¨Ã¨S¦³Ãþ¦ü¦¹³¯¦CªºÅܼơC½Ð¦P®É°Ñ¾\ <function>import_request_variables</function>¡C + 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> - ¦b<link linkend="features.commandline">«ü¥O¦æ</link>¼Ò¦¡¤U°õ¦æ®É¡A¥¦¨Ã<emphasis>¤£</emphasis>¥]§t <varname>argv</varname> ©M <varname>argc</varname> ¶µ¥Ø¡C¦¹¨â¶µ¥Ø¥u¦b <varname>$_SERVER</varname> ³¯¦C¤¤¥X²{¡C + 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> - ¥Ø«e©Mµ{¦¡ª`¥U¤Fªº session ÅܼơC¨ä©Ê½èÃþ¦ü¦³ªº <varname>$HTTP_SESSION_VARS</varname>³¯¦C¡AÁöµM¥Ø«eÁÙ¥i¥H¨Ï¥Î¡A¦ý¤£³Q«ØÄ³¡C¸Ô±¡½Ð°Ñ¾\ <link linkend="ref.session">Session ³B²z¨ç¼Æ</link>¤@¸`¡C + 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>Åܼƽd³ò</title> + <title>Variable scope</title> <simpara> - Åܼƪº½d³ò¥u¬O¦b¨ä©Ò©w¸qªºªÅ¶¡¤º¦s¦b¡C¦b¤j³¡¥÷ªº±¡ªp¤U¡APHP ªºÅܼƥu¦³¤@­Ó³æ¤@ªº½d³ò¡C³o³æ¤@ªº½d³ò¤]¥]§t¤F¥H include ©M require ¤è¦¡¤Þ¤JªºÀɮסC¨Ò¦p¡G + 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> - ¨Ò¤l¤¤¡AÅÜ¼Æ <varname>$a</varname> ¤]¦s¦b¤_³Q¥]§t¤Jªºµ{¦¡ <filename>b.inc</filename> ¤¤¡C¦ý¬O¦b¨Ï¥ÎªÌ¦Û­qªº¨ç¼Æ¤¤¡A¤@­Ó°Ï°ì©Êªº¨ç¼Æ½d³ò±N³Q¤Þ¥Î¡C¥ô¦ó¦b¨ç¼Æ¤º¨Ï¥ÎªºÅܼƦb¹w³]ªº±¡ªp¤U¥u§½­­©ó¸Ó¨ç¼Æªº½d³ò¤º¡C¨Ò¦p¡G + 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> - ³o­Óµ{¦¡¤£·|¿é¥X¥ô¦óªF¦è¡A¦]¬° echo ­z¥y¨Ï¥Î¤F¥»°Ï°ìª©¥»ªº <varname>$a</varname> ÅܼơA¦Ó¸Ó¥»°Ï°ìÅܼƨå¼´¿³Q¤À°t¤@­Ó­È¡C§A·|µo²{³o©M C »y¨¥ªº°µªk¤£¦P¡A¦]¬° C ªº¥þ°ìÅܼƬO¦Û°Êªº´£¨Ñµ¹¦U¨ç¼Æ¡A°£«D¦b¥»°ì©w¸q¤¤«ü©úºM¾P¡C¦¹°µªk¥i¯à·|¤Þ­P¤@¨Ç°ÝÃD¡A¦p¨Ï¥ÎªÌ¤£·V¦a§ó§ï¤F¥þ°ìÅܼƪº­È¡C¦b PHP ¤¤¡A¥þ°ìÅܼƦb¥»°ì¨Ï¥Î«e¥²¶·¥ý«Å§G¬°¥þ°ì¡C¨Ò¤l¡G + 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> - ¤W­z¨Ò¤l±N·|¿é¥X &quot;3&quot;¡C¦b¨ç¼Æ¤º«Å§G <varname>$a</varname> ¤Î <varname>$b</varname> ¬°¥þ°ì«á¡A©Ò¦³¯A¤Î¸Ó¨â­ÓÅܼƪº¨Ï¥Î±N¦Û°Ê«ü¦V¥þ°ìªºª©¥»¡CPHP ¨S¦³­­©w¤@­Ó¨ç¼Æ¥i¥H¨Ï¥Îªº¥þ°ìÅܼƪº¼Æ¶q¡C + The above script will output &quot;3&quot;. 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> - ²Ä¤GºØ¦s¨ú¥þ°ì½d³ò¤¤ªºÅܼƤèªk¬O¨Ï¥Î¤@­Ó¥Ñ PHP ¯S§O©w¸qªº³¯¦C¡G<varname>$GLOBALS</varname>¡C«e­±ªº¨Ò¤l¥i¥H­«¼g¬°¡G + 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> ¬O¤@­ÓÃöÁp³¯¦C (associative array)¡A¥þ°ìÅܼƪº¦W§Y¬°¯Á¤ÞÁä¡A¦Ó¸ÓÅܼƪº¤º®e§Y¬°³¯¦Cªº­È¡C¦³§_¯d·N¨ì <varname>$GLOBALS</varname> ¦b¥ô¦ó¤@­Ó½d³ò³£¥X²{¡H¨º¬O¦]¬° $GLOBALS ¬O¤@­Ó<link linkend="language.variables.superglobals">superglobal</link>¡C³o¬O¤@­Ó¥Ü½d superglobals ±j¤j¥\¯àªº¨Ò¤l¡G + 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> - Åܼƽd³òªº¥t¤@­Ó­«­n¥\¯à¬°<emphasis>ÀRºA</emphasis>ÅܼơCÀRºAÅܼƥu¦b¥»°ì¨ç¼Æ½d³ò¤º¦s¦b¡A¦ý¬O·íµ{¦¡°õ¦æÂ÷¶}¦¹½d³ò®É¡A¥¦¨Ã¤£·|³à¥¢¥¦ªº­È¡C¬Ý¬Ý¤U­±ªº¨Ò¤l¡G + 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> - ¦¹¨ç¼Æ¨S¦³¤°»ò¥Î³B¡A¦]¨C¤@¦¸°õ¦æ¥¦®É¡A¥¦±N <varname>$a</varname> ³]¬° <literal>0</literal> µM«á¦C¦L¥X &quot;0&quot;¡C<varname>$a</varname>++ ¼W¥[¤F <varname>$a</varname> ªº­È¡A¦ý¨Ã¨S¦³¹ê»Ú¥Î³~¦]¬°¤@¥¹Â÷¶}¤F¸Ó¨ç¼Æ¡AÅÜ¼Æ <varname>$a</varname> ¤]ÀH¤§®ø¥¢¡C­n³]­p¤@­Ó¦³¥Îªº¡B¤£·|¥á¥¢·í«e­p¼Æªº­p¼Æ¨ç¼Æ¡A§Ú­Ì¥i¥H±N <varname>$a</varname>«Å§G¬°ÀRºA¡G + This function is quite useless since every time it is called it + sets <varname>$a</varname> to <literal>0</literal> and prints + &quot;0&quot;. 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> - ²{¦b¡A¨C·í¨ç¼Æ Test() ³Q©I¥s®É¡A¥¦·|¦C¦L¥X <varname>$a</varname> ªº­È¡AµM«á¥[¤W¤@¡C + Now, every time the Test() function is called it will print the + value of <varname>$a</varname> and increment it. </simpara> <simpara> - ÀRºAÅܼƤ]´£¨Ñ¤@ºØ³B²z»¼°j¨ç¼Æªº¤èªk¡C»¼°j¨ç¼Æ¬O¤@ºØ©I¥s¦Û¤vªº¨ç¼Æ¡C½s¼g»¼°j¨ç¼Æ®É¥²¶·¯d·N¡A¦]¬°­Y½s¼g¿ù»~¡A¥¦¦³¥i¯à·|µL©w­­¦a»¼°j¡C±z¥²¶·½T©w¨¬°÷ªº¤è¦¡¨Ó²×¤î»¼°j¡C¤U¦C²³æªº¨ç¼Æ±N»¼°j¦a¼Æ¨ì 10¡A§Q¥ÎÀRºAÅÜ¼Æ <varname>$count</varname> ¨ÓÂ_©w¤°»ò®É­Ô°±¤î¡G + 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> - ÅX°Ê <literal>PHP 4</literal> ªº Zend Engine 1 ¬O¥H°Ñ·Óªº¤è¦¡¨Ó¹ê²{ <literal>static</literal> ©M <literal>global</literal> ªº¡C¨Ò¦p¡A¤@­Ó¯u¥¿ªº¥þ°ìÅܼƨϥΠ<literal>global</literal> ¤è¦¡¤Þ¶i¤@­Ó¨ç¼Æ½d³ò®É¹ê»Ú¤W´N¬O«Ø¥ß¤F¤@­Ó¥þ°ìÅܼƪº°Ñ·Ó¡C³o±N¾É­P¤@¨Ç·N·Q¤£¨ìªº¦æ¬°¡A¥¿¦p¤U¦C¨Ò¤l©Ò­z¡G + 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> - °õ¦æ³o­Ó¨Ò¤l±N·|¾É­P¤U¦Cªº¿é¥X¡G + Executing this example will result in the following output: </simpara> <screen> @@ -426,7 +573,8 @@ </screen> <simpara> - <literal>static</literal> ³¯­z¦¡¤]·|¾É­P¦P¼Ëªº¿é¥X¡C°Ñ·Ó¨Ã¨S¦³³QÀRºA¦aÀx¦s¡G + A similar behaviour applies to the <literal>static</literal> statement. + References are not stored statically: </simpara> <informalexample> @@ -470,7 +618,7 @@ </informalexample> <simpara> - °õ¦æ¦¹¨Ò¤l±N¾É­P¤U¦Cªº¿é¥X¡G + Executing this example will result in the following output: </simpara> <screen> @@ -485,17 +633,21 @@ </screen> <simpara> - ¤W­z¨Ò¤l¥Ü½d¤F¦b«ü¬£¤@­Ó°Ñ·Óµ¹¤©¤@­ÓÀRºAÅܼƫá¡A·í±z²Ä¤G¦¸©I¥s <literal>&amp;get_instance_ref()</literal> ¨ç¼Æ®É¡A¥¦¬O<emphasis>¤£·|</emphasis>°O¦í¤§«eªº­Èªº¡C + This example demonstrates that when assigning a reference to a static + variable, it's not <emphasis>remembered</emphasis> when you call the + <literal>&amp;get_instance_ref()</literal> function a second time. </simpara> </sect1> <sect1 id="language.variables.variable"> - <title>¥iÅÜÅܼÆ</title> + <title>Variable variables</title> <simpara> - ¦³®É­Ô¡A¥i¥H¨Ï¥Î¥iÅÜÅܼƷ|¥O¨ì¤u§@§ó¥[¤è«K¡C¤]´N¬O´N¡A¤@­ÓÅܼƪº¦W¥i¥H³Q°ÊºAªº³]©w©M¨Ï¥Î¡C¤@­Ó´¶³qªºÅܼƬO¥H¤U¦Cªº¤èªk³]©w¡G + 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> - ¤@­Ó¥iÅÜÅܼƬO¥H¥t¤@­ÓÅܼƪº­È§@¬°¥¦ªºÅܼƦW¡C¦b¤W­z¨Ò¤l¤¤¡A¥u­n¥[¨â­Ó $ ¦b <emphasis>hello</emphasis> ªº«e­±´N¥i§â¥¦·í§@¬O¤@­ÓÅܼƪº¦W¨Ó¨Ï¥Î¤F¡C§Y¡G + 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> - ³o®É¡A¦³¨â­ÓÅܼƳQ©w¸qµM«áÀx¦s¦b PHP ªº²Å¸¹ªí¤¤¡G§t¦³ "hello" ªº <varname>$a</varname> ¤Î§t¦³ "world" ªº <varname>$hello</varname>¡C©Ò¥H¡A¦¹±Ô­z¡G + 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> - »P¤U¦C³o¥y¦³µÛ¦P¼Ëªº¿é¥X¡G + produces the exact same output as: </simpara> <informalexample> @@ -551,34 +709,48 @@ </informalexample> <simpara> - §Y¡A¥¦­Ì³£¿é¥X¡G<computeroutput>hello world</computeroutput>¡C + i.e. they both produce: <computeroutput>hello world</computeroutput>. </simpara> <simpara> - ¬°¤F­n¦b³¯¦C¤¤¨Ï¥Î¥iÅÜÅܼơA±z¥²¶·­º¥ý¸Ñ¨M¤@­Ó¼Ò½kªº°ÝÃD¡C¤ñ¤è»¡¡A¦pªG±z¨Ï¥Î <varname>$$a[1]</varname>¡A¨º­åªR¾¹¥²¶·ª¾¹D±z¬O·Q·í <varname>$a[1]</varname> ¬OÅܼƨӨϥΡAÁÙ¬O·í <varname>$$a</varname> ¬OÅܼơAµM«á«ü¦V¨º­ÓÅܼƪº²Ä [1] ­Ó¯Á¤Þ¡C¸Ñ¨M¤W­z¼Ò½k°ÝÃDªº»yªk¬O¡G²Ä¤@­Ó±¡ªp¨Ï¥Î <varname>${$a[1]}</varname>¡B²Ä¤G­Ó±¡ªp¨Ï¥Î <varname>${$a}[1]</varname>¡C + 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> - ¥²¶·¯d·Nªº¬O¡A¥iÅÜÅܼƬO¤£¯à¨Ï¥Î©ó PHP ªº <link linkend="language.variables.superglobals">Superglobal ³¯¦C</link>¡C³oªí¥Ü±z¤£¯à¨Ï¥Î <varname>${$_GET}</varname> ³o¼Ëªº±Ô­z¡C¦pªG±z¯uªº·Q¨Ï¥Î superglobals ©Mªº <varname>HTTP_*_VARS</varname>¡A±z¥i¥H¹Á¸Õ¥H<link linkend="language.references">°Ñ·Ó</link>ªº¤è¦¡¨Ï¥Î¡C + 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 ¥H¥~ªºÅܼÆ</title> + <title>Variables from outside PHP</title> <sect2 id="language.variables.external.form"> - <title>HTML ªí³æ (GET ©MPOST)</title> + <title>HTML Forms (GET and POST)</title> <simpara> - ·í¤@¥÷ªí³æ¶Ç°eµ¹ PHP µ{¦¡®É¡Aªí³æ¤ºªº¸ê°T±N¦Û°Ê´£¨Ñµ¹¸Óµ{¦¡¡C§Ú­Ì¦³«Ü¦h¤èªk¦s¨ú³o¨Ç¸ê°T¡A½d¨Ò¡G + 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>¤@­Ó²³æªº HTML ªí³æ</title> + <title>A simple HTML form</title> <programlisting role="html"> <![CDATA[ <form action="foo.php" method="post"> @@ -592,12 +764,13 @@ </para> <para> - ®Ú¾Ú±zªº¦w¸Ë¤è¦¡©M­Ó¤H°¾¦n¡APHP ´£¨Ñ¦hºØ¤è¦¡¦s¨ú HTML ªí³æ¤¤ªº¸ê®Æ¡C¤@¨Ç¨Ò¤l¦p¡G + 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>¦s¨ú¤@­Ó¥H POST ¤è¦¡¶Ç°eªºÂ²³æ 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 ¤è¦¡¶Ç°eªºªí³æ¥ÎªkÃþ¦ü¡A¥u¤£¹L¨Ï¥Î¦X¾Aªº¥ý«e©w¸qªº¨ç¼Æ¡CGET ¤]¥i¨Ï¥Î¦b¬d¸ß¦r¦ê (QUERY_STRING) ¡qURL ¤¤°Ý¸¹ '?' «á­±ªº¸ê°T¡r¡C¨Ò¦p¡A<literal>http://www.example.com/test.php?id=3</literal> §t¦³¥H <varname>$_GET['id']</varname> ¤è¦¡¦s¨úªº GET ¸ê®Æ¡C½Ð°Ñ¦Ò <link linkend="reserved.variables.request">$_REQUEST</link> ©M <function>import_request_variables()</function>¡C + 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 ³¯¦C</link>¦p <varname>$_POST</varname> ¤Î <varname>$_GET</varname> ¦b PHP ª©¥» 4.1.0 ¥H¤W¤~¶}©l´£¨Ñ¡C + <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> - ¤W­±´£¨ì¡A¦b PHP 4.2.0 ¤§«eªºª©¥»¤¤¡A<link linkend="ini.register-globals">register_globals</link> ªº¹w³]­È¬O <emphasis>on</emphasis>¡C¦Ó¦b PHP 3 ¤¤¡A¥¦¥Ã»·¬O on ªº¡C§Ú­Ì«ØÄ³©Ò¦³¨Ï¥ÎªÌ¤£­n¨Ì¿à¦¹«ü¥O¡A¨Ã¦b½s¼g®É±N¥¦·í§@¬O <emphasis>off</emphasis> ¨Ó³]­p±zªºµ{¦¡¡C + 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> ²ÕºA«ü¥O·|¼vÅT¨ì Get¡APost ©M Cookie ªº­È¡C¦pªG¶}±Ò¦¹«ü¥O¡A(It's "PHP!") ªº­È±NÅܦ¨ (It\'s \"PHP!\")¡C¦¹¸õ¥X¤u§@¦b±N¦r¦ê´¡¤J¸ê®Æ®w«e¬O¥²¶·­n°µªº¡C½Ð°Ñ¦Ò <function>addslashes</function>¡A<function>stripslashes</function> ©M <link linkend="ini.magic-quotes-sybase">magic_quotes_sybase</link>¡C + 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 ¤]À´±o³B²z³¯¦C¦¡ªºªí³æÅܼơq½Ð°ÑÆ[¬ÛÃöªº <link linkend="faq.html">FAQ</link>¡r¡C±z¥i¥H±N¬ÛÃöªºÅܼÆÂkÃþ¡A©Î¥Î¦¹¥\¯à¨Ó¨ú¦^¦h¤¸¿ï¾Ü¿é¤Jªí³æªº­È¡C¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì±Nªí³æ±i¶K¨ì¥¦¦Û¤v¡A¨Ã¦b¶Ç°e«á§â¸ê®ÆÅã¥Ü¥X¨Ó¡G - + 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 ªº³¯¦Cªí³æÅܼƥu¥i¥H¨Ï¥Î¤@ºû³¯¦C¡A¦Ó PHP 4 «h¨S¦³¦¹­­¨î¡C + 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 ÅܼƦWºÙ</title> + <title>IMAGE SUBMIT variable names</title> <simpara> - ¦b¶Ç°eªí³æ®É¡A§Ú­Ì¥i¥H¨Ï¥Î¤U¦C¼ÐÅÒ±N¼Ð·Çªº´£¥æÁä¥H¹Ï¹³¨Ó¨ú¥N¡G + 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> - ·í¨Ï¥ÎªÌ¦b¹Ï¹³ªº¥ô¦ó¤@³B«ö¤@¤U¡A¦ñÀHªºªí³æ´N·|¶Ç°e¨ì¦øªA¾¹¡AÀH¨ä¦Ó¨ÓÁÙªþ¥[¤F¨â­ÓÅܼơAsub_x ©M sub_y¡C³o¥]§t¤F¨Ï¥ÎªÌ«ö¦b¹Ï¹³ªº®y¼Ð¡C¦³¸gÅ窺¨Ï¥ÎªÌ©Î³\ª¾¹D¥ÑÂsÄý¾¹¶Ç°e¨ÓªºÅܼƦWºÙÀ³¸Ó¥]§t¥y¸¹¡A¦Ó¤£¬O©³½u¡A¦ý PHP ¦Û°Ê¦a±N¥y¸¹Âà´«¦¨¬°©³½u¡C + 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³W®æ</ulink>ªº HTTP cookies¡CCookies ¬O¥Î¨Ó±N¸ê®ÆÀx¦s¦b¥Î¤áºÝªºÂsÄý¾¹¤¤¡A¥H«K¤é«á°lÂÜ©ÎÃѧOªð¦^ªº¨Ï¥ÎªÌ¡C±z¥i¥H¥Î <function>setcookie()</function> ¨ç¼Æ¨Ó³]©w cookies¡CCookies ¬O HTTP header ªº¤@³¡¥÷¡A¦]¦¹ SetCookie ¨ç¼Æ¤@©w­n¦b¥ô¦ó¸ê®Æ¶Ç°e¥hÂsÄý¾¹«e©I¥s¡C³o©M <function>header()</function> ¨ç¼Æ¦³µÛ¬Û¦Pªº­­©w¡CCookie ¸ê®Æ¥i¥H¦b <varname>$_COOKIE</varname>¡A<varname>$HTTP_COOKIE_VARS</varname> ¤Î <varname>$_REQUEST</varname> ³¯¦C¤¤¨Ñ¨Ï¥Î¡C¸Ô±¡©M½d¨Ò½Ð°Ñ¦Ò¤â¥Uªº <function>setcookie()</function> ³¡¥÷¡C + 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> - ­Y±z­n±N¦h­Ó­È«ü¬£µ¹¤@­Ó cookie ÅܼơA¨º±z´N­n¨Ï¥Î³¯¦C¨Ó«ü¬£¤F¡C½d¨Ò¡G + 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> - ³o±N·|«Ø¥ß¨â­Ó­Ó§Oªº cookies¡A¾¨ºÞ²{¦b MyCookie¦b±zªºµ{¦¡¤¤¬O¤@­Ó³æ¤¸³¯¦C¡C¦pªG±z­n¥H¤@­Ó cookie «Ø¥ß¦h­Ó­È¡A¨º±z¥i¥ý¹ï¨º¨Ç­È¨Ï¥Î <function>serialize()</function> ©Î <function>explode()</function> ¨ç¼Æ¡C + 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> - ¦³¤@ÂI¥²¶·¯d·Nªº¬O¡A¤@­Ó cookie ±N·|´À¥NÂsÄý¾¹¤¤¥t¤@­Ó¦P¦Wªº cookie¡A°£«D¸Ó cookie ªº¸ô®|©Îºô°ì¦WºÙ¤£¦P¡C©Ò¥H¡A¦b³]­pÁʪ«¨®µ{¦¡®É¡A±z³Ì¦n«O«ù¤@­Ó­p¼Æ¾¹¡A¨Ã±N¦¹¼Æ¾Ú¶Ç»¼¤U¥h¡A§Y¡G + 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>¤@­Ó <function>setcookie()</function> ªº½d¨Ò</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>¦bÅܼƦW¦r¤¤ªº¥y¸¹ (dots)</title> + <title>Dots in incoming variable names</title> <para> - ¤@¯ë¤W¡APHP ¨Ã¤£·|§ó§ï¶Ç¤Jµ{¦¡ªºÅܼƦWºÙ¡CµM¦Ó¡A±z¥²¶·¯d·N¥y¸¹ (.) ¨Ã¤£¬O¤@­Ó¦³®Äªº PHP ÅܼƦWºÙ¦r¤¸¡A¥¿¦p¡G - + 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> - ³o®É¡A­åªR¾¹¨£¨ìªº¥u¬O¤@­Ó¥s°µ <varname>$varname</varname> ªºÅܼơA¸òÀHµÛ¦r¦ê³sµ²¹Bºâ¤l (.)¡A¦A¨Ó¬O¤@­ÓµL¤Þ¸¹ªº¦r¦ê¡Gext¡CÅãµM¦a¡A³o¨Ã¤£·|±a¨Ó±z·Q­nªºµ²ªG¡C + 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> - ¥¿¦]¦p¦¹¡APHP ·|¦Û°Êªº±N¶Ç¤JªºÅܼƦWºÙ¤¤ªº¥y¸¹ (.) §ï¬°©³½u (_)¡C + 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>§P©wÅܼƪºÃþ«¬</title> + <title>Determining variable types</title> <para> - ¥Ñ©ó PHP ·|¦Û¦æ§PÂ_ÅܼƪºÃþ«¬¡A¨Ã«ö»Ý­nÂàÅÜ¥¦­ÌªºÃþ«¬¡A¦]¦¹¦³®É¤]Ãø¥H©úÅã¦a§P©w¤@­ÓÅܼƦb¬Y­Ó®É¨èªºÃþ«¬¡C¦³Å²©ó¦¹¡APHP ´£¨Ñ¤F´X­Ó§P©wÅܼÆÃþ«¬ªº¨ç¼Æ¡G<function>gettype()</function>¡B<function>is_array()</function>¡B<function>is_float()</function>¡B<function>is_int()</function>¡B<function>is_object()</function> ¤Î <function>is_string()</function>¡C½Ð°Ñ¾\<link linkend="language.types">Ãþ«¬</link>¤@³¹¡C + 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 +-->
« previous php.doc (#969354333) next »