glace Sun Apr 8 03:13:57 2001 EDT Added files: /phpdoc/hk/language basic-syntax.xml constants.xml control-structures.xml expressions.xml functions.xml oop.xml operators.xml references.xml types.xml variables.xml Log: use xml
Index: phpdoc/hk/language/basic-syntax.xml +++ phpdoc/hk/language/basic-syntax.xml <chapter id="language.basic-syntax"> <title>°ò¥»¥yªk</title> <sect1 id="language.basic-syntax.phpmode"> <title>¶i¤J PHP »y¨¥¼Ò¦¡</title> <para> ¦b HTML ¤å¥ó¤¤´¡¤J PHP ªº«ü¥O¦@¦³¥|ºØ¤èªk: </para> <para> <example> <title>Ways of escaping from HTML</title> <programlisting> 1. <? echo ("this is the simplest, an SGML processing instruction\n"); ?> 2. <?php echo("if you want to serve XHTML or XML documents, do like this\n"); ?> 3. <script language="php"> echo ("some editors (like FrontPage) don't like processing instructions"); </script> 4. <% echo ("You may optionally use ASP-style tags"); %> <%= $variable; # This is a shortcut for "<%echo .." %> </programlisting> </example> </para> <para> ¤W±¦C¥Xªº¤èªk¤¤²Ä¤@ºØ¥u·í PHP ³Q³]©w¦¨±µ¨ü "µu¼ÐÅÒ" ®É¤~·|¦³®Ä¡C ³]©w¤èªk¥i¥H <!-- via the <function>short_tags</function> function,-->¦b½Õ³]¤å¥ó PHP.INI ¤¤§â <link linkend="ini.short-open-tag">short_open_tag</link> ³]¬°¯uÈ¡A ©ÎªÌ¦b½sĶ PHP ®É°õ¦æ <command>configure</command> ®É¥[¤J --enable-short-tags ³oÓ¿ï¶µ¡C </para> <para> The second way is the generally preferred method, as it allows for the next generation of XHTML to be easily implemented with PHP. </para> <para> ²Ä¥|ºØ¤èªk¤]¥u¦³·í ASP §ÎºAªº¼ÐÅÒ ³Q¤¹³\®É¤~¦³®Ä¡C ³o»Ýn½Õ³] <link linkend="ini.asp-tags">asp_tags</link> ³oӰѼơC <note> <para>¹ï ASP-style ¼ÐÅÒªº¤ä´©±q PHP ²Ä 3.0.4 ª©¶}©l.</para> </note></para> <para> µ{¦¡¬qªºµ²§ô¼Ð°O±N·|¥]¬A¸òÀH¦b¨ä«áªº´«¦æ²Å¸¹¡C (°²¦p¦³´«¦æ²Å¸¹ªº¸Ü¡C) </para></sect1> <sect1 id="language.basic-syntax.instruction-separation"> <title>¤À¹j«ü¥O</title> <simpara> PHP ¦U«ü¥O¦æ¤§¶¡ªº¤À¹j¤èªk©M C¡B PERL ¬O¤@¼Ëªº¡C - ¨C¦æ«ü¥O¶¡¥Î¤À¸¹ ";" ¤À¶}¡C </simpara> <para> ¥Ñ©óµ{¦¡¬qµ²§ô¼ÐÅÒ¦P¼Ë¥Nªí«ü¥Oµ²§ô¡A ¦]¦¹¥H¤Uªº¨â¬qµ{¦¡¬O®ÄªG¬O¤@¼Ëªº¡C <informalexample> <programlisting> <?php echo "This is a test"; ?> <?php echo "This is a test" ?> </programlisting> </informalexample></para></sect1> <sect1 id="language.basic-syntax.comments"> <title>¥[ª`ÄÀªº¤èªk</title> <para> PHP ¤W¥i¥H¥Î C¡B C++¡B ©M Unix shell ªºª`ÄÀ¤èªk¡C ¦bª`ÄÀ¸¹¤ºªº¤º®e±N³Q PHP ¸ÑÄÀ¾¹©¿²¤¡C ¤ñ¦p»¡ : <informalexample><programlisting> <?php echo "This is a test"; // This is a one-line c++ style comment /* This is a multi line comment yet another line of comment */ echo "This is yet another test"; echo "One Final Test"; # This is shell-style style comment ?> </programlisting> </informalexample></para> <simpara> ¦Ü©ó¨ººØ¥uª`¸Ñ¤@¦æªºª`ÄÀ²Å¸¹ªº§@¥Î½d³ò¥u·|¦b¥»¦æ¤º¦³®Ä¡C ( ¦p¥»¦æ¤¤¦P®É¥X²{ PHP µ{üF¬qµ²§ô²Å¸¹ "?>" «h¦b²Å¸¹«áªº¤å¦r±N¤£·|³Q©¿²¤¡C)</simpara> <informalexample><programlisting> <h1>This is an <?php # echo "simple";?> example.</h1> <p>The header above will say 'This is an example'. </programlisting></informalexample> <simpara> ¦pªG¬O C ªº®æ¦¡¨Ó°µª`ÄÀ¡A ª`·N¤£n¹³¤U¨Ò¯ë¦bª`ÄÀ¤¤¦A¥[ª`ÄÀ¡A §_«h·|³y¦¨¿ù»~¡C </simpara> <informalexample><programlisting> <?php /* echo "This is a test"; /* This comment will cause a problem */ */ ?> </programlisting></informalexample></sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/hk/language/constants.xml +++ phpdoc/hk/language/constants.xml <chapter id="language.constants"> <title>±`¼Æ</title> <simpara> PHP ¦Û¤v´£¨Ñ¤F¤@¨Ç±`¼Æ¡A ¤]¤¹³\µ{¦¡°õ¦æ®É¥t¥~¦A©w¸q¦UºØ±`¼Æ¡C±`¼Æ©MÅܼƫܬۦü¡A ¥¦Ì¤§¶¡¥u¦³¨âÓ¤À§O¡G º¥ý±`¥²¶·¥Î <function>define</function> ¨ç¼Æ¨Ó©w¸q¡A ¥t¥~´N¬O¤@¥¹©w¸q«á±`¼ÆªºÈ¤£¥i§ïÅÜ¡C </simpara> <para> ¨t²Î¤º¹w¥ý©w¸qªº±`¼Æ¥]¬A: <variablelist> <varlistentry> <term>__FILE__</term> <listitem> <simpara> Àx¦s¤F¥Ø«e¥¿³Q°õ¦æªº¤å¥óªº¦W¦r¡C ¦pªG¥Î¦b³Q include() ©Î require() ªº¤å¥ó¤§¤¤¡A «hÀx¦s¤F¥À¤å¥óªº¦W¦r¦Ó«D³Q include() ©Î reuqire() ¤å¥óªº¦W¦r¡C </simpara> </listitem> </varlistentry> <varlistentry> <term>__LINE__</term> <listitem> <simpara> ²{¦b¥¿¦b³Q¤ÀªR/¸ÑªRªº«ü¥O¦æ¡C ¦pªG«ü¥O¬O¦b³Q include() ©Î require() ªº¤å¥ó¤¤¡A «hÀx¦s«ü¥O¦b¸Ó¤l¤å¥ó¤¤ªº¦æ¸¹¡C </simpara> </listitem> </varlistentry> <varlistentry> <term>PHP_VERSION</term> <listitem> <simpara> ¥H¤å¥yªº«¬ºAÀx¦s¤F²{®É PHP ªºª©¥»¡C ¨Ò¦p '3.0.8-dev'. </simpara> </listitem> </varlistentry> <varlistentry> <term>PHP_OS</term> <listitem> <simpara> ¹B¦æ PHP ªº§@·~¨t²Î¡A ¦p 'Linux'. </simpara> </listitem> </varlistentry> <varlistentry> <term>TRUE</term> <listitem> <simpara> ¥NªíÅÞ¿èȯu/¬O¡C </simpara> </listitem> </varlistentry> <varlistentry> <term>FALSE</term> <listitem> <simpara> ¥NªíÅÞ¿èȰ²/«D¡C </simpara> </listitem> </varlistentry> <varlistentry> <term>E_ERROR</term> <listitem> <simpara> ¥Nªí¥X²{¤F¨Ã«D¸ÑªR¤Wªº¿ù»~¨Ã¤ÞPµ{¦¡µLªkÄ~Äò°õ¦æ¤U¥h¡C </simpara> </listitem> </varlistentry> <varlistentry> <term>E_WARNING</term> <listitem> <simpara> ¥Nªí¥X²{¤Fĵ¥ü¦ý PHP ·|Ä~Äò°õ¦æµ{¦¡¡C ³o¼ËªºÄµ¥ü¥i¥Îµ{¦¡¤¤®·®»¡CÃþ¦üªº¨Ò¤l¦p <function>ereg</function> ¤¤ªí¹F¦¡§Ë¿ù¤F¡C </simpara> </listitem> </varlistentry> <varlistentry> <term>E_PARSE</term> <listitem> <simpara> ¸ÑªR¾¹¦b¤ÀªR«ü¥O¦æ®Éµo²{¤F¿ù»~¡A ¦b³o±¡ªp¤UµLªk¦^´_µ{¦¡ªº°õ¦æ¡C </simpara> </listitem> </varlistentry> <varlistentry> <term>E_NOTICE</term> <listitem> <simpara> ¥Nªí¦³¨Ç¨Æ¤£¹ï«l¡A ¥i¯à¬Oµo¥Í¤F¿ù»~¤]¥i¯à¤£¬O¡C ³oÃþ°ÝÃDªº¨Ò¤l¦³: ¥ÎÃöÁp¦r¯Á¤Þ¨ú¥Î°}¦C®É¨S¦³§âÃöÁp¦r¥Î¤Þ¸¹¬A°_¨Ó¡B ¨S¦³«ü©wÅܼƪº¨ú¥ÎÅܼơE¡E¡E </simpara> </listitem> </varlistentry> <varlistentry> <term>E_ALL</term> <listitem> <simpara> ?? </simpara> </listitem> </varlistentry> </variablelist> </para> <para> ³o¨Ç¥Ñ E_ ¥´ÀYªº±`¼Æ³q±`¬O©M <function>error_reporting</function>¨ç¼Æ°t¦X¥Î¨Ó½Õ¾ã error reporting level ¡A ´N¬O»¡½Õ³]¦³þ¨Ç¿ù»~·|³Q³B²z¡A ¦³þ¨Ç·|³Q©¿²¤¡C </para> <para> ¦pn©w¸q¦Û¤vªº±`¼Æ¡A ½Ð¥Î <function>define</function> ¨ç¼Æ¡C </para> <para> ¯d¤ß³o³£¬O±`¼Æ¦Ó¤£¬O C ·®æªº¥¨¶°¡G ±`¼Æ¥u¯àÀx¦s¦Xªkªº¼Æ¾Ú¡C <example> <title>³]©w ±`¼Æ</title> <programlisting> <?php define("CONSTANT", "Hello world."); echo CONSTANT; // outputs "Hello world." ?> </programlisting> </example> <example> <title>__FILE__ ©M __LINE__ ªº¥Îªk </title> <programlisting> <?php function report_error($file, $line, $message) { echo "An error occured in $file on line $line: $message."; } report_error(__FILE__,__LINE__, "Something went wrong!"); ?> </programlisting> </example></para> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/hk/language/control-structures.xml +++ phpdoc/hk/language/control-structures.xml <chapter id="control-structures"> <title>±±¨îµ{¦¡ªº¬yµ{</title> <simpara> PHP µ{¦¡¥þ³£¬O¥Ñ¤@¥y¥yªº»y¥y©Òºc¦¨¡C »y¥y¥i¥H«ü¬£ÅܼƪºÈ¡B ½Õ¥Î¨ç¼Æ¡B °õ¦æ°j°é¡B «ö±ø¥ó¤À¬yµ{¦¡¬Æ¦Ü¤°»ò¤]¤£°µ(ªÅ¥Õ»y¥y)¡C ¤@¥y»y¥y³q±`¥H ';' ¤À¸¹§@µ²§À¡C §Q¥Îªá¬A¸¹ '{ }'¥i¥H§â¦n¦h±ø»y¥y²Õ¦¨¤@²Õ¡A ³o¼Ë²Õ¦¨ªº»y²Õ¥¦¦Û¤v¤]¬O¤@¥y»y¥y¡C ¥»¸`·|¤¶²Ð¦U¦¡¦U¼Ëªº»y¥yÃþ«¬¡C </simpara> <sect1 id="control-structures.if"> <title><literal>if</literal></title> <para> if ¬Oµ{¦¡¤¤»y¨¥¤¤·¥¨ä«nªº²Õ¦¨³¡¤À¡A ¹ï PHP ¤£¤]¤£¨Ò¥~¡C ¥¦´£¨Ñ¤F«ö»Ýn°õ¦æ¤£¦Pµ{¦¡¬qªº¥\¯à¡C PHP ©Ò¥Îªº if µ²ºc©M C ©Ò¥Îªº¨S¤°»ò¤£¦P¡G <informalexample> <programlisting> if (expr) statement </programlisting> </informalexample> </para> <simpara> if ¬Oµ{¦¡¤¤»y¨¥¤¤·¥¨ä«nªº²Õ¦¨³¡¤À¡A ¹ï PHP ¤£¤]¤£¨Ò¥~¡C ¥¦´£¨Ñ¤F«ö»Ýn°õ¦æ¤£¦Pµ{¦¡¬qªº¥\¯à¡C PHP ©Ò¥Îªº if µ²ºc©M C ©Ò¥Îªº¨S¤°»ò¤£¦P¡G </simpara> <para> ¥H¤U½d¨Ò·|¥´¥X a is bigger than b ¦pªG $a ªºÈ¤ñ $bªº¤j: <informalexample> <programlisting role="php"> if ($a > $b) print "a is bigger than b"; </programlisting> </informalexample> </para> <para> ³q±`¦b¨M©w¤F±ø¥ó«á§A¥i¯àn°õ¦æ¦h¹L¤@¥y«ü¥O¡A ·íµM§A¤£¥²¨CÓ«ü¥O«e³£¥Î¤@¦¸ if ¡A §A¥u»Ýn§â³o¨Çn°õ¦æªº«ü¥O¥Î '{ }' ²Õ¦¨»y²Õ´N¦æ¤F¡C ¦n¹³¥H¤Uªºµ{¦¡¬q·|Åã¥Ü¥X a is bigger than b ¦pªG $a ¤ñ $b ¤j¡A ¦P®É·|§â $a ªºÈ§Û¨ì $b¤¤: <informalexample> <programlisting role="php"> if ($a > $b) { print "a is bigger than b"; $b = $a; } </programlisting> </informalexample> </para> <simpara> If ¥i¥H¦b§Oªº if ¤§¤º¥X²{¡A ¹³³o¼Ëªº±¡ªpºÙ¬°±_ª¬ªº if¡A PHP ¤¹³\µL¦hªº±_ª¬µ²ºc¡C ¦p¦¹§A¦b½sµ{¦¡¦³«Ü¤jªº¦Û¥Ñ«×¨Ó±±¨îµ{¦¡ªº¬yµ{¡C </simpara> </sect1> <sect1 id="control-structures.else"> <title><literal>else</literal></title> <para> ¦³®ÉÔ§A·|¦³¨â²Õ«ü¥O¡A ¤@²Õ¦b±ø¥ó¦¨¥ß®É°õ¦æ¡A ¥t¤@²Õ¦b±ø¥ó¤£¦¨¥ß®É°õ¦æ¡C ³o®É¥Î else ´N¤è«K«Ü¦h¤F¡C else ¸É¥R if ªº¥\¯à¡G ¦pªG if »y¥y¤¤ªºªí¹F¦¡¤£¦¨¥ß®É¡A if ¤£·|°õ¦æºò¸òªº«ü¥O²Õ¡A ¦Ó·|°õ¦æ else ¤§«áªº«ü¥O²Õ¡C ¹³¤U±µ{¦¡·|¥´¥X a is bigger than b ¦pªG $a ¤j¹L $b¡A ¤Ï¤§«h¥´¥X a is NOT bigger than b : <informalexample> <programlisting role="php"> if ($a > $b) { print "a is bigger than b"; } else { print "a is NOT bigger than b"; } </programlisting> </informalexample> else ¤¤ªº«ü¥O²Õ¥u·í if ªí¹F¦¡ºtºâªºµ²ªG¬O°²ªº®ÉÔ¤~·|³Q°õ¦æ¡C¦pªGÁÙ¦³§Oªº elseif ªí¹F¦¡ªº¸Ü¡A ³o¨Çªí¹F¦¡ªºµ²ªG¤]n¬O°²¤~¦æ¡C(¬Ý¤U±) </para> </sect1> <sect1 id="control-structures.elseif"> <title><literal>elseif</literal></title> <para> elseif¡A ¤@¬Ý´Nª¾¹D¬Oif ©M elseªº²V¦XÅé¡C Like else¤@¼Ë¡A ¥¦¸É¥R¤F if »y¥yªº¥\¯à¡G ¥¦¦b if ªº±ø¥ó¤£¦¨¥ß®É¤~·|°õ¦æ¡C¦ý©M else¤£¤@¼Ëªº¬O¡A elseif °£¤Fn¨D if ªº±ø¥ó¤£¦¨¥ß¡A ¥¦¦P®Én¨D¦Û¤vªº±ø¥ó¦¨¥ß¡A§_«h¥¦¤£·|°õ¦æ¸òµÛªºµ{¦¡¬q¡C ¤U¦C¥Ü½d¨Ì $a¡B$b ªº¤j¤p¥´¥X a is bigger than b©Î a equal to b ©Î a is smaller than b: <informalexample> <programlisting role="php"> if ($a > $b) { print "a is bigger than b"; } elseif ($a == $b) { print "a is equal to b"; } else { print "a is smaller than b"; } </programlisting> </informalexample> </para> <simpara> ¥i¥H¦³¦n´XÓ elseif¥X²{¦b¦P¤@Ó if »y¥y¤¤¡C ²Ä¤@Ó¦¨¥ßªº elseif ·|³Q°õ¦æ¡C ½s¼g PHP µ{¦¡®É§A¤]¥i¥H¥Î¨âÓ¦rªº 'else if' ¡A ³o©M³æ¦rªº 'elseif' ¬O¬Û¦Pªº¡C ÁöµM¨âªÌ¦b¥yªkªº¸ÑÄÀ¤W¦³¤£¦P¡A¦ý©Ò¦³ªº¥\¯à©Mµ²ªG³£¬O¨S¦³¤À§Oªº¡C (¦p§A¼ô±x C ªº¸Ü¡A ³o©M C ªº±¡ªp¬O¤@¼Ë¡C) </simpara> <simpara> ¦A´£¿ô¤@¦¸¡A elseif ¤¤ªº«ü¥O²Õ¥u¦³·í¥¦«e±ªº if ©M¨ä¥Lªº elseif ªº±ø¥ó¤£¦¨¥ß¨Ã¥B¥¦¦Û¤vªº±ø¥ó¦¨¥ß®É¤~·|³Q°õ¦æ¡C </simpara> </sect1> <sect1 id="control-structures.alternative-syntax"> <title>µ¥®Ä¥yªk</title> <para> PHP ¹ï©ó¥H¤Uªº±ø¥ó¥y´£¨Ñ¤F¥t¥~¤@ºØ¼gªk¡A³o¨Ç»y¥y¥]¬A¡Gif¡B while¡B for¡B switch¡C µ¥®Äªº¼gªk´N¬O§â«ü¥O²Õ¶}ÀYªº '{' ¬A¸¹Åܦ¨«_¸¹ ':'¡G ¦P®Éµ²§Àªº '}' ¤À§OÅܦ¨ endif; endwhile; endfor; endswitch;¡C <informalexample> <programlisting role="php"> <?php if ($a == 5): ?> A is equal to 5 <?php endif; ?> </programlisting> </informalexample> </para> <simpara> ¦b¤Wz¥Ü½d¤¤¡A HTML ®æ¦¡ªº¥y¤l "A is equal to 5" ³Q¥]¦b¤@Ó if »y¥y¤§¤¤¡C ³o±¡ªp¤U¥u¦³·í $a ¬O 5 ®É¸Ó¥y¤l¤~³Q¦L¥X¨Ó¡C </simpara> <para> µ¥®Äªº¥yªk¤]¥i¥Î¦b else©M elseif ¤W¡C¤U±´N¬O if ¥[¤W elseif ©M else ªºµ¥®Ä¼gªk¡C <informalexample> <programlisting role="php"> if ($a == 5): print "a equals 5"; print "..."; elseif ($a == 6): print "a equals 6"; print "!!!"; else: print "a is neither 5 nor 6"; endif; </programlisting> </informalexample> </para> <para> ¥t½Ð¬Ý <link linkend="control-structures.while">while</link>, <link linkend="control-structures.for">for</link>, <link linkend="control-structures.if">if</link> ¤¤ªº¨ä¥L¥Ü½d¡C </para> </sect1> <sect1 id="control-structures.while"> <title><literal>while</literal></title> <para> while °j°é¬O PHP ùس̲³æªº°j°é§Î¦¡¡C ©M c ªº while ¤@¼Ë¡A ¥¦°ò¥»ªº¥y¦¡¬O¡G <informalexample> <programlisting> while (expr) statement </programlisting> </informalexample> </para> <simpara> while ªº·N«ä«Üª½±µ¡G ¥un¥¦ªº±ø¥óªí¹F¦¡¦¨¥ß¡A ¥¦·|¥s PHP ¤£°±¦a°õ¦æ while ¤§¤ºªº«ü¥O¡C ¦]¬°¨C¦¸ while ¤§¤ºªº«ü¥O¥þ³¡°õ¦æ«á³£·|Àˬd¤@¦¸ while ±ø¥ó¬O§_¨ÌµM¦¨¥ß¡A ©Ò¥H´Nºâ±ø¥ó¦b«ü¥O¤§®É¤w¸g§ïÅÜ¡A ¦ý³£nµ¥©Ò¦³«ü¥O³£§¹¦¨«á¤~·|¸õ¥X°j°é¡C(¨C¦¸°õ¦æ§¹°j°é¤¤©Ò¦³ªº«ü¥O³£¥s°µ§¹¦¨¤F¤@¦¸´`Àô) °²¦p while ªí¹F¦¡¦b¤@¶}©lªº®ÉÔ´N¤£¦¨¥ß¡A¨º while ¤§¤¤ªº«ü¥O®Ú¥»¤£·|³Q°õ¦æ (¹s¦¸´`Àô)¡C </simpara> <para> ©M if ¤@¼Ë¡A §A¥i¥H§â¦n¦h«ü¥O¥Î '{ }' ¥]°_¨Ó©ñ¦bwhile °j°é¤§¤¤¡C §A¤]¥i¥H¥Î while ªºµ¥®Ä¥yªk¨Ó¼g: <informalexample> <programlisting> while (expr): statement ... endwhile; </programlisting> </informalexample> </para> <para> ¤U±¨âӥܽd±N²£¥Í¬Û¦Pªºµ²ªG¡G §â 1 ¨ì 10 ¦L¥X¨Ó <informalexample> <programlisting> /* example 1 */ $i = 1; while ($i <= 10) { print $i++; /* the printed value would be $i before the increment (post-increment) */ } /* example 2 */ $i = 1; while ($i <= 10): print $i; $i++; endwhile; </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.do.while"> <title><literal>°j°é</literal></title> <simpara> do.while °j°é ©M while °j°é®t¤£¦h¡A¦ý¥¦¨ì³Ì«á¤~Àˬd°õ¦æ±ø¥ó¬O§_¦¨¥ß¡C ³o¼Ë«OÃÒ¤F do.while ¤¤ªº«ü¥O°_½X·|³Q°õ¦æ¤@¦¸¡C ¥Ñ©ó while ¬O¦b¤@¶}ÀY«KÀˬd±ø¥ó¡A ©Ò¥H¥Î while ªº¸Ü¥i¯à¤@¦¸´`Àô¤]°õ¦æ¤£¤F¡C </simpara> <para> do.while °j°é ¥u¦³¤@Ó¥yªk¡A ¨S¦³§Oªºµ¥®Ä¼gªk : <informalexample> <programlisting role="php"> $i = 0; do { print $i; } while ($i>0); </programlisting> </informalexample> </para> <simpara> ¤W±ªºµ{¦¡¥u·|¶]¤@¦¸¡A ¦]¬°«ü¥O°õ¦æ§¹¤F¤~Àˬd±ø¥ó¤~µo²{ì¨Ó±ø¥ó¤£¦¨¥ß©ó¬O¸õ¥X°j°é¡C </simpara> <para> ¥Î C ½sµ{ªº°ª¤â¥i¯àÀ´±o¥t¤@ºØ do.while °j°é ªº¥Îªk¡G ¥Î <link linkend="control-structures.break"><literal>break</literal></link> «ü¥O©M do.while(0) °j°é¨Ó¨Ïµ{¦¡¦b°õ¦æ¤¤³~²×¤î¡C¤U¦Cµ{¦¡¬q¥Ü½d³o¤@ӥΪk: <informalexample> <programlisting role="php"> do { if ($i < 5) { print "i is not big enough"; break; } $i *= $factor; if ($i < $minimum_limit) { break; } print "i is ok"; ...process i... } while(0); </programlisting> </informalexample> </para> <simpara> Áa¨Ï§A¬Ý¤£¥X¤W±µ{¦¡¦³¤°»ò¶ø§®¤]¨SÃö«Y¡C ´Nºâ¤£¥Î do.while ªº³oÓ¯S©Ê¤]¤@¼Ë¥i¥H¼g¥X¥\¯à«Ü±jªºµ{¦¡¡C </simpara> </sect1> <sect1 id="control-structures.for"> <title><literal>for °j°é</literal></title> <para> for °j°é ¥i»¡¬O PHP ¤¤³Ì½ÆÂøªº°j°éÃþ¡C¥¦Ìªº¥Îªk©M¦b C ¤¤ªº¥Îªk¨Ã¨S¦³¤À§O: <informalexample> <programlisting> for (expr1; expr2; expr3) statement </programlisting> </informalexample> </para> <simpara> º¥ý²Ä¤@Óªí¹F¦¡ (expr1) µL½×¦p¦ó³£·|ºtºâ¤@¦¸ </simpara> <simpara> µM«á¦b¨CÓ´`Àô¶}©l«e expr2 ³£·|³Qºâ¥X¨Ó¡A ¦pªG±o¥Xµ²ªG¦¨¥ßªº¸Ü¡A °j°é¤¤ªº«ü¥O«K³Q°õ¦æ¡A ¦pªG expr2 ¤£¦¨¥ßªº¸Ü¡A «K·|¸õ¥X°j°é¡C </simpara> <simpara> ¨C¤@Ó´`Àô§¹¦¨¤§«á¹q¸£·|ºtºâ¤@¦¸ expr3¡C </simpara> <simpara> ¤TÓªí¹F¦¡¤¤¥ô¦ó¤@Ó³£¥i¥H¬OªÅªº¡C expr2 ªÅªº¸Ü¥Nªí°j°é·|µL¤î¹Ò¦a°õ¦æ¤U¥h¡C (PHP ©M C ¤@¼Ë¡A ¦Û°Ê§âªÅªºªí¹F¦¡·í¦¨¬O¦¨¥ßªº) ³oÅ¥¤W¥h«ÜµL²á¡A ¦ý¹ê»Ú¤W§A¥i¯à·Q¦b°j°é¤¤¥Î <link linkend="control-structures.break"><literal>break</literal></link> ªº¤è¦¡¨Óµ²§ô¡C </simpara> <para> ¥H¤U¨C¤@Ó¨Ò¤l³£·|Åã¥Ü 1 ¨ì 10: <informalexample> <programlisting role="php"> /* example 1 */ for ($i = 1; $i <= 10; $i++) { print $i; } /* example 2 */ for ($i = 1;;$i++) { if ($i > 10) { break; } print $i; } /* example 3 */ $i = 1; for (;;) { if ($i > 10) { break; } print $i; $i++; } /* example 4 */ for ($i = 1; $i <= 10; print $i, $i++) ; </programlisting> </informalexample> </para> <simpara> ·íµM¡A ¦b¤W±ªº¥Ü½d¤¤¬Ý°_¨Ó¥u¦³²Ä¤@Ó (¤]³\¤]¤H³ß·R²Ä¥|Ó¨Ò¤l) ³Ì¥¿³W¡A ¦³¤£¹L¦³®ÉÔ¦b for °j°é ¥ÎªÅªºªí¹F¦¡¥i¥H¥O½s¼gµ{¦¡Åܫܤè«K¡C </simpara> <para> for °j°é ¤]¦³¥¦ªº¥t¤@ºØ¼gªk¡C <informalexample> <programlisting> for (expr1; expr2; expr3): statement; ...; endfor; </programlisting> </informalexample> </para> <para> ¦³ªºµ{¦¡»y¨¥¦³Ó¥s foreach ªº©R¥O¨Ó¦s¨ú°}¦C©M«÷Âø¸ê®Æªí¡C PHP3 ¨S¦³³o¼Ë«ü¥O¦ý PHP4 ¦³¡C (½Ð¬Ý <link linkend="control-structures.foreach">foreach</link>)¡C¥Î PHP3 ®É¡A§An¦P®É¥Î <link linkend="control-structures.while">while</link> ©M<function>list</function> ¥H¤Î <function>each</function> ¨ç¼Æ¤~¯à§¹¦¨¬Û¦Pªº¤u§@¡C ·Qª¾¹D«ç»ò°µªº¸Ü½Ð¬Ý¦³Ãö³o¨Ç¨ç¼Æªº³¹¸`¡C </para> </sect1> <sect1 id="control-structures.foreach"> <title><literal>foreach</literal></title> <para> PHP4 (PHP3 ¨Ã¨S¦³) ¥[¤J¤F foreach ªº¥\¯à¡C³o¥\¯à¦b PERL ©M¨ä¥L»y¨¥¤¤³£¦³¡C ¥Î¥¦¥i¥H«Ü¤è«K¦a¦s¨ú°}¦C¡C ¦Xªkªº¥y¦¡¦³¨âÓ¡G ²Ä¤GÓ¥u¬O¹ï²Ä¤@Ó¥yªk§@¤F¤@ÂI§ï°Ê¦ý«Ü¦³¥Î¡C¦p¤U¡G <informalexample> <programlisting> foreach(array_expression as $value) statement foreach(array_expression as $key => $value) statement </programlisting> </informalexample> </para> <simpara> ²Ä¤@Ó¥y¦¡¨C¦¸´`Àô³£·|§â°}¦C array_expression ¤¤ªº¤@Ó¤¸¯ÀªºÈ¦s¨ìÅÜ¼Æ $value ¤¤ª½¨ì°}¦C¤¤©Ò¦³ªº¤¸¯À¥Î§¹¤F¡C </simpara> <simpara> ²Ä¤GÓ¥y¦¡©M²Ä¤@Ó®t¤£¦h¡A ¤À§O¦b©ó°£¤F $value ·|Àx¦s¤F¤¸¯ÀªºÈ¡A $key ÁÙ·|Àx¦s¤F¥Ø«e¤¸¯Àªº¯Á¤Þ¸¹¡C </simpara> <para> <note> <para> ·í foreach ¶}©l®É¤º¸mªº°}¦C«ü¼Ð·|«ü¦V°}¦CªººÓ¤¸¯Àªº¦ì¸m¡C ³o¥Nªí§A¤£¥²¦b¨C¦¸ foreach «e½Õ¥Î <function>reset</function> ¨ç¼Æ¡C </para> </note> </para> <para> <note> <para> Also note that <literal>foreach</literal> operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified like with the each construct. </para> </note> </para> <para> ¦b¤U±ªº½d¨Ò¡A §AÀ³¥i¬Ý¥X¥¦Ì¥\¯à¬O¤@¼Ëªº: <informalexample> <programlisting role="php"> reset ($arr); while (list(, $value) = each ($arr)) { echo "Value: $value<br>\n"; } foreach ($arr as $value) { echo "Value: $value<br>\n"; } </programlisting> </informalexample> ¤U±ªº½d¨Ò¤]¬O¤@¼Ë¥\¯àªº: <informalexample> <programlisting role="php"> reset ($arr); while (list($key, $value) = each ($arr)) { echo "Key: $key; Value: $value<br>\n"; } foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br>\n"; } </programlisting> </informalexample> </para> <para> §ó¦hªº¥Ü½d: <informalexample> <programlisting role="php"> /* foreach example 1: value only */ $a = array (1, 2, 3, 17); foreach ($a as $v) { print "Current value of \$a: $v.\n"; } /* foreach example 2: value (with key printed for illustration) */ $a = array (1, 2, 3, 17); $i = 0; /* for illustrative purposes only */ foreach($a as $v) { print "\$a[$i] => $v.\n"; } /* foreach example 3: key and value */ $a = array ( "one" => 1, "two" => 2, "three" => 3, "seventeen" => 17 ); foreach($a as $k => $v) { print "\$a[$k] => $v.\n"; } </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.break"> <title><literal>break</literal></title> <simpara> break ¬O¥Î¨Ó¸õÂ÷Ãþ¦ü¤U¦Cªº°j°é©Î±ø¥ó¥y¦¡ ¡G if ¡B for ¡B while¡Bswitch ¡C </simpara> <simpara> ¥Î break ®É¥i¥H¥[¤W¤@ӰѼƧi¶D¹q¸£n¸õÂ÷¦h¤Ö¼hªº°j°é¡C </simpara> <para> <informalexample> <programlisting role="php"> $arr = array ('one', 'two', 'three', 'four', 'stop', 'five'); while (list (, $val) = each ($arr)) { if ($val == 'stop') { break; /* You could also write 'break 1;' here. */ } echo "$val<br>\n"; } /* Using the optional argument. */ $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5<br>\n"; break 1; /* Exit only the switch. */ case 10: echo "At 10; quitting<br>\n"; break 2; /* Exit the switch and the while. */ default: break; } } </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.continue"> <title><literal>continue </literal></title> <simpara> continue ¦b°j°é¤¤ªº§@¥Î¬O¥ß®É¸õÂ÷²{®É°õ¦æ¤¤ªº´`Àô¦Ó¶}©l¤U¤@Ó´`Àô¡C </simpara> <simpara> continue ©M break ¤@¼Ë¥i¥Î¤@ӰѼƫü¥Ü¥¦À³¸Ó±q¨º¤@¼h°j°é¶}©l¤U¤@¦¸´`Àô¡C </simpara> <para> <informalexample> <programlisting role="php"> while (list ($key, $value) = each ($arr)) { if (!($key % 2)) { // skip odd members continue; } do_something_odd ($value); } $i = 0; while ($i++ < 5) { echo "Outer<br>\n"; while (1) { echo " Middle<br>\n"; while (1) { echo " Inner<br>\n"; continue 3; } echo "This never gets output.<br>\n"; } echo "Neither does this.<br>\n"; } </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.switch"> <title><literal>switch</literal></title> <simpara> switch ©R¥Oªº¥\¯à©M¤@³s¦ê¥Î¦P¤@±ø¥óªí¹F¦¡ªº if »y¥y¬Û¦ü¡C ¦³¤£¤Öªº±¡ªpn¨D§â¤@ÓÅܼƩΤ@¥yªí¹F¦¡©M¤£¦Pªº¼ÆÈ§@¤ñ¸û¡A ·í¥¦µ¥©ó¬Y¤@Ó¼ÆÈ®É«K°õ¦æ¬Û¹ïÀ³ªºµ{¦¡¬q¡C ³o¥¿¬O switch ªº¥\¥Î¡C </simpara> <para> ¤U±¥Ü½d¤F«ç¼Ë¥Î¤@³s¦êªº if ©M¥Î¤@Ó switch ¨Ó§¹¦¨¬Û¦Pªº¤u§@¡G <informalexample> <programlisting role="php"> if ($i == 0) { print "i equals 0"; } if ($i == 1) { print "i equals 1"; } if ($i == 2) { print "i equals 2"; } switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; } </programlisting> </informalexample> </para> <para> ¬°¤FÁ×§K§Ë¿ù¡A³Ì¦n¬O·d©ú¥Õ switch ªº¹B§@¤è¦¡¡C switch ¬O¤@¥y±µ¤@¥yªº°õ¦æµ{¦¡ªº¡C º¥ý PHP n§ä¤@Ócase ªºÈ©M switch ±ø¥óªí¹F¦¡©Òºtºâ¥X¨ÓªºÈ¬O¤@¼Ëªº¡C µM«á PHP ·|°õ¦æ¦b¨ºÓ case ¤§«á¥X²{ªº©Ò¦³«ü¥Oª½¨ì switch µ{¦¡¬qªº³Ì«á©ÎªÌª½¨ì PHP ¸I¨ì¤@¥y break «ü¥O¡C ¦pªG§A¨S¦³¦b¨CÓ case ªº³Ì«á¥[¤W break «ü¥O¡A PHP ·|±µ¤U¥h°õ¦æ±µ¦b¤U±ªº¥t¤@Ó case ¤§¤¤ªº«ü¥O¡I ¤U±ªºµ{¦¡¥Ü½d¤F³o¤@ÂI¡G <informalexample> <programlisting role="php"> switch ($i) { case 0: print "i equals 0"; case 1: print "i equals 1"; case 2: print "i equals 2"; } </programlisting> </informalexample> </para> <simpara> ¹ï¤W±µ{¦¡¦Ó¨¥¡A ¦pªG $i ¬O 0 ªº¸Ü¡A PHP ·|°õ¦æ©Ò¦³ªº print «ü¥O¡I ¦pªG $i ¬O 1¡A PHP ¥u·|°õ¦æ«á±¨â¥y«ü¥O¡F ¥u¦³·í $i µ¥ 2 ¤~·|±o¨ì¥»¨Ó·Qnªºµ²ªG¡G¦L¥X 'i equals 2' ¡C ¥Ñ¦¹¥i¨£¡A ¥ý¸U§Onº|¤F©ñ break «ü¥O¦b¨CÓ case ¤§«á¡C (ÁöµM¦b¤@¨Ç±¡ªp¤U§A¥i¯à¯S¦a¤£¥[ break¡C) </simpara> <simpara> µ{¦¡°õ¦æ switch ®É¡A ±ø¥óªí¹F¦¡¥u·|³QÀˬd¤@¦¸¡C ³o¤@¦¸ªº¹Bºâµ²ªG·|¥Î¨Ó©M¨CÓ case ¤ñ¸û¡F ¦ý¦b elseif »y¥y¤¤¸Ó±ø¥óªí¹F¦¡·|«·s³Qºtºâ¤@¹M¡C ¦]¦¹¦b±ø¥óªí¹F¦¡«Ü½ÆÂø©Î¬O¦b³t«×n¨D«Ü°ªªº°j°é¤¤¥Î switch ·|¤ñ¸û§Ö¤@¨Ç¡C </simpara> <para> ¤@Ó case ©Ò§tªº«ü¥O¼Æ¥Ø¤]¥i¥H¬O 0 Ó¡A ³o¼Ë¤@¨Óµ{¦¡·|¦Û°ÊÀˬd¤U¤@Ó case <informalexample> <programlisting role="php"> switch ($i) { case 0: case 1: case 2: print "i is less than 3 but not negative"; break; case 3: print "i is 3"; } </programlisting> </informalexample> </para> <para> ¦³Ó¯S®íªº case ¬O default case¡A ¦pªG©Ò¦³ªº case ³£¨Sªk¦¨¥ß®É¡A default «K·|³Q°õ¦æ¡C ¬Ý¤U±ªº¥Ü½d: <informalexample> <programlisting role="php"> switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; default: print "i is not equal to 0, 1 or 2"; } </programlisting> </informalexample> </para> <para> ¦³®Äªº case ªí¹F¦¡¤@©wn±o¥X¤@ÓÃþ§Î²³æªº¼ÆÈ¡A ¦p¾ã¼Æ¡B ¤p¼Æ©Î¬O¤å¥y¡C ¦pªG¬O°}¦Cªº¸Ü´N¤@©wn«ü¥X°}¦C¤¤ªº¨º¤@Ó¤¸¯À¨Ã¸Ó¤¸¯À¤]n¬O¤@ÓÃþ«¬Â²³æªºÈ¡C </para> <para> swithes ¤]¦³¥¦Ì¥t¤@ºØµ¥®Äªº¼gªk¡A ¸Ô±¡¥i¬Ý <link linkend="control-structures.alternative-syntax">±±¨îµ{¦¡¬yµ{ªºµ¥®Ä¥yªk </link> . <informalexample> <programlisting role="php"> switch ($i): case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; default: print "i is not equal to 0, 1 or 2"; endswitch; </programlisting> </informalexample> </para> </sect1> <sect1 id="function.require"> <title><function>require</function></title> <simpara> The <function>require</function> statement replaces itself with the specified file, much like the C preprocessor's <literal>#include</literal> works. </simpara> <simpara> If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be <function>require</function>ed using an URL instead of a local pathname. See <link linkend="features.remote-files">Remote files</link> and <function>fopen</function> for more information. </simpara> <simpara> An important note about how this works is that when a file is <function>include</function>ed or <function>require</function>ed, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes PHP mode again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within <link linkend="language.basic-syntax.phpmode">valid PHP start and end tags</link>. </simpara> <simpara> <function>require</function> is not actually a function in PHP; rather, it is a language construct. It is subject to some different rules than functions are. For instance, <function>require</function> is not subject to any containing control structures. For another, it does not return any value; attempting to read a return value from a <function>require</function> call results in a parse error. </simpara> <simpara> Unlike <function>include</function>, <function>require</function> will <emphasis>always</emphasis> read in the target file, <emphasis>even if the line it's on never executes</emphasis>. If you want to conditionally include a file, use <function>include</function>. The conditional statement won't affect the <function>require</function>. However, if the line on which the <function>require</function> occurs is not executed, neither will any of the code in the target file be executed. </simpara> <simpara> Similarly, looping structures do not affect the behaviour of <function>require</function>. Although the code contained in the target file is still subject to the loop, the <function>require</function> itself happens only once. </simpara> <para> This means that you can't put a <function>require</function> statement inside of a loop structure and expect it to include the contents of a different file on each iteration. To do that, use an <function>include</function> statement. <informalexample> <programlisting role="php"> require ('header.inc'); </programlisting> </informalexample> </para> <simpara> When a file is <function>require</function>ed, the code it contains inherits the variable scope of the line on which the <function>require</function> occurs. Any variables available at that line in the calling file will be available within the called file. If the <function>require</function> occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. </simpara> <para> If the <function>require</function>ed file is called via HTTP using the fopen wrappers, and if the target server interprets the target file as PHP code, variables may be passed to the <function>require</function>ed file using an URL request string as used with HTTP GET. This is not strictly speaking the same thing as <function>require</function>ing the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script. <informalexample> <programlisting role="php"> /* This example assumes that someserver is configured to parse .php * files and not .txt files. Also, 'works' here means that the variables * $varone and $vartwo are available within the require()ed file. */ /* Won't work; file.txt wasn't handled by someserver. */ require ("http://someserver/file.txt?varone=1&vartwo=2"); /* Won't work; looks for a file named 'file.php?varone=1&vartwo=2' * on the local filesystem. */ require ("file.php?varone=1&vartwo=2"); /* Works. */ require ("http://someserver/file.php?varone=1&vartwo=2"); $varone = 1; $vartwo = 2; require ("file.txt"); /* Works. */ require ("file.php"); /* Works. */ </programlisting> </informalexample> </para> <simpara> In PHP 3, it is possible to execute a <literal>return</literal> statement inside a <function>require</function>ed file, as long as that statement occurs in the global scope of the <function>require</function>ed file. It may not occur within any block (meaning inside braces ({}). In PHP 4, however, this ability has been discontinued. If you need this functionality, see <function>include</function>. </simpara> <simpara> See also <function>include</function>, <function>require_once</function>, <function>include_once</function>, <function>readfile</function>, and <function>virtual</function>. </simpara> </sect1> <sect1 id="function.include"> <title><function>include</function></title> <simpara> The <function>include</function> statement includes and evaluates the specified file. </simpara> <simpara> If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be <function>include</function>ed using an URL instead of a local pathname. See <link linkend="features.remote-files">Remote files</link> and <function>fopen</function> for more information. </simpara> <simpara> An important note about how this works is that when a file is <function>include</function>ed or <function>require</function>ed, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within <link linkend="language.basic-syntax.phpmode">valid PHP start and end tags</link>. </simpara> <para> This happens each time the <function>include</function> statement is encountered, so you can use an <function>include</function> statement within a looping structure to include a number of different files. <informalexample> <programlisting role="php"> $files = array ('first.inc', 'second.inc', 'third.inc'); for ($i = 0; $i < count($files); $i++) { include $files[$i]; } </programlisting> </informalexample> </para> <para> <function>include</function> differs from <function>require</function> in that the include statement is re-evaluated each time it is encountered (and only when it is being executed), whereas the <function>require</function> statement is replaced by the required file when it is first encountered, whether the contents of the file will be evaluated or not (for example, if it is inside an <link linkend="control-structures.if">if</link> statement whose condition evaluated to false). </para> <para> Because <function>include</function> is a special language construct, you must enclose it within a statement block if it is inside a conditional block. <informalexample> <programlisting role="php"> /* This is WRONG and will not work as desired. */ if ($condition) include($file); else include($other); /* This is CORRECT. */ if ($condition) { include($file); } else { include($other); } </programlisting> </informalexample> </para> <simpara> In both PHP 3 and PHP 4, it is possible to execute a <literal>return</literal> statement inside an <function>include</function>ed file, in order to terminate processing in that file and return to the script which called it. Some differences in the way this works exist, however. The first is that in PHP 3, the <literal>return</literal> may not appear inside a block unless it's a function block, in which case the <literal>return</literal> applies to that function and not the whole file. In PHP 4, however, this restriction does not exist. Also, PHP 4 allows you to return values from <function>include</function>ed files. You can take the value of the <function>include</function> call as you would a normal function. This generates a parse error in PHP 3. </simpara> <example> <title><function>include</function> in PHP 3 and PHP 4</title> <para> Assume the existence of the following file (named <filename>test.inc</filename>) in the same directory as the main file: <programlisting role="php"> <?php echo "Before the return <br>\n"; if (1) { return 27; } echo "After the return <br>\n"; ?> </programlisting> </para> <para> Assume that the main file (<filename>main.html</filename>) contains the following: <programlisting role="php"> <?php $retval = include ('test.inc'); echo "File returned: '$retval'<br>\n"; ?> </programlisting> </para> <para> When <filename>main.html</filename> is called in PHP 3, it will generate a parse error on line 2; you can't take the value of an <function>include</function> in PHP 3. In PHP 4, however, the result will be: <screen> Before the return File returned: '27' </screen> </para> <para> Now, assume that <filename>main.html</filename> has been altered to contain the following: <programlisting role="php"> <?php include ('test.inc'); echo "Back in main.html<br>\n"; ?> </programlisting> </para> <para> In PHP 4, the output will be: <screen> Before the return Back in main.html </screen> However, PHP 3 will give the following output: <screen> Before the return 27Back in main.html Parse error: parse error in /home/torben/public_html/phptest/main.html on line 5 </screen> </para> <para> The above parse error is a result of the fact that the <literal>return</literal> statement is enclosed in a non-function block within <filename>test.inc</filename>. When the return is moved outside of the block, the output is: <screen> Before the return 27Back in main.html </screen> </para> <para> The spurious '27' is due to the fact that PHP 3 does not support <literal>return</literal>ing values from files like that. </para> </example> <simpara> When a file is <function>include</function>ed, the code it contains inherits the variable scope of the line on which the <function>include</function> occurs. Any variables available at that line in the calling file will be available within the called file. If the <function>include</function> occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. </simpara> <para> If the <function>include</function>ed file is called via HTTP using the fopen wrappers, and if the target server interprets the target file as PHP code, variables may be passed to the <function>include</function>ed file using an URL request string as used with HTTP GET. This is not strictly speaking the same thing as <function>include</function>ing the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script. <informalexample> <programlisting role="php"> /* This example assumes that someserver is configured to parse .php * files and not .txt files. Also, 'works' here means that the variables * $varone and $vartwo are available within the include()ed file. */ /* Won't work; file.txt wasn't handled by someserver. */ include ("http://someserver/file.txt?varone=1&vartwo=2"); /* Won't work; looks for a file named 'file.php?varone=1&vartwo=2' * on the local filesystem. */ include ("file.php?varone=1&vartwo=2"); /* Works. */ include ("http://someserver/file.php?varone=1&vartwo=2"); $varone = 1; $vartwo = 2; include ("file.txt"); /* Works. */ include ("file.php"); /* Works. */ </programlisting> </informalexample> </para> <simpara> See also <function>require</function>, <function>require_once</function>, <function>include_once</function>, <function>readfile</function>, and <function>virtual</function>. </simpara> </sect1> <sect1 id="function.require-once"> <title><function>require_once</function></title> <para> The <function>require_once</function> statement replaces itself with the specified file, much like the C preprocessor's <literal>#include</literal> works, and in that respect is similar to the <function>require</function> statement. The main difference is that in an inclusion chain, the use of <function>require_once</function> will assure that the code is added to your script only once, and avoid clashes with variable values or function names that can happen. </para> <para> For example, if you create the following 2 include files <literal>utils.inc</literal> and <literal>foolib.inc</literal> <example> <title>utils.inc</title> <programlisting role="php"> <?php define(PHPVERSION, floor(phpversion())); echo "GLOBALS ARE NICE\n"; function goodTea() { return "Oolong tea tastes good!"; } ?> </programlisting> </example> <example> <title>foolib.inc</title> <programlisting role="php"> <?php require ("utils.inc"); function showVar($var) { if (PHPVERSION == 4) { print_r($var); } else { var_dump($var); } } // bunch of other functions ... ?> </programlisting> </example> And then you write a script <literal>cause_error_require.php</literal> <example> <title>cause_error_require.php</title> <programlisting role="php"> <?php require("foolib.inc"); /* the following will generate an error */ require("utils.inc"); $foo = array("1",array("complex","quaternion")); echo "this is requiring utils.inc again which is also\n"; echo "required in foolib.inc\n"; echo "Running goodTea: ".goodTea()."\n"; echo "Printing foo: \n"; showVar($foo); ?> </programlisting> </example> When you try running the latter one, the resulting ouptut will be (using PHP 4.01pl2): <informalexample> <programlisting> GLOBALS ARE NICE GLOBALS ARE NICE Fatal error: Cannot redeclare goodTea() in utils.inc on line 5 </programlisting> </informalexample> By modifying <literal>foolib.inc</literal> and <literal>cause_errror_require.php</literal> to use <function>require_once</function> instead of <function>require</function> and renaming the last one to <literal>avoid_error_require_once.php</literal>, we have: <example> <title>foolib.inc (fixed)</title> <programlisting role="php"> .. require_once("utils.inc"); function showVar($var) { .. </programlisting> </example> <example> <title>avoid_error_require_once.php</title> <programlisting role="php"> .. require_once("foolib.inc"); require_once("utils.inc"); $foo = array("1",array("complex","quaternion")); .. </programlisting> </example> And when running the latter, the output will be (using PHP 4.0.1pl2): <informalexample> <programlisting> GLOBALS ARE NICE this is requiring globals.inc again which is also required in foolib.inc Running goodTea: Oolong tea tastes good! Printing foo: Array ( [0] => 1 [1] => Array ( [0] => complex [1] => quaternion ) ) </programlisting> </informalexample> </para> <para> Also note that, analogous to the behavior of the <literal>#include</literal> of the C preprocessor, this statement acts at "compile time", e.g. when the script is parsed and before it is executed, and should not be used for parts of the script that need to be inserted dynamically during its execution. You should use <function>include_once</function> or <function>include</function> for that purpose. </para> <para> For more examples on using <function>require_once</function> and <function>include_once</function>, look at the PEAR code included in the latest PHP source code distributions. </para> <para> See also: <function>require</function>, <function>include</function>, <function>include_once</function>, <function>get_required_files</function>, <function>get_included_files</function>, <function>readfile</function>, and <function>virtual</function>. </para> </sect1> <sect1 id="function.include-once"> <title><function>include_once</function></title> <para> The <function>include_once</function> statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the <function>include</function> statement, with the important difference that if the code from a file has already been included, it will not be included again. </para> <para> As mentioned in the <function>require_once</function> description, the <function>include_once</function> should be used in the cases in which the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc. </para> <para> For more examples on using <function>require_once</function> and <function>include_once</function>, look at the PEAR code included in the latest PHP source code distributions. </para> <para> <function>include_once</function> was added in PHP 4.0.1pl2 </para> <para> See also: <function>require</function>, <function>include</function>, <function>require_once</function>, <function>get_required_files</function>, <function>get_included_files</function>, <function>readfile</function>, and <function>virtual</function>. </para> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> <!-- Keep this comment for vi/vim/gvim vi: et:ts=1:sw=1 --> Index: phpdoc/hk/language/expressions.xml +++ phpdoc/hk/language/expressions.xml <chapter id="language.expressions"> <title>ªí¹F¦¡</title> <simpara> ªí¹F¦¡¬O PHP µ{¦¡ªº³Ì®Ú¥»ªº²Õ¦¨³¡¥÷¡C ¥Î PHP ½sµ{¡A´X¥G©Ò¦³§A¼g±o¤U¨Óªº«ü¥O¥y³£¬Oªí¹F¦¡¡C ¥Î³Ì²³æ¥B³Ìºë½Tªº¥y¤l¨Ó´yzªí¹F¦¡´N¬O¡G "¥ô¦ó¦³ÈªºªF¦è"¡C </simpara> <simpara> ³Ì°ò¥»ªºªí¹F¦¡¬O±`¼Æ©MÅܼơC·í§A¿é¤J "$a = 5"¡A §A§â '5' «ü¬£µ¹ $a¡C '5'¡A ·íµM¥Nªí¼ÆÈ 5¡A ¦ý´«Ó»¡ªk¬O '5' ¬O¤@Óªí¹F¦¡¡A ¥¦ªºÈ¬O 5¡C(¦b¥»¨Ò¤¤¡A '5' ¬O¤@Ó¾ã¼Æ§ÎºAªº±`¼Æ). </simpara> <simpara> ³oÓ«ü¬£¾Þ§@§¹¦¨«á¡A $a ªºÈ¤]À³¸Ó¬O 5¡A °²¦p§A¦A¼g $b = $a¡A¥¦µ¹¥Xªºµ²ªGÀ³¸Ó©M¼g $b = 5 ¬O¤@¼Ëªº¡C ´«Ó»¡ªk¡A $a ¤]¤@Óªí¹F¦¡¡A ¥¦ªí¹FªºÈ¦P¼Ë¬O 5¡C ¦pªG¨S°µ¿ù¤°»òªº¸Ü¡A ±o¥Xªºµ²ªG´N¬O³oÓ¡C </simpara> <para> ¸û½ÆÂøªºªí¹F¦¡¥i¥H¥Î¨ç¼Æ¨Ó§@¥Ü½d¡C ¦p¥H¤Uªº¨Ò¤l¡A <informalexample> <programlisting> function foo () { return 5; } </programlisting> </informalexample></para> <simpara> ¦p§A¹ï¨ç¼ÆÀ³¥Î¦³¤@©w»{ÃÑ (¦p§_½Ð¬Ý¦³Ãö¨ç¼Æªº³¹¸`)¡A §A·|¦P·N$c = foo() ©M $c = 5 ³o¨â¥y¬O¤@¼Ëªº¡C ¨Æ¹ê¤W¥¿¬O³o¼Ë¡C ¨ç¼Æ¤]¬O¤@ºØªí¹F¦¡¡A ¥¦Ì©Òªí¹FªºÈ´N¬O¨ç¼Æªº¹Bºâµ²ªG(¶Ç¦^È)¡C ¦]¬° foo() ¶Ç¦^ 5¡A ©Ò¥Hªí¹F¦¡ 'foo()' ªºÈ´N¬O 5¡C ·íµM¤@¯ë¨Ó»¡¨ç¼Æ¤£·|¥ú¶Ç¦^¤@Ó±`¼Æ¦Ó¬On§@¨Ç¹Bºâªº¡C </simpara> <simpara> ·íµM¡A Ȥ£¤@©w¬O¾ã¼Æ¡C PHP ¥i¥HÅý§AÀ³¥Î¤TºØ¤£¦PÃþ§Îªº¯Â¶qÈ¡A ¤À§O¬O¾ã¼Æ¡B¯BÂI¼Æ¡B¤å¥y¡C(¯Â¶qÈ´N¬O¤£¯à¦A¤À¦¨§ó¤pªº¤À¶qªºÈ¡A«D¯Â¶qȪº¨Ò¤l¦³¥Ú¶q¡A°}¦Cµ¥)¡C¥t¥~ PHP ¤]¤ä´©°}¦C©Mª«¥ó¨âÃþ«D¯Â¶qÈ¡C ±q¨ç¼Æ¶Ç¦^ªºÈªº§ÎºA©M«ü¬£µ¹ÅܼƪºÈªº§ÎºA¥i¥H¬O¤Wzªº¥ô¦ó¤@ºØÈ¡C </simpara> <simpara> ¨ì¥Ø«e¬°¤î¡A ¥Î PHP/FI 2 ªº¤HÀ³¤£·|ı±o¦³¤°»ò¤£¤@¼Ë¡C µM¦Ó¡A PHP §âªí¹F¦¡ªº·§©À¶i¤@¨Bµo´§¡A Åܦ¨©M«Ü¦h§Oªºµ{¦¡»y¨¥¤@¼Ë¡C PHP ¬O¥Hªí¹F¦¡¬°°ò¦ªº½sµ{»y¨¥¡A ³o¼Ë»¡¬O¦]¬°´X¥G©Ò¦³ PHP ªº¥y¤l³£¬O¥Ñªí¹F¦¡²Õ¦¨¡C ¤ÀªR¤W±¥Î¹Lªº¨Ò¤l '$a = 5'¡A«Ü©úÅ㪺¦b¾Þ§@¤¤¦³¨â¶µÈ¡A ¤@¶µ¬O¾ã¼Æ '5'¡A ¥t¤@¶µ¬O $a ªºÈ¡A ¥¦·|³Q '5' ©Ò¨ú¥N¡C ¤£¹L¨ä¹êÁÙ¦³¥t¤@ÓȦs¦b¡A¥¦´N¬Oªí¹F¦¡¥»¨©Òªí¹FªºÈ¡C ¦b¥»¨Ò¤¤ªí¹F¦¡ªºÈ´N¬O«ü¥NÈ¡A ¤]´N¬O '5'¡E À³¥Îªº®ÉÔ§ÚÌ¥i¥H§â '$a=5' ¬Ý¦¨¬OӶǦ^ '5' ³oÓȪºªí¹F¦¡¡C ¥Ñ¦¹¡A ¦pªG§A¼g '$b = ($a = 5)' ´Nµ¥¦P©ó¼g '$a = 5; $b = 5;' (¤À¸¹¥Nªí»y¥yªºµ²§ô)¡C ¦A¶i¤@¨B»¡¡A ¦]¬°ªí¹F¦¡¬O±q¥k¦V¥ª¨Ó°µ¹Bºâªº¡A ©Ò¥H´Nºâ²³æªº¼g¦¨ '$b = $a = 5'¡A µª®×¤]¬O¤@¼Ëªº¡C </simpara> <simpara> ¥Hªí¹F¦¡¬°°ò¥»ªº¾Þ§@ÁÙ¦³¥t¥~¤@ºØ«Ü¦nªº¥Ü¨Ò¡C ´N¬O«e¸m©Î«á¸mªº¼WÈ©M·lȾާ@¡C ¨Ï¥Î PHP/FI 2 ©M³\¦h¨ä¥L»y¨¥ªºµ{¦¡û¤j·§¤w¬ÝºD¤F ÅܼÆ++ ©M ÅܼÆ-- ªºªí¹F¥y¦¡¡C ³o¨Ç«K¬O¼WÈ©M·lȹBºâ²Å¡C ¦b PHP/FI 2 ªº®ÉÔ¡A '$a++' ¨Ã¤£¥Nªí¥ô¦óÈ (¨Ã¤£¬O¤@Óªí¹F¦¡)¡A ¦]¦¹°ò¥»¤W§A¤£¯à¥Î¥¦¨Ó°µ§Oªº¨Æ¡C ¨ì¤F PHP¡A ³o¨Ç¥y¦¡³£Åܦ¨¤Fªí¹F¦¡ (´N¹³ C ¤@¼Ë)¡C ¥t¥~©M C ¬Û¦Pªº¬O¡A ¦³¨âºØ¼WȺâ²Å¡G «e¸mªº©M«á¸mªº¡C ¨âºØ³£·|§âÅܼƼW¥[ 1¡A ¤À§O¦b©ó¨äªí¹F¦¡©Òªí¹FªºÈ¡C «e¸m¦¡¬O¼g¦¨ '++$variable'¡A ¶Ç¦^ªº¬O¼Wȫ᪺ȡF (PHP ¦bŪ¨úªí¹F¦¡ªºÈ¤§«e¥ý§âÅܼÆÈ¥[ 1¡A ¦]¦¹¥s«e¸m) «á¸mªí¹F¦¡¼g¦¨ '$variable++' ¶Ç¦^ÅܼÆÁÙ¨S¼W¥[ 1 «eªºÈ (PHP Ū¨úªºªí¹F¦¡ªºÈ¤§«á¤~§âÅܼƼWÈ)¡C </simpara> <simpara> ¤ñ¸û¦¡¬O¤@ºØ·¥¤§±`¨£ªºªí¹F¦¡¡C ³oÃþªí¹F¦¡ªº¹Bºâµ²ªG¬O 1 ©Î 0¡A ¤À§O¥NªíÅÞ¿èÈ ¯u ©M °²¡C PHP ©ú¥Õ > (¤j©ó)¡F >= (¤j©ó©Îµ¥©ó)¡F == (µ¥©ó)¡F != (¤£µ¥©ó)¡F < (¤p©ó)¡F <= (¤p©ó©Îµ¥©ó)¡C ³oÃþªí¹F¦¡³q±`¥Î¦b±ø¥ó¥y¤§¤¤¡A ¦pif »y¥y¡C </simpara> <simpara> ³Ì«á¤@ӥܽd§ÚÌ·|±¹ï¤@Ãþ¦P®É°µ«ü¬£©M¹Bºâªºªí¹F¦¡¡C §A¤wª¾¹D¦pªG·Q§â $a ¼W¥[ 1¡A ¥i¥Hª½±µ¥Î '$a++' ©Î '++$a'¡C ¦ý¬O¦pªG§A¨ä¹ê¬O·Q§â $a ¼W¥[¦h¤@¨Ç¡A ¤ñ¦p»¡ '3' ¨º¸Ó«ç¼Ë°µ¡@¡H ·íµM¥i¥H³s¼g 3 ¦æ'$a++'¡A ¤£¹L³o«Ü¤£¦n¬Ý¡C ¸û¥¥Áªº°µªk¬O¥Î '$a=$a+3;'¡G '$a + 3' ªºªí¹Fªº¬O $a ¦A¥[¤W 3¡A ³oµ²ªG·|©ñ¦^ÅÜ¼Æ $a ¤§¤¤¡A ¾ãÓ¹Bºâ§¹¦¨«á $a ªº¤º®e·|¼W¥[¤F 3¡C ¥Î PHP ©M³\¦h¨ä¥L»y¨¥¦P¼Ë¥i¥H§â¦¹Ãþ¾Þ§@¼g¦¨¥t¤@Ó¸û²³æªº¥y¦¡¡A±N¨Ó³oºØ¥yªk¥i¯à·|§ó¬y¦æ¤]§ó©ö²z¸Ñ¡C §â $a ªºÈ¼W¥[ 3 ¥i¼g¦¨ '$a += 3'. ¥¦¥N¥¿¥¿¥Nªí¤F§â $a ªºÈ®³¥X¨Ó¡A ¼W¥[ 3¡A ¦A¦s¦^ $a ·í¤¤¡C°£¤F§óµu©M§ó©ú¥Õ¤§¥~¡A ³o¥y¦¡°õ¦æ°_¨Ó¤]§ó§Ö¡C ³o¥yªí¹F¦¡¶Ç¦^ªºÈ©M°Q½× regular expr ®É¤@¼Ë¡A ¬Oªí¹F¦¡¹Bºâªºµ²ªG '$a+3'¡C ¥ô¦ó¤@Ó»Ýn¨âÓ¹Bºâ¤¸ªº¹Bºâ²Å³£¥i¥Î¦¹Ãþ¥y¦¡³y¥y¡A ¨Ò¦p '$a -= 5' (§â $a ´î 5), '$b *= 7' (§â $b ¼¥H 7) µ¥¡C </simpara> <para> ¥t¥~¦³¤@ºØ¬Ý°_¨Ó¤ñ¸û©Ç¦ý¬O¦b¨ä¥Lªºµ{¦¡»y¨¥¤¤¤]·|¥Î¨ìªºªí¹F¦¡: (ternary conditional operator ¤T¤¸¹Bºâ¦¡) <informalexample><programlisting> $first ? $second : $third </programlisting></informalexample> ·í²Ä¤@Óªí¹F¤À¦¡¦¨¥ß®É¡A ²Ä¤GÓªí¹F¤À¦¡·|³Q°õ¦æ¦Ó¹Bºâµ²ªG·|¦¨¬°¾ãÓªí¹F¦¡ªºÈ¡C ·í²Ä¤@Óªí¹F¤À¦¡¤£¦¨¥ß®É¡A ²Ä¤TÓ¤À¦¡ªºµ²ªG·|¦¨¬°¾ãÓªí¹F¦¡ªºÈ¡C </para> <para> ¥H¤Uªº¥Ü½d·|À°§A§ó¦n¦a´x´¤«e¸m©M«á¸m¼WȹBºâ²Å©Mªí¹F¦¡ªº¹B¥Î: <informalexample> <programlisting> function double($i) { return $i*2; } $b = $a = 5; /* assign the value five into the variable $a and $b */ $c = $a++; /* post-increment, assign original value of $a (5) to $c */ $e = $d = ++$b; /* pre-increment, assign the incremented value of $b (6) to $d and $e */ /* at this point, both $d and $e are equal to 6 */ $f = double($d++); /* assign twice the value of $d <emphasis>before</emphasis> the increment, 2*6 = 12 to $f */ $g = double(++$e); /* assign twice the value of $e <emphasis>after</emphasis> the increment, 2*7 = 14 to $g */ $h = $g += 10; /* first, $g is incremented by 10 and ends with the value of 24. the value of the assignment (24) is then assigned into $h, and $h ends with the value of 24 as well. */ </programlisting></informalexample></para> <simpara> ¦b¥»³¹¶}©l®É§ÚÌ»¡·|Á¿¨ì»y¥y statment ªºÃþ«¬¡C ªí¹F¦¡¥i¥H¬O»y¥y¡A ¦ý¨Ã«D¥þ³¡ªºªí¹F¥N³£¬O»y¥y¡C ¦b¤W¨Ò¤¤¡A »y¥yªº§Î¦¡¬O 'expr' ';' ¡A ´N¬O¤@¶µªí¹F¦¡¥[¤W¤À¸¹¡C ¤ñ¦p '$b=$a=5;'¡A $a=5 ¬O¤@Ó¦Xªkªºªí¹F¦¡¦ý¤£¬O¤@¥y»y¥y¡C ¦ý '$b=$a=5;' ˬO¤@¥y¦Xªkªº»y¥y¡C </simpara> <simpara> ³Ì«áȪº¤@´£ªº¬Oªí¹F¦¡ªºÅÞ¿èÈ¡C³\¦h±¡ªp¤U¡A ¤×¨ä¬O±ø¥ó¥y©M°j°é¥yùØ¡A ªí¹F¦¡ªºÈ¨Ã¤£¬O«ÂI¤Ï¦Ó¸Óªí¹F¦¡¬O§_¦¨¥ß ( TRUE ÁÙ¬O FALSE)¡C (PHP ¨Ã¨S¦³¯S§O«ü©wÅÞ¿èÅܼÆÃþ¡C) ªí¹F¦¡ªº¯u°²Èªºpºâ¤èªk©M perl ®t¤£¦h¡C ¥ô¦ó¤£µ¥©ó 0 ªº¼Æ¦r³£¥Nªí¯u¡A 0 «h¥Nªí°²¡C ·í¤ßt¼Æ¦]¬°¤]¤£µ¥ 0 ¡A ©Ò¥H¤]¥Nªí¯u¡I ªÅªº¤å¥y©Î¤å¥y "0" ¬O°²¡A ©Ò¦³¨ä¥L¤å¥y³£¬O¯u¡C ¦Ü©ó¹³°}¦C©Mª«¥ó³o¼Ëªº«D¯Â¶qÈ¡A ¦pªGµ²ªG¬OªÅªº°}¦C«h¥Nªí¯u¡A ¤Ï¤§«h¬O°²¡C </simpara> <simpara> PHP ´£¨Ñ¤F§¹¾ã¦Ó¥B¥\¯à±j¤jªºªí¹F¦¡¡C ¥»¤â¥U¨S¥i¯à§â©Ò¦³¥i¥Îªºªí¹F¦¡ªº¥yªk²Î²Î¾ã²z¦¨¤å¥ó¡A ¦ý¤W±ªº¥Ü½dÀ³¥iÅý§Aª¾¹D³y¥yªº¤èªk¡C ¦b¥»¤â¥U¤§¤¤¡A §ÚÌ·|¥Î expr ¨Ó¥Nªí¦Xªkªºªí¹F¦¡¡C </simpara> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/hk/language/functions.xml +++ phpdoc/hk/language/functions.xml <chapter id="functions"> <title>Functions</title> <sect1 id="functions.user-defined"> <title>User-defined functions</title> <para> A function may be defined using syntax such as the following: <informalexample> <programlisting role="php"> function foo ($arg_1, $arg_2, ..., $arg_n) { echo "Example function.\n"; return $retval; } </programlisting> </informalexample> </para> <simpara> Any valid PHP code may appear inside a function, even other functions and <link linkend="keyword.class">class</link> definitions. </simpara> <simpara> In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4. </simpara> <simpara> PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions. </simpara> <simpara> PHP 3 does not support variable numbers of arguments to functions, although default arguments are supported (see <link linkend="functions.arguments.default">Default argument values</link> for more information). PHP 4 supports both: see <link linkend="functions.variable-arg-list">Variable-length argument lists</link> and the function references for <function>func_num_args</function>, <function>func_get_arg</function>, and <function>func_get_args</function> for more information. </simpara> </sect1> <sect1 id="functions.arguments"> <title>Function arguments</title> <simpara> Information may be passed to functions via the argument list, which is a comma-delimited list of variables and/or constants. </simpara> <para> PHP supports passing arguments by value (the default), <link linkend="functions.arguments.by-reference">passing by reference</link>, and <link linkend="functions.arguments.default">default argument values</link>. Variable-length argument lists are supported only in PHP 4 and later; see <link linkend="functions.variable-arg-list">Variable-length argument lists</link> and the function references for <function>func_num_args</function>, <function>func_get_arg</function>, and <function>func_get_args</function> for more information. A similar effect can be achieved in PHP 3 by passing an array of arguments to a function: <informalexample> <programlisting role="php"> function takes_array($input) { echo "$input[0] + $input[1] = ", $input[0]+$input[1]; } </programlisting> </informalexample> </para> <sect2 id="functions.arguments.by-reference"> <title>Making arguments be passed by reference</title> <simpara> By default, function arguments are passed by value (so that if you change the value of the argument within the function, it does not get changed outside of the function). If you wish to allow a function to modify its arguments, you must pass them by reference. </simpara> <para> If you want an argument to a function to always be passed by reference, you can prepend an ampersand (&) to the argument name in the function definition: <informalexample> <programlisting role="php"> function add_some_extra(&$string) { $string .= 'and something extra.'; } $str = 'This is a string, '; add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' </programlisting> </informalexample> </para> <para> If you wish to pass a variable by reference to a function which does not do this by default, you may prepend an ampersand to the argument name in the function call: <informalexample> <programlisting role="php"> function foo ($bar) { $bar .= ' and something extra.'; } $str = 'This is a string, '; foo ($str); echo $str; // outputs 'This is a string, ' foo (&$str); echo $str; // outputs 'This is a string, and something extra.' </programlisting> </informalexample> </para> </sect2> <sect2 id="functions.arguments.default"> <title>Default argument values</title> <para> A function may define C++-style default values for scalar arguments as follows: <informalexample> <programlisting role="php"> function makecoffee ($type = "cappucino") { return "Making a cup of $type.\n"; } echo makecoffee (); echo makecoffee ("espresso"); </programlisting> </informalexample> </para> <para> The output from the above snippet is: <screen> Making a cup of cappucino. Making a cup of espresso. </screen> </para> <simpara> The default value must be a constant expression, not (for example) a variable or class member. </simpara> <para> Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected. Consider the following code snippet: <informalexample> <programlisting role="php"> function makeyogurt ($type = "acidophilus", $flavour) { return "Making a bowl of $type $flavour.\n"; } echo makeyogurt ("raspberry"); // won't work as expected </programlisting> </informalexample> </para> <para> The output of the above example is: <screen> Warning: Missing argument 2 in call to makeyogurt() in /usr/local/etc/httpd/htdocs/php3test/functest.html on line 41 Making a bowl of raspberry . </screen> </para> <para> Now, compare the above with this: <informalexample> <programlisting role="php"> function makeyogurt ($flavour, $type = "acidophilus") { return "Making a bowl of $type $flavour.\n"; } echo makeyogurt ("raspberry"); // works as expected </programlisting> </informalexample> </para> <para> The output of this example is: <screen> Making a bowl of acidophilus raspberry. </screen> </para> </sect2> <sect2 id="functions.variable-arg-list"> <title>Variable-length argument lists</title> <simpara> PHP 4 has support for variable-length argument lists in user-defined functions. This is really quite easy, using the <function>func_num_args</function>, <function>func_get_arg</function>, and <function>func_get_args</function> functions. </simpara> <simpara> No special syntax is required, and argument lists may still be explicitly provided with function definitions and will behave as normal. </simpara> </sect2> </sect1> <sect1 id="functions.returning-values"> <title>Returning values</title> <para> Values are returned by using the optional return statement. Any type may be returned, including lists and objects. <informalexample> <programlisting role="php"> function square ($num) { return $num * $num; } echo square (4); // outputs '16'. </programlisting> </informalexample> </para> <para> You can't return multiple values from a function, but similar results can be obtained by returning a list. <informalexample> <programlisting role="php"> function small_numbers() { return array (0, 1, 2); } list ($zero, $one, $two) = small_numbers(); </programlisting> </informalexample> </para> <para> To return a reference from a function, you have to use the reference operator & in both the function declaration and when assigning the returned value to a variable: <informalexample> <programlisting role="php"> function &returns_reference() { return $someref; } $newref =&returns_reference(); </programlisting> </informalexample> </para> </sect1> <sect1 id="functions.old-syntax"> <title><literal>old_function</literal></title> <simpara> The <literal>old_function</literal> statement allows you to declare a function using a syntax identical to PHP/FI2 (except you must replace 'function' with 'old_function'. </simpara> <simpara> This is a deprecated feature, and should only be used by the PHP/FI2->PHP 3 convertor. </simpara> <warning> <para> Functions declared as <literal>old_function</literal> cannot be called from PHP's internal code. Among other things, this means you can't use them in functions such as <function>usort</function>, <function>array_walk</function>, and <function>register_shutdown_function</function>. You can get around this limitation by writing a wrapper function (in normal PHP 3 form) to call the <literal>old_function</literal>. </para> </warning> </sect1> <sect1 id="functions.variable-functions"> <title>Variable functions</title> <para> PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth. </para> <para> <example> <title>Variable function example</title> <programlisting role="php"> <?php function foo() { echo "In foo()<br>\n"; } function bar( $arg = '' ) { echo "In bar(); argument was '$arg'.<br>\n"; } $func = 'foo'; $func(); $func = 'bar'; $func( 'test' ); ?> </programlisting> </example> </para> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/hk/language/oop.xml +++ phpdoc/hk/language/oop.xml <chapter id="language.oop"> <title>Ãþ§O©Mª«¥ó</title> <sect1 id="keyword.class"> <title><literal>Ãþ§O</literal></title> <para> Ãþ§Oªº·§©À²³æ¨Ó»¡´N¬O¤@¤p¬q(¤]¥i¯à¬O¤@¤j¬q)¦³¤@Ó¦WºÙªºµ{¦¡¬q¡A ¨ä¤º¦³ÅܼƥH¤Î¨Ï¥Î³o¨ÇÅܼƪº¨ç¼Æ¡C ©w¸qÃþ§Oªº¥yªk½Ð¬Ý¤U¦C¥Ü½d¡G <informalexample> <programlisting role="php"> <?php class Cart { var $items; // Items in our shopping cart // Add $num articles of $artnr to the cart function add_item ($artnr, $num) { $this->items[$artnr] += $num; } // Take $num articles of $artnr out of the cart function remove_item ($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } ?> </programlisting> </informalexample> </para> <para> ¤W±©w¸q¤F¤@Ó¥s°µ Cart ªºÃþ§O(«ö: ¼ÒÀÀºô¤WÁʪ«¨®)¡C ¥¦¥]¬A¤F¤@Ó¦WºÙ¯Á¤Þ¦¡ªº°}¦C¥H¨âÓ¤À§O¥Î¨Ó¦b°}¦C¤¤¥[¤J¤Î¦©°£ª««~ªº¨ç¼Æ¡C </para> <note> <simpara> In PHP 4, only constant initializers for <literal>var</literal> variables are allowed. Use constructors for non-constant initializers. </simpara> <informalexample> <programlisting role="php"> /* None of these will work in PHP 4. */ class Cart { var $todays_date = date("Y-m-d"); var $name = $firstname; var $owner = 'Fred ' . 'Jones'; } /* This is how it should be done. */ class Cart { var $todays_date; var $name; var $owner; function Cart() { $this->todays_date = date("Y-m-d"); $this->name = $GLOBALS['firstname']; /* etc. . . */ } } </programlisting> </informalexample> </note> <para> Ãþ§O¬O¤@ºØÅܼƪº§ÎºA¡C ¤]´N¬O»¡¹³ integer¡B string µ¥¦ó¥H§â¬YÃþ§O§ÎºA«ü¬£µ¹ÅܼơC ¤£¦Pªº¬O«Ø¥ß¬YÓÃþ§OªºÅܼƻÝn¥Î¨ì new «ü¥O («ö: ³o¼Ë²£¥ÍªºÅܼƳN»y¤W¥s°µ«Ø¥ß¤F¤@Óª«¥ó)¡G </para> <informalexample> <programlisting role="php"> $cart = new Cart; $cart->add_item("10", 1); </programlisting> </informalexample> <para> ¤W¨Ò«Ø¥ß¤F¤@Ó¥s°µ cart ªºª«¥ó¡A ¥¦ªºÃþ§O¬O $Cart¡C (ÁÙ°O±o PHP ¤¤¤j¤p¼g¬O¤£¤@¼Ëªº¶Ü ?) ¸òµÛ½Õ¥Î¸Óª«¥ó¤¤ªº add_item ¨ç¼Æ¦b°}¦C¤¤¥[¤J¤@Ó½s¸¹ '10' ªºªF¦è¡C </para><para> Ãþ§O¤]¥i¥H¬O¨ä¥LÃþ§Oªº©µ¦ù¡C ·sªºÃþ§OÄ~©Ó¤FªºÃþ§O¤§¥~ÁÙ¥i¥H¥[¤W¦Û¤vªºÅܼƤΥ\¯à¡C ©w¸q©µ¦ùÃþ§Oªº¤èªk¬O©w¸qÃþ§O®É¥[¤W extend ³oÓÃöÁä¦r¡C ¤£¹L¤£·Ç©w¦h«Ä~©ÓÃö«Y¡C </para> <informalexample> <programlisting role="php"> class Named_Cart extends Cart { var $owner; function set_owner ($name) { $this->owner = $name; } } </programlisting> </informalexample> <para> ¤W±·s©w¸q¤F Named_Cart Ãþ§O¡A ¥¦¾Ö¦³ Cart Ãþ§O¤¤©Ò¦³ªºÅÜ¼Æ¤Î¨ç¼Æ¥t¥[¤W¤@Ó¦s¥Î¤á¦W¦rªºÅܼƤΦs¨ú¸ÓÅܼƪº¨ç¼Æ¡G set_owner()¡C §A¥i¥Î¦Ñ¤èªk®Ú¾Ú·sÃþ§O«Ø¥ß¤@Óª«¥ó¡A ²{¦b³oÓª«¥ó°£¤F¥i¥H¹³¥H«e¤@¼Ë¥[´îªF¦è¥~ÁÙ¥i¥H³]©w¥Î¤á¦W¦r¡G </para> <informalexample> <programlisting role="php"> $ncart = new Named_Cart; // Create a named cart $ncart->set_owner ("kris"); // Name that cart print $ncart->owner; // print the cart owners name $ncart->add_item ("10", 1); // (inherited functionality from cart) </programlisting> </informalexample> <para> ¦b©w¸qÃþ§O®É $this ³oÓÅܼƬO«üª«¥ó¥»¨¡C ¦bª«¥ó¤§¤¤§A¤@©wn¥Î $this->something ªº¥y¦¡¨Ó½Õ¥Î©Î¦s¨úª«¥ó¤¤ªºÅܼƩM¨ç¼Æ¡C ¡C Both in and outside of the object you do not need a $ when accessing an object's properties. </para> <informalexample> <programlisting role="php"> $ncart->owner = "chris"; // no $ $ncart->$owner = "chris"; // this is invalid because $ncart->$owner = $ncart->"" $myvar = 'owner'; $ncart->$myvar = "chris"; // this is valid because $ncart->$myvar = $ncart->owner </programlisting> </informalexample> <para> Constructors «Ø³yªÌ ¬OÃþ§O¤¤ªº¤@Ó¨ç¼Æ¡C ¥¦·|¦b¨CÓ®Ú¾Ú¸ÓÃþ§O²£¥Íª«¥ó®É¦Û°Ê³Q°õ¦æ¡C §â¨ç¼ÆÅܦ¨«Ø³yªÌ¨ç¼Æªº¤èªk¬O§â¸Ó¨ç¼Æ©R¦W¦¨©MÃþ§Oªº¦W¦r¤@¼Ë¡C </para> <informalexample> <programlisting role="php"> class Auto_Cart extends Cart { function Auto_Cart () { $this->add_item ("10", 1); } } </programlisting> </informalexample> <para> ¤W±§ÚÌ©w¸q¤F Auto_Cart Ãþ§O¡C ¥¦¬O¤@Ó Cart Ãþ§O¥[¤W¤F¤@ӫسyªÌ¨ç¼Æ¡A ³o¨ç¼Æ·|¦Û°Ê·|¦b°}¦C (©ÎªÌ»¡¬OÁʪ«³æ) ¤¤¥[¤W¤@Ó '10' ¸¹ªºªF¦è¡C ¥t¥~«Ø³yªÌ¨ç¼Æ¤]¥i¥H±µ¨ü°Ñ¼Æ¡A ³o¥O«Ø¥ßª«¥ó®É¦³§ó¤jªº¼u©Ê¡C </para> <informalexample> <programlisting role="php"> class Constructor_Cart extends Cart { function Constructor_Cart ($item = "10", $num = 1) { $this->add_item ($item, $num); } } // Shop the same old boring stuff. $default_cart = new Constructor_Cart; // Shop for real... $different_cart = new Constructor_Cart ("20", 17); </programlisting> </informalexample> <caution> <simpara> ¹ï©óÄ~©ÓÃþ§O¦Ó¨¥¡A ¦pªG¤lÃþ§O©M¤÷Ãþ§O³£¦³«Ø³yªÌ¨ç¼Æ¸Ü¡A «Ø¥ßÄ~©ÓÃþ§Oªºª«¥ó®É¨Ã¤£·|¦P®É½Õ¥Î¥ÀÃþ§Oªº«Ø³yªÌ¨ç¼Æ¡C </simpara> </caution> </sect1> <sect1 id="language.oop.newref"> <title>References inside the constructor</title> <para> Creating references within the constructor can lead to confusing results. This tutorial-like section helps you to avoid problems. <informalexample> <programlisting role="php"> class foo { function foo($name) { // create a reference inside the global array $globalref global $globalref; $globalref[] = &$this; // set name to passed value $this->setName($name); // and put it out $this->echoName(); } function echoName() { echo "<br>",$this->Name; } function setName($name) { $this->Name = $name; } } </programlisting> </informalexample> </para> <para> Let us check out if there is a difference between <varname>$bar1</varname> which has been created using the copy <literal>=</literal> operator and <varname>$bar2</varname> which has been created using the reference <literal>=&</literal> operator... <informalexample> <programlisting role="php"> $bar1 = new foo('set in constructor'); $bar1->echoName(); $globalref[0]->echoName(); /* output: set in constructor set in constructor set in constructor */ $bar2 =& new foo('set in constructor'); $bar2->echoName(); $globalref[1]->echoName(); /* output: set in constructor set in constructor set in constructor */ </programlisting> </informalexample> </para> <para> Apparently there is no difference, but in fact there is a very significant one: <varname>$bar1</varname> and <varname>$globalref[0]</varname> are _NOT_ referenced, they are NOT the same variable. This is because "new" does not return a reference by default, instead it returns a copy. <note> <simpara> There is no performance loss (since php 4 and up use reference counting) returning copies instead of references. On the contrary it is most often better to simply work with copies instead of references, because creating references takes some time where creating copies virtually takes no time (unless none of them is a large array or object and one of them gets changed and the other(s) one(s) subsequently, then it would be wise to use references to change them all concurrently). </simpara> </note> To prove what is written above let us watch the code below. <informalexample> <programlisting role="php"> // now we will change the name. what do you expect? // you could expect that both $bar and $globalref[0] change their names... $bar1->setName('set from outside'); // as mentioned before this is not the case. $bar1->echoName(); $globalref[0]->echoName(); /* output: set on object creation set from outside */ // let us see what is different with $bar2 and $globalref[1] $bar2->setName('set from outside'); // luckily they are not only equyl, they are thesame variable // thus $bar2->Name and $globalref[1]->Name are the same too $bar2->echoName(); $globalref[1]->echoName(); /* output: set from outside set from outside */ </programlisting> </informalexample> </para> <para> Another final example, try to understand it. <informalexample> <programlisting role="php"> class a { function a($i) { $this->value = $i; // try to figure out why we do not need a reference here $this->b = new b($this); } function createRef() { $this->c = new b($this); } function echoValue() { echo "<br>","class ",get_class($this),': ',$this->value; } } class b { function b(&$a) { $this->a = &$a; } function echoValue() { echo "<br>","class ",get_class($this),': ',$this->a->value; } } // try to undestand why using a simple copy here would yield // in an undesired result in the *-marked line $a =& new a(10); $a->createRef(); $a->echoValue(); $a->b->echoValue(); $a->c->echoValue(); $a->value = 11; $a->echoValue(); $a->b->echoValue(); // * $a->c->echoValue(); /* output: class a: 10 class b: 10 class b: 10 class a: 11 class b: 11 class b: 11 */ </programlisting> </informalexample> </para> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/hk/language/operators.xml +++ phpdoc/hk/language/operators.xml <chapter id="language.operators"> <title>¦UÃþ¹Bºâ²Å</title> <simpara> </simpara> <sect1 id="language.operators.arithmetic"> <title>ºâ³N¹Bºâ²Å</title> <simpara> ÁÙ°O±o¤¤¤p¾Ç±Ðªº¥[´î¼°£¶Ü ¡H ³o¨Çºâ²Å´N¬O¨º¼Ë¥Îªº¡C </simpara> <table> <title>ºâ³N¹Bºâ²Å</title> <tgroup cols="3"> <thead> <row> <entry>¥Ü½d </entry> <entry>ºâ²Å¦WºÙ</entry> <entry>¹Bºâµ²ªG</entry> </row> </thead> <tbody> <row> <entry>$a + $b</entry> <entry>¥[ªk Addition</entry> <entry>$a ¡B$b ªº©M</entry> </row> <row> <entry>$a - $b</entry> <entry> ´îªk Subtraction </entry> <entry>$a¡B $b ªº®t</entry> </row> <row> <entry>$a * $b</entry> <entry> ¼ªk Multiplication </entry> <entry>$a ¡B$b ªº¿n</entry> </row> <row> <entry>$a / $b</entry> <entry>°£ªk Division</entry> <entry>$a ¡B$b ªº°Ó</entry> </row> <row> <entry>$a % $b</entry> <entry>¾l¼Æ Modulus</entry> <entry>$a °£¥H $b «áªº¾l¼Æ</entry> </row> </tbody> </tgroup> </table> <simpara> The division operator ("/") returns an integer value (the result of an integer division) if the two operands are integers (or strings that get converted to integers) and the quotient is an integer. If either operand is a floating-point value, or the operation results in a non-integer value, a floating-point value is returned. </simpara> </sect1> <sect1 id="language.operators.assignment"> <title>«ü©wÅܼƪºÈ</title> <simpara> °ò¥»ªº«ü¬£ºâ²Å¬O '='¡C n¥ý¤J¬°¥Dªº¥H¬°³o¥Nªí 'µ¥©ó'¡A ¥¦¨ä¹ê¬O«ün§â '=' ¸¹¥k±ªºÈ¦s¨ì¥ª±ªº¹Bºâ¤¸¤§¤¤¡C </simpara> <para> «ü©wÈ¥»¨¤]¬OÓªí¹F¦¡¡A ¥¦©Ò¥NªíªºÈ´N¬O¥¦n«ü©wªºÈ¡C ´N¬O»¡ "$a = 3" ªºÈ´N¬O 3¡C ³o¥i¥HÅý§A³y¤@¨Ç¦³½ìªº»y¥y: <informalexample> <programlisting role="php"> $a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4. </programlisting> </informalexample> </para> <para> ¥t¦³¤@Ãþ¹Bºâ²Å§âpºâ©M«ü¬£ªº¾Þ§@¦X¦Ó¬°¤@¡C ©Ò¦³ªº¤G¤¸ºâ²Å³£¥i¥H¥Î¦X¨Öªº¥y¦¡¨ú¥ÎÅܼƪºÈ¨Ó°µ¾Þ§@¦P®É§â¹Bºâµ²ªG¦s¦^¨ì¸ÓÅܼƤ§¤¤¡C ½Ð¬Ý¤U¨Ò: <informalexample> <programlisting role="php"> $a = 3; $a += 5; // sets $a to 8, as if we had said: $a = $a + 5; $b = "Hello "; $b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!"; </programlisting> </informalexample> </para> <para> ª`·N«ü¬£ªº°Ê§@¬O§â¼Æ¾Ú§Û¨ì·sªºÅܼƤ§¤¤¥h¡C ¾Þ§@§¹¦¨«á·sªºÅܼƸò¥»¨Óªº¦A¤]¨S¦³ÃöÁp¡C ·í§A»Ýn§Û¤@ӫܤj«Ü¤jªº°}¦C®É¥i¯à·|»Ýn¸ûªøªº®É¶¡¡C ¦b³o¤è±¡A PHP4 ¤¹³\§A¥Î°Ñ·Óªº¤è¦¡«ü¬£ÅܼơC ¦p¤Uªº¥y¦¡ $var = &$othervar; §¹¦¨«á¨âÓÅܼƳ̲׳£«ü¦V¦P¤@¥÷¸ê®Æ¡A ´Á¶¡¨S¦³¯A¤Î¥ô¦ó§Û¼g¸ê®Æªº°Ê§@¡C ª`·N¦b PHP3 ®É¬O¤£¦Xªkªº¡C To learn more about references, please read <link linkend="language.references">References explained</link>. </para> </sect1> <sect1 id="language.operators.bitwise"> <title>¥H¦ì¤¸¬°³æ¦ìªº¹Bºâ²Å</title> <simpara> ¦ì¤¸¾Þ§@¹Bºâ²Å¤¹³\§A³]©w¾ã¼Æ¤¤¬YӦ줸ªºÈ¡C </simpara> <table> <title>¥H¦ì¤¸¬°³æ¦ìªº¹Bºâ²Å</title> <tgroup cols="3"> <thead> <row> <entry>¥Ü½d </entry> <entry>¹Bºâ²Å¦WºÙ</entry> <entry>¹Bºâµ²ªG</entry> </row> </thead> <tbody> <row> <entry>$a & $b</entry> <entry>And</entry> <entry>·í $a ©M $b ªº¬Û¹ïÀ³¦ì¤¸¬Ò¬O '1' ®É¤~·|¶Ç¦^ 1 ¡C</entry> </row> <row> <entry>$a | $b</entry> <entry>Or</entry> <entry>·í $a ©M $b ªº¬Û¹ïÀ³¦ì¤¸¤¤¦³¤@Ó©Î¥H¤W '1' ®É¤~·|¶Ç¦^ 1 ¡C/entry> </row> <row> <entry>$a ^ $b</entry> <entry>Xor</entry> <entry> ·í $a ©M $b ªº¬Û¹ïÀ³¦ì¤¸¤¤¥u¦³¤@Ó¬O '1' ®É¤~·|¶Ç¦^ 1 ¡C </entry> </row> <row> <entry>~ $a</entry> <entry>Not</entry> <entry> §â $a ùتº¦ì¤¸¨ú¤Ï¬Û¡G 1 ÅÜ 0 ¡B 0 ÅÜ 1 ¡C </entry> </row> <row> <entry>$a << $b</entry> <entry>Shift left</entry> <entry> §â $a ªº¦ì¤¸¦V¥ª²¾°Ê $b ¨º»ò¦h¦ì¡C(¨C²¾¤@¦¸¥Nªí§â $a ¼¥H 2) </entry> </row> <row> <entry>$a >> $b</entry> <entry>Shift right</entry> <entry> §â $a ªº¦ì¤¸¦V¥k²¾°Ê $b ¨º»ò¦h¦ì¡C(¨C²¾¤@¦¸¥Nªí§â $a °£¥H 2) </entry> </row> </tbody> </tgroup> </table> </sect1> <sect1 id="language.operators.comparison"> <title>¥Î¨Ó§@¤ñ¸ûªº¹Bºâ²Å</title> <simpara> ¤ñ¸ûºâ²Å¡A ÅU¦W«ä¸q¬O¥Î¨Ó¤ñ¸û¨âÓȪº¤j¤pªº¡C </simpara> <table> <title>¥Î¨Ó§@¤ñ¸ûªº¹Bºâ²Å</title> <tgroup cols="3"> <thead> <row> <entry>¥Ü½d </entry> <entry>ºâ²Å¦WºÙ</entry> <entry>¹Bºâµ²ªG</entry> </row> </thead> <tbody> <row> <entry>$a == $b</entry> <entry>¬Ûµ¥</entry> <entry>·í¨âªÌ¬Ûµ¥®É¦¨¥ß</entry> </row> <row> <entry>$a === $b</entry> <entry>¥þµ¥</entry> <entry> ·í¨âªÌ¬Ûµ¥¥B§ÎºA¤@¼Ë®É¦¨¥ß(PHP4 ¤~¦³) </entry> </row> <row> <entry>$a != $b</entry> <entry>¤£µ¥</entry> <entry>·í¨âªÌ¤£µ¥®É¦¨¥ß</entry> </row> <row> <entry>$a !== $b</entry> <entry>Not identical</entry> <entry> True if $a is not equal to $b, or they are not of the same type. (PHP 4 only) </entry> </row> <row> <entry>$a < $b</entry> <entry> ¤p©ó </entry> <entry>«eªÌ¤p©ó«áªÌ®É¦¨¥ß</entry> </row> <row> <entry>$a > $b</entry> <entry>¤j©ó</entry> <entry> ·í«eªÌ¤j©ó«áªÌ®É¦¨¥ß /entry> </row> <row> <entry>$a <= $b</entry> <entry>¤p©ó©Îµ¥©ó </entry> <entry>·í«eªÌ¤ñ«áªÌ¤p©Î¨âªÌ¤@¼Ë®É¦¨¥ß</entry> </row> <row> <entry>$a >= $b</entry> <entry> ¤j©ó©Îµ¥©ó </entry> <entry>·í«eªÌ¤ñ«áªÌ¤j©Î¨âªÌ¤@¼Ë®É¦¨¥ß</entry> </row> </tbody> </tgroup> </table> <para> ¥t¤@ºØ¥Î¨Ó§@±ø¥óºâ²Åªº¬O "?:" (©ÎºÙ¤T¤¸ trinary) ºâ²Å¡C ¦b C ¥H¤Î¨ä¥Lµ{¦¡»y¨¥¤¤³£¥i³oÃþºâ²Å¡C <informalexample> <programlisting> (expr1) ? (expr2) : (expr3); </programlisting> </informalexample> ·í expr1 ¦¨¥ß®É¡A ¸Óºâ¦¡ªºÈµ¥©ó expr2 ¡A §_«hºâ¦¡ªºÈµ¥©ó expr3 ¡C </para> </sect1> <sect1 id="language.operators.errorcontrol"> <title>²Î±±¿ù»~ªº¹Bºâ²Å </title> <simpara> PHP ¥u¦³¤@Ӳα±¿ù»~ªººâ²Å: (@)ºâ²Å¡C ¥i¥H§â¥¦¥[¨ì¥ô¦ó¤@¥y PHP «ü¥O¤§«e¡A ©¡®É¸Ó«ü¥O°²¦p²£¥Íĵ§i©Î¿ù»~°T¸¹ªº¸Ü PHP ±N¤£·|²z·|³o¨Çĵ§i¤]¤£·|±Nĵ§i¥y¤l¥´¥X¨Ó¡C </simpara> <simpara> °²¦p³]¸m¤F <link linkend="ini.track-errors">track_errors</link> ¿ï¶µ¡A ©Ò¦³¿ù»~«H®§·|³Q§Û¨ì $php_errormsg ÅܼƤ¤¡C ³oÓÅܼƨ䣬O°}¦C¡A ¨C¦¸¦³·sªº¿ù»~³£·|¨ú¥Nì¨Óªº«H®§¡A ©Ò¥Hn¥Îªº¸ÜnºÉ¦¨ú¥Î¡C </simpara> <para> <informalexample> <programlisting role="php"> <?php /* Intentional SQL error (extra quote): */ $res = @mysql_query ("select name, code from 'namelist") or die ("Query failed: error was '$php_errormsg'"); ?> </programlisting> </informalexample> </para> <simpara> ¥t½Ð¬Ý <function>error_reporting</function>¤@¸`¡C </simpara> <warning> <para> Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why. </para> </warning> </sect1> <sect1 id="language.operators.execution"> <title>¥i°õ¦æ©R¥Oªº¹Bºâ²Å</title> <para> PHP ¤]¦³¤@Ó°õ¦æ©R¥Oªººâ²Å: ˱×ÂI (``)¡C ¯d·N¥¦¨Ã¤£¬O³æ¤Þ¸¹¡I PHP ·|¹³°õ¦æ¥~³¡µ{¦¡¤@¼Ë°õ¦æ¦b `` ¤§¤¤¥]µÛªº¤å¥y¡C ¦p¦³µ²ªG¿é¥Xªº¸Ü¤]·|¥´¥X¨Ó¡C §A¥i¥H§â¦³Ãöªº¿é¥Xµ²ªG¦s¨ìÅܼƤ§¤¤¡C <informalexample> <programlisting role="php"> $output = `ls -al`; echo "<pre>$output</pre>"; </programlisting> </informalexample> </para> <note> <para> The backtick operator is disabled when <link linkend="ini.safe-mode">safe mode</link> is enabled. </para> </note> <para> ¥t½Ð¬Ý <function>system</function>, <function>passthru</function>, <function>exec</function>, <function>popen</function>, ©M <function>escapeshellcmd</function>. </para> </sect1> <sect1 id="language.operators.increment"> <title>Incrementing/Decrementing ¼WÈ/·lȹBºâ²Å </title> <para> PHP ¤ä´© C ¼Ë¦¡ªº«e¸m©M«á¸m¼W·lºâ²Å¡C </para> <table> <title>Increment/decrement ¼W·l¹Bºâ²Å</title> <tgroup cols="3"> <thead> <row> <entry>¥Ü½d</entry> <entry>ºâ²Å¦WºÙ</entry> <entry>§@¥Î</entry> </row> </thead> <tbody> <row> <entry>++$a</entry> <entry>Pre-increment «e¸m¼WÈ</entry> <entry>¥ý§â $a ¼W¥[ 1¡A ¦A¶Ç¦^ $a ªºÈ¡C </entry> </row> <row> <entry>$a++</entry> <entry>Post-increment «á¸m¼WÈ</entry> <entry>¥ý¶Ç¦^ $a ªºÈ¦A§â $a ¼W¥[ 1¡C</entry> </row> <row> <entry>--$a</entry> <entry>Pre-decrement</entry> <entry>¥ý§â $a ´î¥h 1¡A ¦A¶Ç¦^ $a ªºÈ¡C</entry> </row> <row> <entry>$a--</entry> <entry>Post-decrement</entry> <entry>¥ý¶Ç¦^ $a ªºÈ¦A§â $a ´î¥h 1¡C</entry> </row> </tbody> </tgroup> </table> <para> ³o¨à¦³Ó²³æªº¨Ò¤l: <informalexample> <programlisting role="php"> <?php echo "<h3>Postincrement</h3>"; $a = 5; echo "Should be 5: " . $a++ . "<br>\n"; echo "Should be 6: " . $a . "<br>\n"; echo "<h3>Preincrement</h3>"; $a = 5; echo "Should be 6: " . ++$a . "<br>\n"; echo "Should be 6: " . $a . "<br>\n"; echo "<h3>Postdecrement</h3>"; $a = 5; echo "Should be 5: " . $a-- . "<br>\n"; echo "Should be 4: " . $a . "<br>\n"; echo "<h3>Predecrement</h3>"; $a = 5; echo "Should be 4: " . --$a . "<br>\n"; echo "Should be 4: " . $a . "<br>\n"; ?> </programlisting> </informalexample> </para> </sect1> <sect1 id="language.operators.logical"> <title>ÅÞ¿èÃþ¹Bºâ²Å</title> <table> <title>ÅÞ¿èÃþ¹Bºâ²Å</title> <tgroup cols="3"> <thead> <row> <entry>¥Ü½d</entry> <entry>ºâ²Å¦WºÙ</entry> <entry> µ²ªG </entry> </row> </thead> <tbody> <row> <entry>$a and $b</entry> <entry>And</entry> <entry>·í $a¡B$b ³£¬O¯u®É¦¨¥ß¡C</entry> </row> <row> <entry>$a or $b</entry> <entry>Or</entry> <entry> ·í $a¡B$bÀH«K¤@Ó¬O¯u®É´N¦¨¥ß¡C </entry> </row> <row> <entry>$a xor $b</entry> <entry>Xor</entry> <entry>·í $a¡B$b ¤¤¦³¥B¥u¦³¤@Ó¬O¯u®É¦¨¥ß¡C </entry> </row> <row> <entry>! $a</entry> <entry>Not</entry> <entry>µ²ªG¬O $a ªº¬Û¤Ï¡C</entry> </row> <row> <entry>$a && $b</entry> <entry>And</entry> <entry>·í $a¡B$b ³£¬O¯u®É¦¨¥ß¡C</entry> </row> <row> <entry>$a || $b</entry> <entry>Or</entry> <entry>·í $a¡B$bÀH«K¤@Ó¬O¯u®É´N¦¨¥ß¡C</entry> </row> </tbody> </tgroup> </table> <simpara> "and" ©M "or" ¦³¨âÓª©¥»¡A ¥\¯à¬O¤@¼Ëªº¤£¹L¥¦Ì°õ¦æ®ÉªºÀu¥ý¦¸§Ç¤£¦P¡C (½Ð°Ñ¦Ò <link linkend="language.operators.precedence">¹Bºâ²Å¸¹°õ¦æ®ÉªºÀu¥ý¦¸§Ç</link>) </simpara> </sect1> <sect1 id="language.operators.precedence"> <title>¹Bºâ²Åªº¥ý«á¦¸§Ç (¥ý¼°£«á¥[´îªº°ÝÃD)</title> <para> ºâ²ÅªºÀu¥ý¦¸§Ç¨M©w¤Fºâ²Å§@¥Îªºªí¹F¦¡¤§¶¡³s±µªº¦³¦hºò¡C ¤ñ¦p»¡¡A¦b 1 + 5 * 3ªí¹F¦¡¤¤¡A µª®×¬O 16 ¦Ó¤£¬O 18¡A ³o¬O¦]¬° '*' ¸¹¤ñ '+' ¸¹§óÀu¥ý°õ¦æ¡C </para> <para> ¤Uªí¦C¥X¤F±q§C¨ì°ªªºÀu¥ý¦¸§Ç¡C ³Ì¤U±ºâ²Å·|³Ì¥ý³Q°õ¦æ¡C <table> <title>¹Bºâ²ÅÀu¥ý¦¸§Ç</title> <tgroup cols="2"> <thead> <row> <entry>¹Bºâ²ÅÀu¥ý¦¸§Ç </entry> <entry>¦UÃþ¹Bºâ²Å</entry> </row> </thead> <tbody> <row> <entry>¥ª</entry> <entry>,</entry> </row> <row> <entry>¥ª</entry> <entry>or</entry> </row> <row> <entry>¥ª</entry> <entry>xor</entry> </row> <row> <entry>¥ª</entry> <entry>and</entry> </row> <row> <entry>¥k</entry> <entry>print</entry> </row> <row> <entry>¥ª</entry> <entry> = += -= *= /= .= %= &= |= ^= ~= <<= >>= </entry> </row> <row> <entry>¥ª</entry> <entry>? :</entry> </row> <row> <entry>¥ª</entry> <entry>||</entry> </row> <row> <entry>¥ª</entry> <entry>&&</entry> </row> <row> <entry>¥ª</entry> <entry>|</entry> </row> <row> <entry>¥ª</entry> <entry>^</entry> </row> <row> <entry>¥ª</entry> <entry>&</entry> </row> <row> <entry> ¨SÃöÁp non-associative</entry> <entry>== != === !==</entry> </row> <row> <entry> ¨SÃöÁp non-associative</entry> <entry>< <= > >=</entry> </row> <row> <entry>¥ª</entry> <entry><< >></entry> </row> <row> <entry>¥ª </entry> <entry>+ - .</entry> </row> <row> <entry>¥ª </entry> <entry>* / %</entry> </row> <row> <entry>¥k</entry> <entry>! ~ ++ -- (int) (double) (string) (array) (object) @</entry> </row> <row> <entry>¥k</entry> <entry>[</entry> </row> <row> <entry> ¨SÃöÁp non-associative</entry> <entry>new</entry> </row> </tbody> </tgroup> </table> </para> </sect1> <sect1 id="language.operators.string"> <title>¤å¥y¹Bºâ²Å</title> <simpara> ¦³¨âÓ¤å¥y¹Bºâ²Å¥i¥Î¡C ²Ä¤@Ó¬O¦ê±µ¤å¥y¥Îªº('.')¡G ¥¦ªºÈ´N¬O§â¥k±ªº¤å¥y¥[¨ì¥ª±¤å¥yªº§À¤Ú¤W¥h¡C ²Ä¤GÓ´N¬O¦ê±µ«ü¬£¦X¨Ö¨ì¤@¶ôªº ('.=') ºâ²Å¡C ³oÃþ¦X¨Öºâ²Åªº¸Ô±¡½Ð¬Ý <link linkend="language.operators.assignment">«ü¬£¹Bºâ²Å </link>¤@¸`¡C </simpara> <para> <informalexample> <programlisting role="php"> $a = "Hello "; $b = $a . "World!"; // now $b contains "Hello World!" $a = "Hello "; $a .= "World!"; // now $a contains "Hello World!" </programlisting> </informalexample> </para> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/hk/language/references.xml +++ phpdoc/hk/language/references.xml <chapter id="language.references"> <title>References Explained</title> <sect1 id="language.references.whatare"> <title>What References Are</title> <simpara> References are a means in PHP to access the same variable content by different names. They are not like C pointers, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The most close analogy is with Unix filenames and files - variable names are directory entries, while variable contents is the file itself. References can be thought of as hardlinking in Unix filesystem. </simpara> </sect1> <sect1 id="language.references.whatdo"> <title>What References Do</title> <para> PHP references allow you to make two variables to refer to the same content. Meaning, when you do: <informalexample> <programlisting role="php"> $a =& $b </programlisting> </informalexample> it means that <varname>$a</varname> and <varname>$b</varname> point to the same variable. <note> <para> <varname>$a</varname> and <varname>$b</varname> are completely equal here, that's not <varname>$a</varname> is pointing to <varname>$b</varname> or vice versa, that's <varname>$a</varname> and <varname>$b</varname> pointing to the same place. </para> </note> </para> <para> The same syntax can be used with functions, that return references, and with <literal>new</literal> operator (in PHP 4.0.4 and later): <informalexample> <programlisting role="php"> $bar =& new fooclass(); $foo =& find_var ($bar); </programlisting> </informalexample> </para> <note> <para> Unless you use the syntax above, the result of <literal>$bar = new fooclass()</literal> will not be the same variable as <literal>$this</literal> in the constructor, meaning that if you have used reference to <literal>$this</literal> in the constructor, you should use reference assignment, or you get two different objects. </para> </note> <para> The second thing references do is to pass variables by-reference. This is done by making a local variable in a function and a variable in the calling scope reference to the same content. Example: <informalexample> <programlisting role="php"> function foo (&$var) { $var++; } $a=5; foo ($a); </programlisting> </informalexample> will make <varname>$a</varname> to be 6. This happens because in the function <varname>foo</varname> the variable <varname>$var</varname> refers to the same content as <varname>$a</varname>. See also more detailed explanations about <link linkend="language.references.pass">passing by reference</link>. </para> <simpara> The third thing reference can do is <link linkend="language.references.return">return by reference</link>. </simpara> </sect1> <sect1 id="language.references.arent"> <title>What References Are Not</title> <para> As said before, references aren't pointers. That means, the following construct won't do what you expect: <informalexample> <programlisting role="php"> function foo (&$var) { $var =& $GLOBALS["baz"]; } foo($bar); </programlisting> </informalexample> </para> <simpara> What happens is that <varname>$var</varname> in foo will be bound with <varname>$bar</varname> in caller, but then it will be re-bound with <varname>$GLOBALS["baz"]</varname>. There's no way to bind <varname>$bar</varname> in the calling scope to something else using the reference mechanism, since <varname>$bar</varname> is not available in the function foo (it is represented by <varname>$var</varname>, but <varname>$var</varname> has only variable contents and not name-to-value binding in the calling symbol table). </simpara> </sect1> <sect1 id="language.references.pass"> <title>Passing by Reference</title> <para> You can pass variable to function by reference, so that function could modify its arguments. The sytax is as follows: <informalexample> <programlisting role="php"> function foo (&$var) { $var++; } $a=5; foo ($a); // $a is 6 here </programlisting> </informalexample> Note that there's no reference sign on function call - only on function definition. Function definition alone is enough to correctly pass the argument by reference. </para> <para> Following things can be passed by reference: <itemizedlist> <listitem> <simpara> Variable, i.e. <literal>foo($a)</literal> </simpara> </listitem> <listitem> <simpara> New statement, i.e. <literal>foo(new foobar())</literal> </simpara> </listitem> <listitem> <para> Reference, returned from a function, i.e.: <informalexample> <programlisting role="php"> function &bar() { $a = 5; return $a; } foo(bar()); </programlisting> </informalexample> See also explanations about <link linkend="language.references.return">returning by reference</link>. </para> </listitem> </itemizedlist> </para> <para> Any other expression should not be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid: <informalexample> <programlisting role="php"> function bar() // Note the missing & { $a = 5; return $a; } foo(bar)); foo($a = 5) // Expression, not variable foo(5) // Constant, not variable </programlisting> </informalexample> These requirements are for PHP 4.0.4 and later. </para> </sect1> <sect1 id="language.references.return"> <title>Returning References</title> <para> Returning by-reference is useful when you want to use a function to find which variable a reference should be bound to. When returning references, use this syntax: <informalexample> <programlisting role="php"> function &find_var ($param) { ...code... return $found_var; } $foo =& find_var ($bar); $foo->x = 2; </programlisting> </informalexample> In this example, the property of the object returned by the <varname>find_var</varname> function would be set, not the copy, as it would be without using reference syntax. </para> <note> <simpara> Unlike parameter passing, here you have to use <literal>&</literal> in both places - to indicate that you return by-reference, not a copy as usual, and to indicate that reference binding, rather than usual assignment, should be done for <varname>$foo</varname>. </simpara> </note> </sect1> <sect1 id="language.references.unset"> <title>Unsetting References</title> <para> When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed. For example: <informalexample> <programlisting role="php"> $a = 1; $b =& $a; unset ($a); </programlisting> </informalexample> won't unset <varname>$b</varname>, just <varname>$a</varname>. </para> <simpara> Again, it might be useful to think about this as analogous to Unix <command>unlink</command> call. </simpara> </sect1> <sect1 id="language.references.spot"> <title>Spotting References</title> <simpara> Many syntax constructs in PHP are implemented via referencing mechanisms, so everything told above about reference binding also apply to these constructs. Some constructs, like passing and returning by-reference, are mentioned above. Other constructs that use references are: </simpara> <sect2 id="references.global"> <title><literal>global</literal> References</title> <para> When you declare variable as <command>global $var</command> you are in fact creating reference to a global variable. That means, this is the same as: <informalexample> <programlisting role="php"> $var =& $GLOBALS["var"]; </programlisting> </informalexample> </para> <simpara> That means, for example, that unsetting <varname>$var</varname> won't unset global variable. </simpara> </sect2> <sect2 id="references.this"> <title><literal>$this</literal></title> <simpara> In an object method, <varname>$this</varname> is always reference to the caller object. </simpara> </sect2> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/hk/language/types.xml +++ phpdoc/hk/language/types.xml <chapter id="language.types"> <title>¦UÃþÅܼÆ</title> <para> PHP ¥i¥H³B²z¥H¤U¼Æ¾Ú§ÎºA: <itemizedlist> <listitem> <simpara> <link linkend="language.types.array">°}¦C </link> </simpara> </listitem> <listitem> <simpara> <link linkend="language.types.double">¯BÂI¼Æ </link> </simpara> </listitem> <listitem> <simpara> <link linkend="language.types.integer">¾ã¼Æ </link> </simpara> </listitem> <listitem> <simpara> <link linkend="language.types.object">Ãþ§O </link> </simpara> </listitem> <listitem> <simpara> <link linkend="language.types.string">¤å¥y </link> </simpara> </listitem> </itemizedlist> </para> <simpara> µ{¦¡û³q±`¤£¥Î«ü©wÅܼƬO¤°»ò§ÎºA¡C PHP ·|¦b¹B¦æ®Ú¾Ú¸ÓÅܼƩҧtªºÈ¨Ó¦Û¦æ¨M©w¸Ó«ü¬£¤°»òÃþ«¬µ¹ÅܼơC </simpara> <simpara> ¦pªG§A·Q¦Û¤v¨M©wÅܼƪº§ÎºA©Î§ïÅÜÅܼƪº§ÎºA¡A n»ò´N¬O¥Î<link linkend="language.types.typecasting">ÅܼƮM´«</link>¡A ¤£µM´N¬O½Õ¥Î <function>settype</function>¨ç¼Æ¡C </simpara> <simpara> ¥²¶·ª`·N¤£¦P§ÎºAªºÅܼƦb¯S©w±¡ªp¤Uªº¤£¦Pªí²{¡C ³o¦b <link linkend="language.types.type-juggling">Type Juggling</link>¤@¸`¤¤¦³§ó¸Ô²Óªº´yz¡C </simpara> <sect1 id="language.types.integer"> <title>¾ã¼Æ</title> <para> ¾ã¼Æ <informalexample> <programlisting role="php"> $a = 1234; # decimal number $a = -123; # a negative number $a = 0123; # octal number (equivalent to 83 decimal) $a = 0x12; # hexadecimal number (equivalent to 18 decimal) </programlisting> </informalexample> The size of an integer is platform-dependent, although a maximum value of about 2 billion is the usual value (that's 32 bits signed). </para> </sect1> <sect1 id="language.types.double"> <title>¯BÂI¼Æ</title> <para> «ü©w¯BÂI§ÎºA (DOUBLES) ªºÅܼƥi¥Î¥H¤U¥yªk : <informalexample> <programlisting role="php"> $a = 1.234; $a = 1.2e3; </programlisting> </informalexample> The size of a floating point number is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (that's 64 bit IEEE format). </para> <warning id="warn.float-precision"> <para> It is quite usual that simple decimal fractions like <literal>0.1</literal> or <literal>0.7</literal> cannot be converted into their internal binary counterparts without a little loss of precision. This can lead to confusing results: for example, <literal>floor((0.1+0.7)*10)</literal> will usually return <literal>7</literal> instead of the expected <literal>8</literal> as the result of the internal representation really being something like <literal>7.9999999999...</literal>. </para> <para> This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, <literal>1/3</literal> in decimal form becomes <literal>0.3333333. . .</literal>. </para> <para> So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you should use the <link linkend="ref.bc">arbitrary precision math functions</link> or <link linkend="ref.gmp">gmp</link> functions instead. </para> </warning> </sect1> <sect1 id="language.types.string"> <title>¤å¥y </title> <para> «ü©wÅܼƬ°¤å¥y§ÎºA¦³´XºØ»yªk¥i¥H¥Î¡C </para> <para> If the string is enclosed in double-quotes ("), variables within ¦pªG¤å¥y¬O¥ÎÂù¤Þ¸¹¥]°_¨Óªº¸Ü¡A §t¦b¨ä¤¤ªºÅܼƷ|³Q®i¶} (·íµM¤]¨ü¤ÀªR¾¹¤ÀªR®Éªº¨î)¡C ©M C¡B PERL ¤@¼Ë¡A ·Q¥[¤J¯S§Oªº±±¨î²Å¥i¥H«e¸m "\"²Å¸¹: <table> <title>¯S§O±±¨î²Å¸¹¦ê</title> <tgroup cols="2"> <thead> <row> <entry>²Å¸¹¦ê</entry> <entry>§t·N</entry> </row> </thead> <tbody> <row> <entry><literal>\n</literal></entry> <entry>¶}·s¤@¦æ (LF or 0x0A in ASCII)</entry> </row> <row> <entry><literal>\r</literal></entry> <entry> Â_¦æ (CR or 0x0D in ASCII)</entry> </row> <row> <entry><literal>\t</literal></entry> <entry>¸õ®æ (HT or 0x09 in ASCII)</entry> </row> <row> <entry><literal>\\</literal></entry> <entry>backslash ¤Ï±×½u</entry> </row> <row> <entry><literal>\$</literal></entry> <entry>$ ²Å¸¹</entry> </row> <row> <entry><literal>\"</literal></entry> <entry>Âù¤Þ¸¹</entry> </row> <row> <entry><literal>\[0-7]{1,3}</literal></entry> <entry> 8 ¶i¨îªº¦r²Å (¥ª±¥Îªº¬O REGULAR EXPRESSION ªºªí¹F¤èªk¡E) </entry> </row> <row> <entry><literal>\x[0-9A-Fa-f]{1,2}</literal></entry> <entry> 16 ¶i¨îªº¦r²Å </entry> </row> </tbody> </tgroup> </table> </para> <para> §A¥i¥H¦b "\" «á©ñ¤W¥ô¦ó§Oªº¦r¤¸¡A ¦ý¬O¦bµ{¦¡°õ¦æ®É·|¦³Äµ§i¡C(°£«D¥t¥~½Õ³]¡A §_«hĵ§i¤£·|¼vÅTµ{¦¡°õ¦æ¡C </para> <para> ²Ä¤GºØ©w¸q¤å¥yªº¤èªkªº¤èªk¬O¥Î³æ¤Þ¸¹ "'"¡C ¦b³æ¤Þ¸¹¤å¥y¤¤¥u¦³¨âÓ«ö¨î¶µ¡G "\\" ©M "\'"¡A ³o¬O¬°¤F¯à¦b¤å¥y¤¤¥[¤J³æ¤Þ¸¹¡C ³æ¤Þ¸¹¤å¥y¤¤ªºÅܼƨ䣷| ³Q®i¶}¡C </para> <simpara> ¥t¥~ÁÙ¦³¤@ºØ¬É©w¤å¥yªº¤èªk¡G ¥Î HERE DOC ªº¥y¦¡ ("<<<")¡C ª`·Nn¦b <literal><<<</literal>¤§«á¥[¤W¤@Ó¿ëÃѦr¨Ã¥²¶·¦b¤å¥y§¹«áªº²Ä¤@¦æ ³Ì¶}ÀY ¥[¤W¦P¤@Ó¿ëÃѦr¡C </simpara> <simpara> The closing identifier <emphasis>must</emphasis> begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore. </simpara> <para> Here doc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a here doc as with strings. <example> <title>¥Î Here doc ¬É©w¤å¥yªº¥Ü½d</title> <programlisting> <?php $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; /* More complex example, with variables. */ class foo { var $foo; var $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'MyName'; echo <<<EOT My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should print a capital 'A': \x41 EOT; ?> </programlisting> </example> </para> <note> <para> ¦b PHP ª© 4 ¤¤¤~¯à¥Î Here doc ¥yªk. </para> </note> <para> ¤å¥y¥i¥H¥ÎÂI '.' ¦ê±µ°_¨Ó¡Cª`·N¥Î "+" ¸¹¦ê³s¤å¥y¬OµL®Äªº¡C <link linkend="language.operators.string">¤å¥y¹Bºâ²Å </link> ¤@¸`¤¤¦³§ó¸Ô²Óªº¬ö¿ý¡C </para> <para> ¦b¤å¥y¤¤ªº¨C¤@Ó¦r¥À¥i¥H·í¬O¦r¥À°}¦C¤¤ªº¤@ÓÓ¤¸¯À¡A ¦p¦¹¥i¥H¥Î¹³ C ¨º¼Ëªº»yªk¨Ó½Õ¥Î¡A ½Ð¬Ý¤U¨Ò ¡G </para> <para> <example> <title>¥Ü½d¤å¥y </title> <programlisting role="php"> <?php /* Assigning a string. */ $str = "This is a string"; /* Appending to it. */ $str = $str . " with some more text"; /* Another way to append, includes an escaped newline. */ $str .= " and a newline at the end.\n"; /* This string will end up being '<p>Number: 9</p>' */ $num = 9; $str = "<p>Number: $num</p>"; /* This one will be '<p>Number: $num</p>' */ $num = 9; $str = '<p>Number: $num</p>'; /* Get the first character of a string */ $str = 'This is a test.'; $first = $str[0]; /* Get the last character of a string. */ $str = 'This is still a test.'; $last = $str[strlen($str)-1]; ?> </programlisting> </example> </para> <sect2 id="language.types.string.conversion"> <title>¤å¥yÅÜ´« </title> <simpara> ·í¤å¥y³Q·í¦¨¬O¼Æ¦r¨º¼Ë¨Ó¾Þ§@®É¡A ¨ä§ÎºA©Mȸò±q¥H¤Uªº³W«h¡G </simpara> <simpara> ¦pªG¤å¥y¤¤¦³ '.'¡B 'e'¡B 'E'²Å¸¹¡A «h¤å¥y·|Åܦ¨¯BÂI¼Æ¦r¡A ¤Ï¤§«h·|³Q·í¦¨¬O¾ã¼Æ¡C </simpara> <para> ¤å¥y©Ò¥Nªº¼ÆÈµø¤å¥y¶}ÀYªº¦r¥À¦Ó©w¡A ¥un¤å¥y¶}ÀYªº¤£¬O¦X²zªº¼Æ¦r²Å¸¹«h¤å¥yªºÈ´N·|¬O 0¡C ¦X²zªº¼Æ¦r²Å¸¹¥]¬A¥¿t¸¹¡B «á±¸ò 0 ¨ì 9 ªºªü©Ô§B¼Æ¦r¡B ¤p¼ÆÂI¡B ³Ì«á¬O¥i¦³¥iµLªº«ü¼Æ¥Nªí²Å¸¹ ("E" ©Î "e" «á±¸ò¤W¤@¦êªü©Ô§B¼Æ¦r)¡C </para> <simpara> ¦pªG¦bªí¹F¥Ü¤¤ÀY¤@¶µ¬O¤å¥y«h¹Bºâµ²ªGn¬Ý²Ä¤G¶µ¬O¤°»òÅܼƧκA¡C </simpara> <informalexample> <programlisting role="php"> $foo = 1 + "10.5"; // $foo is double (11.5) $foo = 1 + "-1.3e3"; // $foo is double (-1299) $foo = 1 + "bob-1.3e3"; // $foo is integer (1) $foo = 1 + "bob3"; // $foo is integer (1) $foo = 1 + "10 Small Pigs"; // $foo is integer (11) $foo = 1 + "10 Little Piggies"; // $foo is integer (11) $foo = "10.0 pigs " + 1; // $foo is integer (11) $foo = "10.0 pigs " + 1.0; // $foo is double (11) </programlisting> </informalexample> <simpara> ¦b Unix ¤â¥U strtod(3) ¤@±ø¤¤¦³§óÃö©ó³oÃþÅÜ´«ªº¸ê®Æ¡C </simpara> <para> ¦pªGn¸Õ¥Î¥»¸`¤¤¥Ü½dªº¨Ò¤l¡A ¥i¥H§âµ{¦¡½X½Æ»s¨ì¤@Ó·sªºµ{¦¡¬q¤¤¨Ã´¡¤J¤U¦C«ü¥O¦æ¡A ¥¦§i¶D§A¨Ï¥Î¤¤ªºÅܼƬO¤°»ò§ÎºA : <informalexample> <programlisting role="php"> echo "\$foo==$foo; type is " . gettype ($foo) . "<br>\n"; </programlisting> </informalexample> </para> </sect2> </sect1> <sect1 id="language.types.array"> <title>°}¦C</title> <para> PHP ªº°}¦C¦P®É¾Ö¦³«÷Âøªí HASH TABLE ©M ¯Á¤Þªí INDEX TABLE ªº¯S©Ê¡C(ÃöÁp¦r¯Á¤Þ¦¡°}¦C ASSOCIATIVE ARRAY ©M ¥Ú¶q VECTOR ´N¬O¨âÓ¨Ò¤l) </para> <sect2 id="language.types.array.single-dim"> <title>³æºû°}¦C</title> <para> ¦b PHP ¤¤ ¼ÆÈ¯Á¤Þ¦¡©MÃöÁp¦r¯Á¤Þ¦¡³o¨âÃþ°}¦C¨ä¹ê¤W¬O¤@¼Ëªº¡C §A¥i¥Î <function>list</function> ©Î <function>array</function> ¨ç¼Æ¨Ó«Ø¥ß°}¦C¡A ¤]¥i¥Hª½±µ«ü©w°}¦C¤¤¤¸¯ÀªºÈ¡C <informalexample> <programlisting role="php"> $a[0] = "abc"; $a[1] = "def"; $b["foo"] = 13; </programlisting> </informalexample> </para> <para> °²¦p¨S¦³µ¹¥X°}¦C¤¸¯Àªº°Ñ·Ó¼Ð¸¹¡A PHP ·|¦b°}¦C³Ì¥¼ºÝ¥[¤J¤@Ó¤¸¯À¡A ¨Ã§â¼ÆÈ¦s¨ì¸Ó¤¸¯À¤¤¥h¡C ¥Î³oÓ¯SÂI§A¥i¥H«Ü²³æ¦a«Ø¥ß¤@Ó°}¦C¡C <informalexample> <programlisting role="php"> $a[] = "hello"; // $a[2] == "hello" $a[] = "world"; // $a[3] == "world" </programlisting> </informalexample> </para> <para> n§â°}¦C¤¤ªº¤¸¯À±Æ§Ç¡A ¥i«ö°}¦Cªº§ÎºA½Õ¥Î <function>asort</function>, <function>arsort</function>, <function>ksort</function>, <function>rsort</function>, <function>sort</function>, <function>uasort</function>, <function>usort</function>, <function>uksort</function> µ¥¨ç¼Æ¡C </para> <para> <function>count</function> ¨ç¼Æ·|µ¹¥X°}¦C¤¤¦³¦h¤ÖÓ¤¸¯À¡C </para> <para> §A¥i¥H¥Î <function>next</function>¤Î <function>prev</function>¨ç¼Æ¨Ó«ü¦V°}¦C¤¤ªº¤£¦P¤¸¯À¡A ¥t¤@Ó±`¥Îªº¦PÃþ¨ç¼Æ¬O <function>each</function>. </para> </sect2> <sect2 id="language.types.array.multi-dim"> <title>°ªºû°}¦C</title> <para> °ªºû°}¦C(¦hºû°}¦C) ªº¨Ï¥Î¨ä¹ê¬O«Ü²³æªº¡C §A¥un¦b°}¦C«á±¥[¦h¤@Ó¤Þ¼Æ "[ ]"¡A °}¦C«K¦h¤F¤@Óºû¼Æ¡G <informalexample> <programlisting role="php"> $a[1] = $f; # one dimensional examples $a["foo"] = $f; $a[1][0] = $f; # two dimensional $a["foo"][2] = $f; # (you can mix numeric and associative indices) $a[3]["bar"] = $f; # (you can mix numeric and associative indices) $a["foo"][4]["bar"][0] = $f; # four dimensional! </programlisting> </informalexample> </para> <para> PHP3 ¨Ã¤£¤¹³\ª½±µ¦b¤å¥y¤§¤¤°Ñ·Ó¦hºû°}¦C¤¤ªº¤¸¯À¡C ¦p¤U¨Òªºµ{¦¡±N²£¥Í¿ù»~ªºµ²ªG¡C <informalexample> <programlisting role="php"> $a[3]['bar'] = 'Bob'; echo "This won't work: $a[3][bar]"; </programlisting> </informalexample> ¥Î PHP3¡A ¥H¤W¹Bºâªºµ²ªG±N¬O This won't work: Array[bar]¡I ¹ï¥I³oÓ°ÝÃDn¥Î¨ì "." ¤å¥y«÷¦X¹Bºâ²Å¤~¦æ¡G <informalexample> <programlisting role="php"> $a[3]['bar'] = 'Bob'; echo "This will work: " . $a[3][bar]; </programlisting> </informalexample> </para> <para> ¦b PHP4 ª©¥»¤¤¡A ¥Îªá¬A¸¹¦b¤å¥y¤¤§â°}¦C°Ñ·Ó¥]°_¨Ó´N¸Ñ¨M¤F¥H¤Wªº¥j©Ç°ÝÃD : <informalexample> <programlisting role="php"> $a[3]['bar'] = 'Bob'; echo "This will work: {$a[3][bar]}"; </programlisting> </informalexample> </para> <para> n¶ñº¡¤@Ó¦hºûªº°}¦C¦³«Ü¦h¿ìªk¡C ¦ý¤ñ¸ûÃø§ËÀ´ªº¬O¥Î <function>array</function> ¨ç¼Æ¨Ó«ü©wÃöÁp¦r¯Á¤Þ¦¡°}¦C ASSOCIATIVE ARRAY ¤¤ªºÈ¡C ¥H¤U¨â¬qµ{¦¡½Xªº¥\¯à¬O§¹¥þ¤@¼Ëªº : <informalexample> <programlisting role="php"> # Example 1: $a["color"] = "red"; $a["taste"] = "sweet"; $a["shape"] = "round"; $a["name"] = "apple"; $a[3] = 4; # Example 2: $a = array( "color" => "red", "taste" => "sweet", "shape" => "round", "name" => "apple", 3 => 4 ); </programlisting> </informalexample> </para> <para> ¥Î±_ª¬ªº <function>array</function> ¨ç¼Æ¥i¥H«Ø¥ß¥X¦hºûªº°}¦C : <informalexample> <programlisting role="php"> <?php $a = array( "apple" => array( "color" => "red", "taste" => "sweet", "shape" => "round" ), "orange" => array( "color" => "orange", "taste" => "tart", "shape" => "round" ), "banana" => array( "color" => "yellow", "taste" => "paste-y", "shape" => "banana-shaped" ) ); echo $a["apple"]["taste"]; # will output "sweet" ?> </programlisting> </informalexample> </para> </sect2> </sect1> <sect1 id="language.types.object"> <title>ª«¥ó</title> <sect2 id="language.types.object.init"> <title>«Ø¥ßª«¥ó</title> <para> ¥Î NEW «ü¥O¥i¥H§âÅܼƫü©w¬°¬Y¤@ª«¥ó¡C <informalexample> <programlisting role="php"> <?php class foo { function do_foo() { echo "Doing foo."; } } $bar = new foo; $bar->do_foo(); ?> </programlisting> </informalexample> </para> <simpara> For a full discussion, please read the section <link linkend="language.oop">Classes and Objects</link>. </simpara> </sect2> </sect1> <sect1 id="language.types.type-juggling"> <title>¦UÃþÅܼƶ¡ªº¤¬´«</title> <simpara> PHP ¬J¤£»Ýn¤]¤£¤ä´©©ú½T¦a¤½§iÅܼƪº§ÎºA¡C ÅܼƪºÃþ§Îµø¥¦©ÒÀx¦sªº¸ê®Æ¦Ó©w¡G ¦pªG§A§â¤å¥y¦s¨ì var·í¤¤¡A var ·|¬O¤å¥y§ÎÅܼơF ¦pªG¦s¤Jªº¬O¾ã¼Æ var¡A¥¦´NÅܦ¨¾ã¼Æ§Î¡C </simpara> <para> ¤U±¥Î "+" ¥[¸¹ªº¹Bºâ¨Ó¥Ü½d PHP «ç¼Ë¦Û°Ê½Õ¾ãÅܼƪº§ÎºA¡C ¦pªG¥[¼Æ©M³Q¥[¼Æ¤¤¥ô¦ó¤@Ó¬O¯BÂI¼Æ¡A ¹Bºâªºµ²ªG±N·|¬O¯BÂI¼Æ¡A ¤Ï¤§«hµ²ªG·|¬O¾ã¼Æ§ÎºA¡C ª`·N³oºØ½Õ¾ã¥u¬O¥Î¦b¹Bºâ¤§¤¤¡A ¨Ã¤£·|§ïÅÜ¥[¼Æ©M³Q¥[¼Æ¥»¨ÓªºÃþ§Î¡C <informalexample> <programlisting role="php"> $foo = "0"; // $foo is string (ASCII 48) $foo++; // $foo is the string "1" (ASCII 49) $foo += 1; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a double (3.3) $foo = 5 + "10 Little Piggies"; // $foo is integer (15) $foo = 5 + "10 Small Pigs"; // $foo is integer (15) </programlisting> </informalexample> </para> <simpara> Y¶û¤W±ªº¨Ò¤l¥Îªº¥yªk¤ñ¸û¡A ¥i¥H°Ñ¦Ò <link linkend="language.types.string.conversion">¤å¥yÅÜ´«</link>¤@¸`¡C </simpara> <simpara> ¦pªG¤£·Q PHP ¦Û°Ê½Õ¸`Åܼƪº§ÎºA¦Ó¥´ºâ¦Û¤v«ü©wÅܼƦb¹Bºâ¤¤ªº§ÎºA¥i¬Ý <link linkend="language.types.typecasting">§ÎºA®M´«</link>. ¡C ¦p·Q§ïÅÜÅܼƪºÃþ§Î¡A ¥i¨Ï¥Î <function>settype</function>¨ç¼Æ¡C </simpara> <para> °²¦p§A·Q¸Õ¥»¸`¤¤ªº¥Ü¨Ò¡A ¥i¥H§âµ{¦¡½X³¡¥÷½Æ¨î¥X¨ÓµM«á¥[¤W¤U±¤@¦æ«ü¥O¡A §A§i¶D¥¦Åܼƪº¦W¦r¡A ¥¦§i¶D§AÅܼƪº§ÎºA: <informalexample> <programlisting role="php"> echo "\$foo==$foo; type is " . gettype ($foo) . "<br>\n"; </programlisting> </informalexample> </para> <note> <para> PHP ¼È®ÉÁÙ¨S¦³©w¸q«ç§â¤@Ó«D°}¦CÅܼƦ۰ÊÂà´«¦¨°}¦CÅܼơC <informalexample> <programlisting role="php"> $a = 1; // $a is an integer $a[0] = "f"; // $a becomes an array, with $a[0] holding "f" </programlisting> </informalexample> </para> <para> ¤W±ªº¨Ò¤l¬Ý°_¨Ó¦æ±o³q¡G "f" ·|Åܦ¨¬O°}¦C $a ªº²ÄÓ¤¸¯À¡A ²{¦b¬Ý¬Ý¤UӥܨҡG <informalexample> <programlisting role="php"> $a = "1"; // $a is a string $a[0] = "f"; // What about string offsets? What happens? </programlisting> </informalexample> </para> <para> ¥Ñ©ó PHP ¥Î¨Óªí¥Ü¤å¥y¤¤¬YÓ¦r¥Àªº¥yªk©M¥Î¨Óªí¥Ü°}¦C¤¤¬YÓ¤¸¯Àªº¥yªk¬O¤@¼Ëªº¡A ¤W±ªº¨Ò¥y·|²£¥Íª[¸q¡G "f" À³¸Ó¥Nªí¤å¥yÅÜ¼Æ $a ¤¤ªººÓ¦r¥ÀÁÙ¬OÀ³¸Ó¥Nªí°}¦CÅÜ¼Æ $a[] ¤¤ªº²Ä¤@Ó¤¸¯À¡H </para> <para> ¦]¦¹¦b PHP 3.0.12 and PHP 4.0b3-RC4 ¨âÓª©¥»¤¤¡A ¨Ã¨S¦³©w¸q¤WzªºÅÜ´«¸Ó«ç¼Ë°µ¡C ¸Ñ¨Mªº¿ìªk¥Ø«eÁÙ¦b°Q½×·í¤¤¡C </para> </note> <sect2 id="language.types.typecasting"> <title>§ÎºA®M´« </title> <para> ©M C ªº¥yªk¤@¼Ë¡A n§âÅܼƪº§ÎºA®M´«¦¨¥t¤@ºØ§ÎºA¥i¥H¥Î¶ê¬A¸¹ "( )" §â·Q«ü©wªºÃþ§Î¬A°_¨Ó©ñ¦bÅܼƫe±¡C <informalexample> <programlisting role="php"> $foo = 10; // $foo is an integer $bar = (double) $foo; // $bar is a double </programlisting> </informalexample> </para> <para> ¤U¦Cªº®M´«¬O¦Xªkªº¡G <itemizedlist> <listitem> <simpara>(int), (integer) - ·í¦¨¾ã¼Æ</simpara> </listitem> <listitem> <simpara>(real), (double), (float) - ·í¦¨¯BÂI¼Æ</simpara> </listitem> <listitem> <simpara>(string) - ·í¦¨¤å¥y</simpara> </listitem> <listitem> <simpara>(array) - ·í¦¨°}¦C</simpara> </listitem> <listitem> <simpara>(object) - ·í¦¨ª«¥ó</simpara> </listitem> </itemizedlist> </para> <para> ¥Ñ©ó¬A¸¹¤¤¥i¥H©ñ¶iªÅ®æ©M¸õ®æ²Å¸¹¡A ©Ò¥H¤U±ªº«ü¥O¬Oµ¥®Äªº: <informalexample> <programlisting role="php"> $foo = (int) $bar; $foo = ( int ) $bar; </programlisting> </informalexample> </para> <para> ¬Y¨Ç®M´«ªºµ²ªG¨Ã¤£¯à¤@²´¬Ý¥X¨Ó¡A ¶i¦æ³o¼ËªºÂà´«®Én¯d·N¥H¤U´XÂI¡C </para> <para> ·í§â¼Æ¦r©Î¤å¥y®M´«¦¨°}¦C®É¡A ¼ÆÈ©Î¾ã¥y¤å¥y·|¦¨¬°°}¦C¤¤ªººÓ¤¸¯À¡C <informalexample> <programlisting role="php"> $var = 'ciao'; $arr = (array) $var; echo $arr[0]; // outputs 'ciao' </programlisting> </informalexample> </para> <para> ·í§â¼Æ¦r©Î¤å¥y®M´«¦¨ª«¥ó®É¡A ª«¥ó¤¤·|¦Û°Ê²£¥Í¤@Ó¥s SCALAR ªºÄݩʡA ·í¤¤Àx¦s¤F®M´««eÅܼƤ¤ªº¤º®e¡C <informalexample> <programlisting role="php"> $var = 'ciao'; $obj = (object) $var; echo $obj->scalar; // outputs 'ciao' </programlisting> </informalexample> </para> </sect2> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/hk/language/variables.xml +++ phpdoc/hk/language/variables.xml <?xml encoding="iso-8859-1"?> <chapter id="language.variables"> <title>¦UÃþÅܼÆ</title> <sect1 id="language.variables.basics"> <title>°ò¥»ª¾ÃÑ</title> <simpara> ©Ò¦³ PHP ªºÅܼƫe±³£¦³¤@Ó "$" ²Å¸¹¡A «á±¸òµÛÅܼƪº¦W¦r¡C ª`·NÅܼƦWºÙ¬O¤À¤j¤p¼gªº¡C </simpara> <para> 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> For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff). </simpara> </note> <para> <informalexample> <programlisting role="php"> $var = "Bob"; $Var = "Joe"; echo "$var, $Var"; // outputs "Bob, Joe" $4site = 'not yet'; // invalid; starts with a number $_4site = 'not yet'; // valid; starts with an underscore $täyte = 'mansikka'; // valid; '?' is ASCII 228. </programlisting> </informalexample> </para> <para> ¦b PHP3 ªº®É´Á¡A «ü©wµ¹Åܼƪº³£¬OÈ¡A ¨Ò¦p§â¤@¦æªí¹F¦¡«ü©wµ¹Åܼƪº¸Ü¡A ÅܼƩÒÀx¦sªº±N¬Oªí¹F¦¡pºâ¥X¨ÓªºÈ¡C ¥¦ªº¤Þ¥Ó·N¸q¬O¦pªG§A§â¬YÅܼƫü©wµ¹¥t¤@ÓÅܼƫá¡A §ïÅܨ䤤¤@Óªº¤º®e¨Ã¤£·|¼vÅT¥t¥~ªº¤@Ó¡C ·Q¶i¤@¨BÁA¸Ñ³oºØ«ü©wÅܼƤèªk¡A ½Ð¬Ý <link linkend="language.expressions">ªí¹F¦¡</link>¤@¸`¡C </para> <para> PHP4 «h´£¨Ñ¤F¥t¤@ºØ«ü©wÅܼƪº¤èªk: <emphasis>ÅܼưѷÓ</emphasis>. ·N«ä¬O·sÅܼƥu¬O°Ñ·Ó¥»¨ÓªºÅܼƤ¤ªºÈ¡C (´«Ó»¡ªk¡A ·sªºÅܼƥu¬O¥»¨ÓªºÅܼƪº"§O¦W"¡A ¤]¥i»¡·sÅܼƬOÂÂÅܼƪº"«ü¼Ð") µL½×§ïÅÜþ¤@ÓÅܼơA §ï°Ê³£¦P®É¤Ï¬M¦b¥t¤@ÓÅܼƤ¤¡C ¥Ñ©óÂÂÅܼƪºÈ¨Ã¨S¦³³Q§Û¨ì·sÅܼƤ¤¡A ©Ò¥H³oºØ«ü©wÅܼƪº¤èªk°õ¦æ®É·|§Ö¤@ÂI¡A ¦ý¤]¥u¬O¤@ÂI¦Ó¤w¡C ¥u¦³¦b«ü©w¤j«¬ªº°}¦C¡Bª«¥ó©Î¬O¦b¤j¶qªº°j°é¾Þ§@¤¤¤~·|¬Ý¥X®Ä¯q¨Ó¡C </para> <para> §@°Ñ·Ó«ü©wªº¤è¦¡¬O¦b³Q°Ñ·ÓªºÅܼÆ(ÂÂÅܼÆ)«e¥[¤WÓ(&) ²Å¸¹¡C ¹³¤U±ªº¥Ü¨Ò·|³sÄò¥´¥X¨â¥y¬Û¦Pªº¥y¤l: <informalexample> <programlisting role="php"> <?php $foo = 'Bob'; // Assign the value 'Bob' to $foo $bar = &$foo; // Reference $foo via $bar. $bar = "My name is $bar"; // Alter $bar... echo $foo; // $foo is altered too. echo $bar; ?> </programlisting> </informalexample> </para> <para> ¦³¤@ÂI«Ü«nªº¬O¥u¦³¯u¥¿ªºÅܼƤ~¥i¥H§@¬°ÂÂÅܼƳQ°Ñ·Ó¡C <informalexample> <programlisting role="php"> <?php $foo = 25; $bar = &$foo; // This is a valid assignment. $bar = &(24 * 7); // Invalid; references an unnamed expression. function test() { return 25; } $bar = &test(); // Invalid. ?> </programlisting> </informalexample> </para> </sect1> <sect1 id="language.variables.predefined"> <title>¤w¹w³]¦nªºÅܼÆ</title> <simpara> 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. </simpara> <simpara> Despite these factors, here is a list of predefined variables available under a stock installation of PHP 3 running as a module under a stock installation of <ulink url="&url.apache;">Apache</ulink> 1.3.6. </simpara> <simpara> For a list of all predefined variables (and lots of other useful information), please see (and use) <function>phpinfo</function>. </simpara> <note> <simpara> This list is neither exhaustive nor intended to be. It is simply a guideline as to what sorts of predefined variables you can expect to have access to in your script. </simpara> </note> <sect2 id="language.variables.predefined.apache"> <title>Apache variables</title> <simpara> These variables are created by the <ulink url="&url.apache;">Apache</ulink> webserver. If you are running another webserver, there is no guarantee that it will provide the same variables; it may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the <ulink url="&url.cgispec;">CGI 1.1 specification</ulink>, so you should be able to expect those. </simpara> <simpara> Note that few, if any, of these will be available (or indeed have any meaning) if running PHP on the command line. </simpara> <para> <variablelist> <varlistentry> <term>GATEWAY_INTERFACE</term> <listitem> <simpara> What revision of the CGI specification the server is using; i.e. 'CGI/1.1'. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_NAME</term> <listitem> <simpara> The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_SOFTWARE</term> <listitem> <simpara> Server identification string, given in the headers when responding to requests. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_PROTOCOL</term> <listitem> <simpara> Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0'; </simpara> </listitem> </varlistentry> <varlistentry> <term>REQUEST_METHOD</term> <listitem> <simpara> Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'. </simpara> </listitem> </varlistentry> <varlistentry> <term>QUERY_STRING</term> <listitem> <simpara> The query string, if any, via which the page was accessed. </simpara> </listitem> </varlistentry> <varlistentry> <term>DOCUMENT_ROOT</term> <listitem> <simpara> The document root directory under which the current script is executing, as defined in the server's configuration file. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_ACCEPT</term> <listitem> <simpara> Contents of the <literal>Accept:</literal> header from the current request, if there is one. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_ACCEPT_CHARSET</term> <listitem> <simpara> Contents of the <literal>Accept-Charset:</literal> header from the current request, if there is one. Example: 'iso-8859-1,*,utf-8'. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_ACCEPT_ENCODING</term> <listitem> <simpara> Contents of the <literal>Accept-Encoding:</literal> header from the current request, if there is one. Example: 'gzip'. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_ACCEPT_LANGUAGE</term> <listitem> <simpara> Contents of the <literal>Accept-Language:</literal> header from the current request, if there is one. Example: 'en'. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_CONNECTION</term> <listitem> <simpara> Contents of the <literal>Connection:</literal> header from the current request, if there is one. Example: 'Keep-Alive'. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_HOST</term> <listitem> <simpara> Contents of the <literal>Host:</literal> header from the current request, if there is one. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_REFERER</term> <listitem> <simpara> The address of the page (if any) which referred the browser to the current page. This is set by the user's browser; not all browsers will set this. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_USER_AGENT</term> <listitem> <simpara> Contents of the <literal>User_Agent:</literal> header from the current request, if there is one. This is a string denoting the browser software being used to view the current page; i.e. <computeroutput>Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586)</computeroutput>. Among other things, you can use this value with <function>get_browser</function> to tailor your page's functionality to the capabilities of the user's browser. </simpara> </listitem> </varlistentry> <varlistentry> <term>REMOTE_ADDR</term> <listitem> <simpara> The IP address from which the user is viewing the current page. </simpara> </listitem> </varlistentry> <varlistentry> <term>REMOTE_PORT</term> <listitem> <simpara> The port being used on the user's machine to communicate with the web server. </simpara> </listitem> </varlistentry> <varlistentry> <term>SCRIPT_FILENAME</term> <listitem> <simpara> The absolute pathname of the currently executing script. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_ADMIN</term> <listitem> <simpara> The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_PORT</term> <listitem> <simpara> The port on the server machine being used by the web server for communication. For default setups, this will be '80'; using SSL, for instance, will change this to whatever your defined secure HTTP port is. </simpara> </listitem> </varlistentry> <varlistentry> <term>SERVER_SIGNATURE</term> <listitem> <simpara> String containing the server version and virtual host name which are added to server-generated pages, if enabled. </simpara> </listitem> </varlistentry> <varlistentry> <term>PATH_TRANSLATED</term> <listitem> <simpara> Filesystem- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping. </simpara> </listitem> </varlistentry> <varlistentry> <term>SCRIPT_NAME</term> <listitem> <simpara> Contains the current script's path. This is useful for pages which need to point to themselves. </simpara> </listitem> </varlistentry> <varlistentry> <term>REQUEST_URI</term> <listitem> <simpara> The URI which was given in order to access this page; for instance, '/index.html'. </simpara> </listitem> </varlistentry> </variablelist> </para> </sect2> <sect2 id="language.variables.predefined.environment"> <title>Environment variables</title> <simpara> These variables are imported into PHP's global namespace from the environment under which the PHP parser is running. Many are provided by the shell under which PHP is running and different systems are likely running different kinds of shells, a definitive list is impossible. Please see your shell's documentation for a list of defined environment variables. </simpara> <simpara> Other environment variables include the CGI variables, placed there regardless of whether PHP is running as a server module or CGI processor. </simpara> </sect2> <sect2 id="language.variables.predefined.php"> <title>PHP variables</title> <simpara> These variables are created by PHP itself. The <varname>$HTTP_*_VARS</varname> variables are available only if the <link linkend="ini.track-vars">track_vars</link> configuration is turned on. When enabled, the variables are always set, even if they are empty arrays. This prevents a malicious user from spoofing these variables. </simpara> <note> <para> As of PHP 4.0.3, <link linkend="ini.track-vars">track_vars</link> is always turned on, regardless of the configuration file setting. </para> </note> <para> If the <link linkend="ini.register-globals">register_globals</link> directive is set, then these variables will also be made available in the global scope of the script; i.e., separate from the <varname>$HTTP_*_VARS</varname> arrays. This feature should be used with care, and turned off if possible; while the <varname>$HTTP_*_VARS</varname> variables are safe, the bare global equivalents can be overwritten by user input, with possibly malicious intent. If you cannot turn off <link linkend="ini.register-globals">register_globals</link>, you must take whatever steps are necessary to ensure that the data you are using is safe. </para> <para> <variablelist> <varlistentry> <term>argv</term> <listitem> <simpara> Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string. </simpara> </listitem> </varlistentry> <varlistentry> <term>argc</term> <listitem> <simpara> Contains the number of command line parameters passed to the script (if run on the command line). </simpara> </listitem> </varlistentry> <varlistentry> <term>PHP_SELF</term> <listitem> <simpara> The filename of the currently executing script, relative to the document root. If PHP is running as a command-line processor, this variable is not available. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_COOKIE_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script via HTTP cookies. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_GET_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script via the HTTP GET method. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_POST_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script via the HTTP POST method. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_POST_FILES</term> <listitem> <simpara> An associative array of variables containing information about files uploaded via the HTTP POST method. See <link linkend="features.file-upload.post-method">POST method uploads</link> for information on the contents of <varname>$HTTP_POST_FILES</varname>. </simpara> <para> <varname>$HTTP_POST_FILES</varname> is available only in PHP 4.0.0 and later. </para> </listitem> </varlistentry> <varlistentry> <term>HTTP_ENV_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script via the parent environment. </simpara> </listitem> </varlistentry> <varlistentry> <term>HTTP_SERVER_VARS</term> <listitem> <simpara> An associative array of variables passed to the current script from the HTTP server. These variables are analogous to the Apache variables described above. </simpara> </listitem> </varlistentry> </variablelist> </para> </sect2> </sect1> <sect1 id="language.variables.scope"> <title>Åܼƪº¦³®Ä½d³ò</title> <simpara> Åܼƪº¦³®Ä½d³ò¬O«ü¥u¦³¦b¸Ó½d³ò¤§¤º¹q¸£¤~ª¾¹D¦³³o¼Ë¤@ÓÅܼƦs¦b¨Ã¥i¦s¨ú¨ä©ÒÀxªºÈ¡C ¤j«eÃD¬O©Ò¦³ªº PHP ÅܼƳ£¥u¦³³æ¤@ªº¦³®Ä½d³ò¡C ³o¦³®Ä½d³ò¦ù®i¨ì³Q include() ©M require() ªº¤å¥ó¤¤¡C ½Ð¬Ý¥Ü½d: </simpara> <informalexample> <programlisting role="php"> $a = 1; include "b.inc"; </programlisting> </informalexample> <simpara> ¦b b.inc ¤å¥ó¤¤ $a ÅܼƪºÈ¤]¬O 1 ¡C µM¦Ó¡A ¦b¨ç¼Æ¤¤ªºÅܼƨ䦳®Ä½d³ò«h§½¦b¨ç¼Æªº¥»Å餧¤¤¡C ¸õ¶i¨ç¼Æ®É¹q¸£¤£·|ª¾¹D¦b¨ç¼Æ³Q½Õ¥Î¤§«e¦³¤°»òÅܼƴ¿³Q©w¸q¡A ¦Ó©Ò¦³¦b¨ç¼Æ°õ¦æ®É¤~©w¸qªºÅܼƦb¸õ¥X¨ç¼Æ«á³£·|®ø¥¢¡C ¦p¤U¨Ò: </simpara> <informalexample> <programlisting role="php"> $a = 1; /* global scope */ Function Test () { echo $a; /* reference to local scope variable */ } Test (); </programlisting> </informalexample> <simpara> ¸Óµ{¦¡¨Ã¤£·|¥´¥X¥ô¦óȦ]¬°¦b echo $a ³o¤@«ü¥O¤¤¡A $a ªº¦³®Ä½d³ò¥u¬O¦b¨ç¼Æ¤§¤¤¡A¦ý¦b¨ç¼Æ·í¤¤¨Ã¨S¦³©w¸q $a ªºÈ¡F ¥t¤@¤è±¦b¨ç¼Æ¥~ªº $a ªº¦³®Ä½d³ò¨Ã¤£·|¦ù®i¨ì¨ç¼ÆùØ¥h¡C ¦p§A¬O C µ{¦¡ûªº¸Ü½Ð¯d·N³oºØ¦³®Ä½d³òªº©wªk©M C ¤£¤@¼Ë¡G ¦b C ·í¤¤©Ò¦³¥Dµ{¦¡¤¤ªºÅܼƳ£¬O¥þ°ìÅܼơC (°£«D¦b¨ç¼Æ¤¤¥t¥~«ü©w¡C) ¦]¬° C ªº¤è¦¡·|¤£¤p¤ß¦b¨ç¼Æ¤¤§ï°Ê¤F¥~³¡ÅܼơA ©Ò¥H¦b PHP ¤§¤¤¦³¦p·Q¦s¨ú¥~³¡Åܼƪº¸Ü´N¥²¶·«Å§i¤@Ó¥þ°ìÅܼơC ¥Ü½d¦p¤U: </simpara> <informalexample> <programlisting role="php"> $a = 1; $b = 2; Function Sum () { global $a, $b; $b = $a + $b; } Sum (); echo $b; </programlisting> </informalexample> <simpara> ¤W±ªº¨Ò¤l¤~·|¦L¥X "3"¡C ¦b¨ç¼Æ¤¤«Å§i $a¡B$b ¬°¥þ°ìÅܼƤ§«á´N¥i¥H¦s¨ú¦b¨ç¼Æ¥~±ªºÅܼơC ¤@Ó¨ç¼Æ¥i¥H«Å§i¥ô·N¦hÓ¥þ°ìÅܼơC </simpara> <simpara> ¥t¥~¤@Ó¦s¨ú¥~³¡Åܼƪº¤èªk¬O½Õ¥Î PHP ¤º¸mªº $GLOBALS[] °}¦C¡C ²{¦b§â«e±ªº¨Ò¤l§ï¼g¤@¤U: </simpara> <informalexample> <programlisting role="php"> $a = 1; $b = 2; Function Sum () { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } Sum (); echo $b; </programlisting> </informalexample> <simpara> $GLOBALS °}¦C¬O¤@ÓÃöÁp¦r¯Á¤Þ¦¡°}¦C¡A Åܼƪº¦W¦r·|§@¬°¦b¦s¨ú¸Ó°}¦Cªº¯Á¤Þ¡C </simpara> <simpara> ¥t¤@Ó«nªºÅܼƦ³®Ä½d³òªº·§©À¬O³Q«Å§i¦¨ static Ãþ«¬(ÀRºA)ªºÅܼơC ÀRºAÅܼƥu¦b¨ç¼Æ¤§¤¤¥X²{¡A ¤£¹L¦b¸õ¥X¨ç¼Æ¤§«á¥¦ÌªºÈ¨Ã¤£·|®ø¥¢¡C ¦Ò¼{¤U±ªº±¡ªp: </simpara> <informalexample> <programlisting role="php"> Function Test () { $a = 0; echo $a; $a++; } </programlisting> </informalexample> <simpara> ¤W±ªº¨ç¼Æ®Ú¥»°_¤£¤F§@¥Î¦]¬°¨C¦¸¶i¤J¨ç¼Æ«á $a ³£·|³Q«·s«ü©w¬° "0"¡E $a++ ³o¤@¥y«ü¥O¤@ÂI¥Î¤]¨S¦³¦]¬°¨C¦¸¸õ¥X¨ç¼Æ®É $a ´N®ø¥¢¤F¡C ¬°¤F¥O¨ç¼Æ¦¨¬°¦³¥Îªºp¼Æ¾¹¡A ¥²¶·§â $a «Å§i¬° static: </simpara> <informalexample> <programlisting role="php"> Function Test () { static $a = 0; echo $a; $a++; } </programlisting> </informalexample> <simpara> ²{¦b test() ¨ç¼Æ·|¦L¥X $a ªºÈ¨Ã§âÈ¥¦¥[ 1¡C </simpara> <simpara> ÀRºAÅܼƤ]¥i¥H¥Î¨Ó½s¼g»¼Âk¨ç¼Æ recursive function¡C ³oÃþ¨ç¼Æ·|½Õ¥Î¦Û¤v¡C ¼g³oÃþ¨ç¼Æ®ÉnÁ×§KµL´`Àô¦a½Õ¥Î¡C §A¤@©wn¦³¿ìªk§â»¼Âk²×¤î¡C ¤U¨Ò¥Î $count ÅܼƨÓp¼Æ¡A ·í¥¦¼Æ¨ì 10 ªº®ÉÔ´N¸õ¥X¨ç¼Æ: </simpara> <informalexample> <programlisting role="php"> Function Test () { static $count = 0; $count++; echo $count; if ($count < 10) { Test (); } $count--; } </programlisting> </informalexample> </sect1> <sect1 id="language.variables.variable"> <title>¥ÎÅܼƨӽեÎÅܼÆ</title> <simpara> ¦³ªº±¡ÒX¤U¡A ¥Î¤@ÓÅܼƨÓÀx¦s¥t¤@ÓÅܼƪº¦W¦r·|¥O½sµ{§ó¤è«K¡C ¦]¬°³o¼Ë¤@¨Ó¥i¥H°ÊºA¦a½Õ¥ÎÅܼơC ¤@Ó¥¿±`ªºÅܼƫŧi¬O³o¼Ëªº: </simpara> <informalexample> <programlisting role="php"> $a = "hello"; </programlisting> </informalexample> <simpara> ¦Ó¤@ÓÅܼƪºÅܼƫh§â¦s¦b¨ä¤¤ªºÈ·í°µ¬O¥t¤@ÓÅܼƪº¦W¦r¡C ¦b¤W¨Òªº $a ¤§«e¥Î¨â '$' ¸¹ªº¸Ü¡A hello´N·|³Q·í¦¨¬O¥t¤@ÓÅܼƪº¦W¦r¡G </simpara> <informalexample> <programlisting role="php"> $$a = "world"; </programlisting> </informalexample> <simpara> ¨ì³oùج°¤î¡A ¤@¦@«Å§i¤F¨âÓÅܼơA ¤@Ó¬O $a¡A ¥t¤@Ó¬O $hello¡C ¬O¥H¤U±ªº«ü¥O¡G </simpara> <informalexample> <programlisting role="php"> echo "$a ${$a}"; </programlisting> </informalexample> <simpara> ©Ò¦L¥X¨Óªºµ²ªG©M³o¤@¥y </simpara> <informalexample> <programlisting> echo "$a $hello"; </programlisting> </informalexample> <simpara> ªºµ²ªG¬O¤@¼Ëªº¡G hello world. </simpara> <simpara> ¦b¥ÎÅܼƪºÅܼƳoªk¤l¨Ó¦s¨ú°}¦Cªº¸Ü¡A n¯d·N $$a[1] ¦³¨âÓ·N«ä¡G ¥i¥H¬O«ü $a[1] ¬O¤@ÓÅܼƪº¦WºÙ¡B¤]¥i¥H«ü $$a ¬O¤@ÓÅܼƦWºÙ¦Ó [1] ¬O¸ÓÅܼƤ¤¤¸¯Àªº¯Á¤Þ ¸¹¡C ¬°¤F©ú½T§i¶D¹q¸£§Aªº·N¹Ï¡A ¥Î ${$a[1]} ¥Nªí²Ä¤@Ó·N«ä¡B ${$a}[1] ¥Nªí²Ä¤GÓ¡C </simpara> </sect1> <sect1 id="language.variables.external"> <title>¥~³¡ÅܼÆ</title> <sect2 id="language.variables.external.form"> <title>HTML ªí (GET and POST) </title> <simpara> ·íªí³æ³Q´£¥æµ¹ PHP µ{¦¡®É¡A ©Ò¦³¦bªí³æ¤¤ªºÅܼƳ£¦Û°Êµ¹¤À¹j¶}¨Ã¶Çµ¹µ{¦¡¡C °²¦p¦³¤@±i¦p¤U¨Òªºªí³æ: If the <link linkend="ini.track-vars">track_vars</link> configuration option is turned on, then these variables will be located in the associative arrays <varname>$HTTP_POST_VARS</varname>, <varname>$HTTP_GET_VARS</varname>, and/or <varname>$HTTP_POST_FILES</varname>, according to the source of the variable in question. </simpara> <para> For more information on these variables, please read <link linkend="language.variables.predefined">Predefined variables</link>. </para> <para> <example> <title>²³æªºªí³æÅܼÆ</title> <programlisting role="php"> <form action="foo.php" method="post"> Name: <input type="text" name="username"><br> <input type="submit"> </form> </programlisting> </example> </para> <para> ·í«ö¤U SUBMIT «á¡A PHP ·|¦Û°Ê«ü©wÅÜ¼Æ $name ¡A ¨ä¤¤·|Àx¦sªí³æ¤¤ Name: ³o¤@Äæ©Ò§tªºÈ¡C PHP ¦³Ó¥s track_vars ªº¥i½Õ¶µ¡A ¦b¶}ªºª¬ºA¤U¥Î POST ©Î GET ¤èªk´£¥æªºªí³æÄæ¦ì¥i¥H¦b¬ÛÀ³ªº $HTTP_POST_VARS ©Î $HTTP_GET_VARS °}¦C¤¤§ä¨ì¡C n¥´¶}³oÓ¥\¯à¥i¥H§ï track_vars ³oӰѼƩΨϥΠ<?php_track_vars?> «ü¥O¡C </para> <simpara> PHP ÁÙ¥i¥H¤À¸Ñªí³æ¤¤°}¦C«¬ºAªºÅܼơC §A¥i§Q¥Î³o¤@¯S©Ê§â¬ÛÃöªºÅܼƤÀ²Õ©ÎªÌ¬OÀx¦s®e³\¦h¶µ¿ï¾Üªºªí³æÄæ¦ì: </simpara> <para> <example> <title>½ÆÂø¤@¨Çªºªí³æ </title> <programlisting role="php"> <form action="array.php" method="post"> Name: <input type="text" name="personal[name]"><br> Email: <input type="text" name="personal[email]"><br> Beer: <br> <select multiple name="beer[]"> <option value="warthog">Warthog <option value="guinness">Guinness <option value="stuttgarter">Stuttgarter Schwabenbräu </select> <input type="submit"> </form> </programlisting> </example> </para> <para> 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> <simpara> ´£¥æªí³æ®É¡A ¥i¥H¤£¥Î¼Ð·Çªº«ö¶s¦Ó¥Î¹Ï®×¥N´À¡C ¥yªk¦p¤U:</simpara> <informalexample> <programlisting role="php"> <input type=image src="image.gif" name="sub"> </programlisting> </informalexample> <simpara> ·í¨Ï¥ÎªÌ«ö¦b¸Ó¹Ï®×¤W®É¡A ¶Ç¥hµ¹¦øªA¾¹ªºªí³æ¸ê®Æ·|¦³¨âÓ·sªºÈ sub_x ©M sub_y¡A ¤À§O¬O¨Ï¥ÎªÌ«ö¦b¹Ï®×¤Wªº®y¼Ð¡C ªí³æ¦Ñ¤âÌ¥i¯à·|ª¾¹D¨ä¹ê°eµ¹¦øªA¾¹ªºªºÄæ¦ì¬O sub.x ©M sub.y¡A ¤£¹L PHP ¦Û°Ê§â¥y¸¹Åܦ¨¤F©³½u¸¹¡C </simpara> </sect3> </sect2> <sect2 id="language.variables.external.cookies"> <title>HTTP cookies (²¢»æ ?) </title> <simpara> PHP ¤ä«ù¥Ñ <ulink url="&spec.cookies;">Netscape's Spec</ulink>©Ò©w¸qªº HTTP cookies¡C Cookies ¬O§â¸ê®Æ¦s¨ì«È¤á¾÷¤Wªº¤@ºØ¤èªk¡C ³o¨Ç¸ê®Æ¥i¥H¥Î¨Ó¿ë§O¥Î¤áªº¨¥÷¡C n³]©w COOKIES ¥i¥Î SetCookie() ¨ç¼Æ¡C ¦]¬° COOKIES ¬O HTTP HEADER ¼ÐÀYªº¤@³¡¥÷©Ò¥H°e¥X COOKIE ªº©R¥O¥²¶·§â¶±¤º®e¿é¥Xµ¹ÂsÄý¾¹¤§«e°õ¦æ¡C ³o¨î©M¨Ï¥Î Header() ¨ç¼Æªº¨î¬O¤@¼Ëªº¡C ¦Ü©ó±qÂsÄý¾¹°e¦^¨Óªº COOKIES «h¹³ GET¡B POST ¤èªk¤@¼Ë·|¦Û°Ê¦s¦bÅܼÆùØ¡C </simpara> <simpara> ¦p·Q¥Î¤@Ó COOKIE ¦s¦h¹L¤@ÓÈ¡A ¥u»Ý¦b COOKIE ¦W¦r«á¥[¤W []¡C ¥Ü½d: </simpara> <informalexample> <programlisting role="php"> SetCookie ("MyCookie[]", "Testing", time()+3600); </programlisting> </informalexample> <simpara> ª`·N°£«D¸ô®|©Îºô°ì¦W¦r¤£¤@¼Ë¡A ¤£µM§A°e¥Xªº COOKIE ·|¨ú¥N¤§«e¦P¦Wªº COOKIE¡C ¦]¦¹¦b½s¼gÁʪ«Äxµ{¦¡®É¦³®Én¥[¤@Óp¼Æ¾¹¡C ´N¹³³o¼Ë¡G </simpara> <example> <title>SetCookie ¥Ü½d</title> <programlisting role="php"> $Count++; SetCookie ("Count", $Count, time()+3600); SetCookie ("Cart[$Count]", $item, time()+3600); </programlisting> </example> </sect2> <sect2 id="language.variables.external.environment"> <title>§@·~Àô¹ÒÅܼÆ</title> <para> PHP ·|¦Û°Ê§âÃö©ó§@·~Àô¹Òªº¸ê®Æ¾ã²z¦¨¤@¯ëªºÅܼƨѵ{¦¡¨ú¥Î¡C <informalexample> <programlisting role="php"> echo $HOME; /* Shows the HOME environment variable, if set. */ </programlisting> </informalexample> </para> <para> ¤p¤ß°_¨£¡A Ū¨úÀô¹ÒÅܼƮɳ̦n¥Î <function>getenv</function>¨ç¼Æ¡A ³o¬O¦]¬° PHP ¤@¼Ë¬°ªí³æ¡BCOOKIE µ¥µ¥¦Û°Ê©w¸qÅܼơA ¦³®ÉÔ·|¤£¤p¤ß¥Î¤F¬Û¦Pªº¦W¦r¡A ¨ì®É§AŪ¨úªº¥i¯à¨Ã¤£¬O¯u¥¿ªºÀô¹ÒÅܼơC §ï°ÊÀô¹ÒÅܼƫh¥i¥Î <function>putenv</function>¨ç¼Æ¡C </para> </sect2> <sect2 id="language.variables.external.dot-in-names"> <title>±a '.' ¸¹ªºÅܼÆ</title> <para> °ò¥»¤W PHP ¤£·|¾Õ§ïÅܼƪº¦W¦r¡A ¦ý¥Ñ©ó PHP ¤§¤¤ '.' ¸¹¦³¯S§Oªº·N«ä(¦ê±µ¤å¥y)¤£¥i¥H§@¬°ÅܼƦW¦r¡A ©Ò¥H±a '.' ¸¹ªºÅܼƦWºÙ¤¤ªº '.' ·|Åܦ¨ '_': <programlisting role="php"> $varname.ext; /* invalid variable name */ </programlisting> ¤W±½d¨Ò¤¤ PHP ·|²z¸Ñ¬°¤@ÓÅÜ¼Æ $varname¡A «á±¸òµÛ¦A¦ê±µ¥t¤@ÓÅÜ¼Æ $ext¡C ÅãµM PHP ²z¸Ñ¿ù¤F¥»¨Óªº¥Î·N¡C </para> </sect2> <sect2 id="language.variables.determining-type-of"> <title>¬d¸ßÅܼƪº§ÎºA </title> <para> ¥Ñ©ó PHP ·|¨Ì¤º®e¦Û°Ê³]©wÅܼƪº§ÎºA¡A ¦³®ÉÔ«ÜÃø¤@²´¬Ý¥X¤@ÓÅܼƨ쩳¨ú¥Î¤F¨º¤@ºØ§ÎºA¡C ¬°¤FÅý¥ÎªÌ¨ú±o³oÃþ¸ê°T¡A PHP ¦³¦n¦hÓ¨ç¼Æ¥i¥H½Õ¥Î¡G <function>gettype</function>, <function>is_long</function>, <function>is_double</function>, <function>is_string</function>, <function>is_array</function>,©M <function>is_object</function>. </para> </sect2> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: -->