cvs: phpdoc-zh /language constants.xml control-structures.xml operators.xml types.xml

From: Date: Sat, 22 Feb 2003 12:28:15 +0000
Subject: cvs: phpdoc-zh /language constants.xml control-structures.xml operators.xml types.xml
Groups: php.doc.zh 
Request: Send a blank email to doc-zh+get-1071@lists.php.net to get a copy of this message
dallas Sat Feb 22 07:28:15 2003 EDT Modified files: /phpdoc-zh/language constants.xml control-structures.xml operators.xml types.xml Log: Update old files.

Index: phpdoc-zh/language/constants.xml diff -u phpdoc-zh/language/constants.xml:1.3 phpdoc-zh/language/constants.xml:1.4 --- phpdoc-zh/language/constants.xml:1.3 Sun Oct 13 01:25:15 2002 +++ phpdoc-zh/language/constants.xml Sat Feb 22 07:28:14 2003 @@ -1,12 +1,13 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.3 $ --> -<!-- $Author: gregory $ --> -<!-- EN-Revision: 1.26 Maintainer: dallas Status: ready --> +<!-- $Revision: 1.4 $ --> +<!-- $Author: dallas $ --> +<!-- EN-Revision: 1.27 Maintainer: dallas Status: ready --> <chapter id="language.constants"> <title>³£Á¿</title> <simpara> - ³£Á¿ÊÇÒ»¸ö¼òµ¥ÖµµÄ±êʶ·û£¨Ãû×Ö£©¡£ÈçͬÆäÃû³ÆËù°µÊ¾µÄ£¬Ôڽű¾Ö´ÐÐÆÚ¼ä¸ÃÖµ²»Äܸı䡣£¨ËùνµÄ¡°magic constants¡± <constant>__FILE__</constant> ºÍ <constant>__LINE__</constant> ¿´ÉÏÈ¥ÊǸöÀýÍ⣬µ«ÊÇËüÃÇʵ¼ÊÉϲ»Êdz£Á¿¡££©³£Á¿Ä¬ÈÏΪ´óСдÃô¸Ð¡£°´ÕÕ¹ßÀý³£Á¿±êʶ·û×ÜÊÇ´óдµÄ¡£ + ³£Á¿ÊÇÒ»¸ö¼òµ¥ÖµµÄ±êʶ·û£¨Ãû×Ö£©¡£ÈçͬÆäÃû³ÆËù°µÊ¾µÄ£¬Ôڽű¾Ö´ÐÐÆÚ¼ä¸ÃÖµ²»Äܸı䣨³ýÁËËùνµÄ<link + linkend="language.constants.predefined">ħÊõ³£Á¿</link>£¬ËüÃÇÆäʵ²»Êdz£Á¿£©¡£³£Á¿Ä¬ÈÏΪ´óСдÃô¸Ð¡£°´ÕÕ¹ßÀý³£Á¿±êʶ·û×ÜÊÇ´óдµÄ¡£ </simpara> <para> ³£Á¿ÃûºÍÆäËüÈκΠPHP ±êÇ©×ñѭͬÑùµÄÃüÃû¹æÔò¡£ºÏ·¨µÄ³£Á¿ÃûÒÔ×Öĸ»òÏ»®Ïß¿ªÊ¼£¬ºóÃæ¸ú×ÅÈκÎ×Öĸ£¬Êý×Ö»òÏ»®Ïß¡£ÓÃÕýÔò±í´ïʽÊÇÕâÑù±í´ïµÄ£º<literal>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</literal> @@ -97,11 +98,52 @@ <simpara> PHP ÏòËüÔËÐеÄÈκνű¾ÌṩÁË´óÁ¿µÄÔ¤¶¨Òå³£Á¿¡£²»¹ýºÜ¶à³£Á¿¶¼ÊÇÓɲ»Í¬µÄÀ©Õ¹¿â¶¨ÒåµÄ£¬Ö»ÓÐÔÚ¼ÓÔØÁËÕâЩÀ©Õ¹¿âʱ²Å»á³öÏÖ£¬»òÕß¶¯Ì¬¼ÓÔØºó£¬»òÕßÔÚ±àÒëʱÒѾ­°üÀ¨½øÈ¥ÁË¡£ </simpara> - + <para> + ÓÐËĸöħÊõ³£Á¿¸ù¾ÝËüÃÇʹÓõÄλÖöø¸Ä±ä¡£ÀýÈç + <constant>__LINE__</constant> µÄÖµ¾ÍÒÀÀµÓÚËüÔڽű¾ÖÐËù´¦µÄÐÐÀ´¾ö¶¨¡£ÕâÐ©ÌØÊâµÄ³£Á¿²»Çø·Ö´óСд£¬ÈçÏ£º + </para> + <para> + <table> + <title>¼¸¸ö PHP µÄ¡°Ä§Êõ³£Á¿¡±</title> + <tgroup cols="2"> + <thead> + <row> + <entry>Ãû³Æ</entry> + <entry>˵Ã÷</entry> + </row> + </thead> + <tbody> + <row> + <entry><constant>__LINE__</constant></entry> + <entry> + ÎļþÖеĵ±Ç°Ðкš£ + </entry> + </row> + <row> + <entry><constant>__FILE__</constant></entry> + <entry> + ÎļþµÄÍê³É·¾¶ºÍÎļþÃû¡£ + </entry> + </row> + <row> + <entry><constant>__FUNCTION__</constant></entry> + <entry> + º¯ÊýÃû³Æ¡£ÕâÊÇ PHP 4.3.0 мӵġ£ + </entry> + </row> + <row> + <entry><constant>__CLASS__</constant></entry> + <entry> + ÀàµÄÃû³Æ¡£ÕâÊÇ PHP 4.3.0 мӵġ£ + </entry> + </row> + </tbody> + </tgroup> + </table> + </para> <simpara> - Ô¤¶¨Òå³£Á¿µÄÁбí¼û<link linkend="reserved.constants">Ô¤¶¨Òå³£Á¿</link>Ò»½Ú¡£ + Ô¤¶¨Òå³£Á¿µÄÁбí¼û<link linkend="reserved.constants">±£ÁôµÄÔ¤¶¨Òå³£Á¿</link>Ò»½Ú¡£ </simpara> - </sect1> </chapter> Index: phpdoc-zh/language/control-structures.xml diff -u phpdoc-zh/language/control-structures.xml:1.11 phpdoc-zh/language/control-structures.xml:1.12 --- phpdoc-zh/language/control-structures.xml:1.11 Sun Dec 29 11:45:02 2002 +++ phpdoc-zh/language/control-structures.xml Sat Feb 22 07:28:15 2003 @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.11 $ --> +<!-- $Revision: 1.12 $ --> <!-- $Author: dallas $ --> -<!-- EN-Revision: 1.66 Maintainer: dallas Status: ready --> +<!-- EN-Revision: 1.67 Maintainer: dallas Status: ready --> <chapter id="control-structures"> <title>Á÷³Ì¿ØÖÆ</title> @@ -752,8 +752,13 @@ Èç¹ûÔÚÒ»¸öº¯ÊýÖе÷Óã¬<function>return</function> Óï¾äÁ¢¼´½áÊø´Ëº¯ÊýµÄÖ´Ðв¢½«ËüµÄ²ÎÊý×÷Ϊº¯ÊýµÄÖµ·µ»Ø¡£<function>return</function> Ò²»áÖÕÖ¹ <function>eval</function> Óï¾ä»òÕ߽ű¾ÎļþµÄÖ´ÐС£ </simpara> <simpara> - Èç¹ûÔÚÈ«¾Ö·¶Î§Öе÷Óã¬Ôòµ±Ç°½Å±¾ÎļþÖÐÖ¹ÔËÐС£Èç¹ûµ±Ç°½Å±¾ÎļþÊDZ» <function>include</function> µÄ»òÕß <function>require</function> µÄ£¬Ôò¿ØÖƽ»»Øµ÷ÓÃÎļþ¡£´ËÍ⣬Èç¹ûµ±Ç°½Å±¾ÊDZ» <function>include</function> µÄ£¬Ôò <function>return</function> µÄÖµ»á±»µ±×÷ <function>include</function> µ÷Óõķµ»ØÖµ¡£Èç¹ûÔÚÖ÷½Å±¾ÎļþÖе÷Óà <function>return</function>£¬Ôò½Å±¾ÖÐÖ¹ÔËÐС£Èç¹ûµ±Ç°½Å±¾ÎļþÊÇÔÚ<link - linkend="configuration.file">ÅäÖÃÎļþ</link>ÖÐµÄ <link + Èç¹ûÔÚÈ«¾Ö·¶Î§Öе÷Óã¬Ôòµ±Ç°½Å±¾ÎļþÖÐÖ¹ÔËÐС£Èç¹ûµ±Ç°½Å±¾ÎļþÊDZ» + <function>include</function> µÄ»òÕß <function>require</function> + µÄ£¬Ôò¿ØÖƽ»»Øµ÷ÓÃÎļþ¡£´ËÍ⣬Èç¹ûµ±Ç°½Å±¾ÊDZ» <function>include</function> + µÄ£¬Ôò <function>return</function> µÄÖµ»á±»µ±×÷ <function>include</function> + µ÷Óõķµ»ØÖµ¡£Èç¹ûÔÚÖ÷½Å±¾ÎļþÖе÷Óà + <function>return</function>£¬Ôò½Å±¾ÖÐÖ¹ÔËÐС£Èç¹ûµ±Ç°½Å±¾ÎļþÊÇÔÚ &php.ini; + ÖеÄÅäÖÃÑ¡Ïî <link linkend="ini.auto-prepend-file">auto_prepend_file</link> »òÕß <link linkend="ini.auto-append-file">auto_append_file</link> ËùÖ¸¶¨µÄ£¬Ôò´Ë½Å±¾ÎļþÖÐÖ¹ÔËÐС£ </simpara> Index: phpdoc-zh/language/operators.xml diff -u phpdoc-zh/language/operators.xml:1.4 phpdoc-zh/language/operators.xml:1.5 --- phpdoc-zh/language/operators.xml:1.4 Sun Oct 13 01:25:15 2002 +++ phpdoc-zh/language/operators.xml Sat Feb 22 07:28:15 2003 @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.4 $ --> -<!-- $Author: gregory $ --> -<!-- EN-Revision: 1.40 Maintainer: dallas Status: ready --> +<!-- $Revision: 1.5 $ --> +<!-- $Author: dallas $ --> +<!-- EN-Revision: 1.44 Maintainer: dallas Status: ready --> <chapter id="language.operators"> <title>ÔËËã·û</title> <simpara> @@ -111,6 +111,13 @@ </tgroup> </table> </para> + <note> + <para> + ¾¡¹Ü <literal>!</literal> ±È <literal>=</literal> µÄÓÅÏȼ¶¸ß£¬PHP + ÈÔ¾ÉÔÊÐíÀàËÆÈçϵıí´ïʽ£º<literal>if (!$a = foo())</literal>£¬ÔÚ´ËÀýÖÐ + <literal>foo()</literal> µÄÊä³ö±»¸³¸øÁË <varname>$a</varname>¡£ + </para> + </note> </sect1> <sect1 id="language.operators.arithmetic"> @@ -415,7 +422,7 @@ </para> <note> <para> - ·´ÒýºÅÔËËã·ûÔÚ¼¤»îÁË <link linkend="ini.safe-mode">safe mode</link> »òÕ߹رÕÁË <function>shell_exec</function> ʱÊÇÎÞЧµÄ¡£ + ·´ÒýºÅÔËËã·ûÔÚ¼¤»îÁË &safe mode; »òÕ߹رÕÁË <function>shell_exec</function> ʱÊÇÎÞЧµÄ¡£ </para> </note> <para> Index: phpdoc-zh/language/types.xml diff -u phpdoc-zh/language/types.xml:1.7 phpdoc-zh/language/types.xml:1.8 --- phpdoc-zh/language/types.xml:1.7 Sun Jan 12 05:51:49 2003 +++ phpdoc-zh/language/types.xml Sat Feb 22 07:28:15 2003 @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.7 $ --> +<!-- $Revision: 1.8 $ --> <!-- $Author: dallas $ --> -<!-- EN-Revision: 1.99 Maintainer: dallas Status: ready --> +<!-- EN-Revision: 1.108 Maintainer: dallas Status: ready --> <!-- CREDITS: Gregory --> <chapter id="language.types"> <title>ÀàÐÍ</title> @@ -118,6 +118,7 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $bool = TRUE; // a boolean $str = "foo"; // a string $int = 12; // an integer @@ -135,6 +136,7 @@ if (is_string($bool)) { echo "String: $bool"; } +?> ]]> </programlisting> </informalexample> @@ -169,7 +171,9 @@ <informalexample> <programlisting role="php"> <![CDATA[ -$foo = True; // assign the value TRUE to $foo +<?php +$foo = True; // assign the value TRUE to $foo +?> ]]> </programlisting> </informalexample> @@ -247,6 +251,7 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php echo gettype((bool) ""); // bool(false) echo gettype((bool) 1); // bool(true) echo gettype((bool) -2); // bool(true) @@ -254,6 +259,7 @@ echo gettype((bool) 2.3e5); // bool(true) echo gettype((bool) array(12)); // bool(true) echo gettype((bool) array()); // bool(false) +?> ]]> </programlisting> </informalexample> @@ -285,10 +291,12 @@ <title>ÕûÊýÎÄ×Ö±í´ï</title> <programlisting role="php"> <![CDATA[ +<?php $a = 1234; # Ê®½øÖÆÊý $a = -123; # Ò»¸ö¸ºÊý $a = 0123; # °Ë½øÖÆÊý£¨µÈÓÚÊ®½øÖÆµÄ 83£© $a = 0x1A; # Ê®Áù½øÖÆÊý£¨µÈÓÚÊ®½øÖÆµÄ 26£© +?> ]]> </programlisting> </example> @@ -296,6 +304,7 @@ <informalexample> <programlisting> <![CDATA[ +<?php decimal : [1-9][0-9]* | 0 @@ -306,6 +315,7 @@ integer : [+-]?decimal | [+-]?hexadecimal | [+-]?octal +?> ]]> </programlisting> </informalexample> @@ -320,6 +330,7 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $large_number = 2147483647; var_dump($large_number); // Êä³öΪ£ºint(2147483647) @@ -336,6 +347,7 @@ $large_number = 50000 * $million; var_dump($large_number); // Êä³öΪ£ºfloat(50000000000) +?> ]]> </programlisting> </informalexample> @@ -354,9 +366,11 @@ <informalexample> <programlisting role="php"> <![CDATA[ -var_dump(25/7); // float(3.5714285714286) +<?php +var_dump(25/7); // float(3.5714285714286) var_dump((int) (25/7)); // int(3) -var_dump(round(25/7)); // float(4) +var_dump(round(25/7)); // float(4) +?> ]]> </programlisting> </informalexample> @@ -399,7 +413,9 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php echo (int) ( (0.1+0.7) * 10 ); // ÏÔʾ 7£¡ +?> ]]> </programlisting> </informalexample> @@ -434,7 +450,13 @@ ¸¡µãÊý£¨Ò²½Ð¡°floats¡±£¬¡°doubles¡±»ò¡°real numbers¡±£©¿ÉÒÔÓÃÒÔÏÂÈκÎÓï·¨¶¨Ò壺 <informalexample> <programlisting role="php"> -$a = 1.234; $a = 1.2e3; $a = 7E-10; +<![CDATA[ +<?php +$a = 1.234; +$a = 1.2e3; +$a = 7E-10; +?> +]]> </programlisting> </informalexample> ÐÎʽÉÏ£º @@ -474,7 +496,7 @@ <sect1 id="language.types.string"> <title>×Ö·û´®</title> <para> - <type>string</type> ÊÇһϵÁÐ×Ö·û¡£ÔÚ PHP ÖУ¬×Ö·ûºÍ×Ö½ÚÒ»Ñù£¬Ò²¾ÍÊÇ˵£¬Ò»¹²ÓÐ 256 ÖÖ²»Í¬×Ö·ûµÄ¿ÉÄÜÐÔ¡£ÕâÒ²°µÊ¾ PHP ¶Ô Unicode ûÓб¾µØÖ§³Ö¡£Çë²ÎÔĺ¯Êý <function>utf8_enncode</function> + <type>string</type> ÊÇһϵÁÐ×Ö·û¡£ÔÚ PHP ÖУ¬×Ö·ûºÍ×Ö½ÚÒ»Ñù£¬Ò²¾ÍÊÇ˵£¬Ò»¹²ÓÐ 256 ÖÖ²»Í¬×Ö·ûµÄ¿ÉÄÜÐÔ¡£ÕâÒ²°µÊ¾ PHP ¶Ô Unicode ûÓб¾µØÖ§³Ö¡£Çë²ÎÔĺ¯Êý <function>utf8_encode</function> ºÍ <function>utf8_decode</function> ÒÔÁ˽âÓÐ¹Ø Unicode Ö§³Ö¡£ </para> <note> @@ -520,23 +542,35 @@ </note> <note> <simpara> - ºÍÆäËûÁ½ÖÖÓï·¨²»Í¬£¬µ¥ÒýºÅ×Ö·û´®ÖгöÏֵıäÁ¿<emphasis>²»»á</emphasis>±»±äÁ¿µÄÖµÌæ´ú¡£ + ºÍÆäËûÁ½ÖÖÓï·¨²»Í¬£¬µ¥ÒýºÅ×Ö·û´®ÖгöÏÖµÄ<link + linkend="language.variables">±äÁ¿</link>ºÍתÒåÐòÁÐ<emphasis>²»»á</emphasis>±»±äÁ¿µÄÖµÌæ´ú¡£ </simpara> </note> <informalexample> <programlisting role="php"> <![CDATA[ +<?php echo 'this is a simple string'; -echo 'You can also have embedded newlines in strings -this way'; + +echo 'You can also have embedded newlines in +strings this way as it is +okay to do'; + +// Outputs: "I'll be back" echo 'Arnold once said: "I\'ll be back"'; -// output: ... "I'll be back" -echo 'Are you sure you want to delete C:\\*.*?'; -// output: ... delete C:\*.*? -echo 'Are you sure you want to delete C:\*.*?'; -// output: ... delete C:\*.*? -echo 'I am trying to include at this point: \n a newline'; -// output: ... this point: \n a newline + +// Outputs: You deleted C:\*.*? +echo 'You deleted C:\\*.*?'; + +// Outputs: You deleted C:\*.*? +echo 'You deleted C:\*.*?'; + +// Outputs: This will not expand: \n a newline +echo 'This will not expand: \n a newline'; + +// Outputs: Variables do not $expand $either +echo 'Variables do not $expand $either'; +?> ]]> </programlisting> </informalexample> @@ -615,7 +649,12 @@ <warning> <simpara> - ºÜÖØÒªµÄÒ»µã±ØÐëÖ¸³ö£¬½áÊø±êʶ·ûËùÔÚµÄÐв»Äܰüº¬ÈÎºÎÆäËü×Ö·û£¬<emphasis>¿ÉÄÜ</emphasis>³ýÁËÒ»¸ö·ÖºÅ£¨<literal>;</literal>£©Ö®Íâ¡£ÕâÓÈÆäÒâζןñêʶ·û<emphasis>²»Äܱ»Ëõ½ø</emphasis>£¬¶øÇÒÔÚ·ÖºÅ֮ǰºÍÖ®ºó¶¼²»ÄÜÓÐÈκοոñ»òÖÆ±í·û¡£ + ºÜÖØÒªµÄÒ»µã±ØÐëÖ¸³ö£¬½áÊø±êʶ·ûËùÔÚµÄÐв»Äܰüº¬ÈÎºÎÆäËü×Ö·û£¬<emphasis>¿ÉÄÜ</emphasis>³ýÁËÒ»¸ö·ÖºÅ£¨<literal>;</literal>£©Ö®Íâ¡£ÕâÓÈÆäÒâζןñêʶ·û<emphasis>²»Äܱ»Ëõ½ø</emphasis>£¬¶øÇÒÔÚ·ÖºÅ֮ǰºÍÖ®ºó¶¼²»ÄÜÓÐÈκοոñ»òÖÆ±í·û¡£Í¬ÑùÖØÒªµÄÊÇÒªÒâʶµ½ÔÚ½áÊø±êʶ·û֮ǰµÄµÚÒ»¸ö×Ö·û±ØÐëÊÇÄãµÄ²Ù×÷ϵͳÖж¨ÒåµÄ»»Ðзû¡£ÀýÈçÔÚ + Macintosh ϵͳÖÐÊÇ <literal>\r</literal>¡£ + </simpara> + <simpara> + Èç¹ûÆÆ»µÁËÕâÌõ¹æÔòʹµÃ½áÊø±êʶ·û²»¡°¸É¾»¡±£¬ÔòËü²»»á±»ÊÓΪ½áÊø±êʶ·û£¬PHP + ½«¼ÌÐøÑ°ÕÒÏÂÈ¥¡£Èç¹ûÔÚÕâÖÖÇé¿öÏÂÕÒ²»µ½ºÏÊʵĽáÊø±êʶ·û£¬½«»áµ¼ÖÂÒ»¸öÔڽű¾×îºóÒ»ÐгöÏÖµÄÓï·¨´íÎó¡£ </simpara> </warning> @@ -669,7 +708,8 @@ <sect3 id="language.types.string.parsing"> <title>±äÁ¿½âÎö</title> <simpara> - µ±ÓÃË«ÒýºÅ»òÕß¶¨½ç·ûÖ¸¶¨×Ö·û´®Ê±£¬ÆäÖеıäÁ¿»á±»½âÎö¡£ + µ±ÓÃË«ÒýºÅ»òÕß¶¨½ç·ûÖ¸¶¨×Ö·û´®Ê±£¬ÆäÖеÄ<link + linkend="language.variables">±äÁ¿</link>»á±»½âÎö¡£ </simpara> <simpara> ÓÐÁ½ÖÖÓï·¨£¬Ò»ÖÖ<link linkend="language.types.string.parsing.simple">¼òµ¥µÄ</link>ºÍÒ»ÖÖ<link @@ -687,10 +727,13 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $beer = 'Heineken'; echo "$beer's taste is great"; // works, "'" is an invalid character for varnames echo "He drank some $beers"; // won't work, 's' is a valid character for varnames echo "He drank some ${beer}s"; // works +echo "He drank some {$beer}s"; // works +?> ]]> </programlisting> </informalexample> @@ -704,16 +747,36 @@ <informalexample> <programlisting role="php"> <![CDATA[ -$fruits = array('strawberry' => 'red', 'banana' => 'yellow'); +<?php +// These examples are specific to using arrays inside of strings. +// When outside of a string, always quote your array string keys +// and do not use {braces} when outside of strings either. -// note that this works differently outside string-quotes. +// Let's show all errors +error_reporting(E_ALL); + +$fruits = array('strawberry' => 'red', 'banana' => 'yellow'); +// Works but note that this works differently outside string-quotes echo "A banana is $fruits[banana]."; +// Works +echo "A banana is {$fruits['banana']}."; -echo "This square is $square->width meters broad."; +// Works but PHP looks for a constant named banana first +// as described below. +echo "A banana is {$fruits[banana]}."; + +// Won't work, use braces. This results in a parse error. +echo "A banana is $fruits['banana']."; + +// Works +echo "A banana is " . $fruits['banana'] . "."; + +// Works +echo "This square is $square->width meters broad."; // Won't work. For a solution, see the complex syntax. echo "This square is $square->width00 centimeters broad."; - +?> ]]> <!-- XXX this won't work: echo "This square is $square->{width}00 centimeters broad."; @@ -739,19 +802,40 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php +// Let's show all errors +error_reporting(E_ALL); + $great = 'fantastic'; -echo "This is { $great}"; // ²»ÐУ¬Êä³öΪ£ºThis is { fantastic} -echo "This is {$great}"; // ¿ÉÒÔ£¬Êä³öΪ£ºThis is fantastic -echo "This square is {$square->width}00 centimeters broad."; + +// ²»ÐУ¬Êä³öΪ£ºThis is { fantastic} +echo "This is { $great}"; + +// ¿ÉÒÔ£¬Êä³öΪ£ºThis is fantastic +echo "This is {$great}"; +echo "This is ${great}"; + +// Works +echo "This square is {$square->width}00 centimeters broad."; + +// Works echo "This works: {$arr[4][3]}"; +// This is wrong for the same reason as $foo[bar] is wrong +// outside a string. In otherwords, it will still work but +// because PHP first looks for a constant named foo, it will +// throw an error of level E_NOTICE (undefined constant). +echo "This is wrong: {$arr[foo][3]}"; +// Works. When using multi-dimensional arrays, always use +// braces around arrays when inside of strings +echo "This works: {$arr['foo'][3]}"; -// This is wrong for the same reason -// as $foo[bar] is wrong outside a string. -echo "This is wrong: {$arr[foo][3]}"; +// Works. +echo "This works: " . $arr['foo'][3]; -echo "You should do it this way: {$arr['foo'][3]}"; echo "You can even write {$obj->values[3]->name}"; + echo "This is the value of the var named $name: {${$name}}"; +?> ]]> <!-- maybe it's better to leave this out?? // this works, but i disencourage its use, since this is NOT @@ -783,6 +867,8 @@ // Get the first character of a string $str = 'This is a test.'; $first = $str{0}; +// Get the third character of a string +$third = $str{2}; // Get the last character of a string. $str = 'This is still a test.'; @@ -822,34 +908,47 @@ <title>×Ö·û´®×ª»»</title> <para> - Äú¿ÉÒÔÓà <literal>(string)</literal> ±ê¼Ç»òÕß <function>strval</function> º¯Êý½«Ò»¸öֵת»»Îª×Ö·û´®¡£µ±Ä³±í´ïʽÐèÒª×Ö·û´®Ê±£¬×Ö·û´®µÄת»»»áÔÚ±í´ïʽ·¶Î§ÄÚ×Ô¶¯Íê³É¡£ÀýÈçµ±ÄãʹÓà <function>echo</function> »òÕß <function>print</function> º¯Êýʱ£¬»òÕß½«Ò»¸ö±äÁ¿ÖµÓëÒ»¸ö×Ö·û´®½øÐбȽϵÄʱºò¡£ + Äú¿ÉÒÔÓà <literal>(string)</literal> ±ê¼Ç»òÕß <function>strval</function> + º¯Êý½«Ò»¸öֵת»»Îª×Ö·û´®¡£µ±Ä³±í´ïʽÐèÒª×Ö·û´®Ê±£¬×Ö·û´®µÄת»»»áÔÚ±í´ïʽ·¶Î§ÄÚ×Ô¶¯Íê³É¡£ÀýÈçµ±ÄãʹÓà + <function>echo</function> »òÕß <function>print</function> + º¯Êýʱ£¬»òÕß½«Ò»¸ö±äÁ¿ÖµÓëÒ»¸ö×Ö·û´®½øÐбȽϵÄʱºò¡£ÔĶÁÊÖ²áÖÐÓйØ<link + linkend="language.types">ÀàÐÍ</link>ºÍ<link + linkend="language.types.type-juggling">ÀàÐÍÏ··¨</link>ÖеIJ¿·ÖÓÐÖúÓÚ¸üÇå³þһЩ¡£²Î¼û + <function>settype</function>¡£ </para> - <para> - ²¼¶ûÖµ &true; ½«±»×ª»»Îª×Ö·û´® <literal>"1"</literal>£¬¶øÖµ &false; ½«±»±íʾΪ <literal>""</literal>£¨¼´¿Õ×Ö·û´®£©¡£ÕâÑùÄú¾Í¿ÉÒÔËæÒâµØÔÚ²¼¶ûÖµºÍ×Ö·û´®Ö®¼ä½øÐбȽϡ£ + ²¼¶ûÖµ &true; ½«±»×ª»»Îª×Ö·û´® <literal>"1"</literal>£¬¶øÖµ &false; ½«±»±íʾΪ + <literal>""</literal>£¨¼´¿Õ×Ö·û´®£©¡£ÕâÑùÄú¾Í¿ÉÒÔËæÒâµØÔÚ²¼¶ûÖµºÍ×Ö·û´®Ö®¼ä½øÐбȽϡ£ </para> <para> - ÕûÊý»ò¸¡µãÊýÊýÖµÔÚת»»³É×Ö·û´®Ê±£¬×Ö·û´®¼´Îª±íʾÕâЩÊýÖµÊý×Ö£¨¸¡µãÊý»¹°üº¬ÓÐÖ¸Êý²¿·Ö£©¡£ + ÕûÊý»ò¸¡µãÊýÊýÖµÔÚת»»³É×Ö·û´®Ê±£¬×Ö·û´®¼´Îª±íʾÕâЩÊýÖµÊý×Ö£¨¸¡µãÊý»¹°üº¬ÓÐÖ¸Êý²¿·Ö£©¡£ </para> <para> - Êý×齫±»×ª»»³É×Ö·û´® <literal>"Array"</literal>£¬Òò´ËÄúÎÞ·¨Í¨¹ý <function>echo</function> »òÕß <function>print</function> º¯ÊýÀ´Êä³öÊý×éµÄÄÚÈÝ¡£Çë²Î¿¼ÏÂÎÄÒÔ»ñÈ¡¸ü¶àÌáʾ¡£ + Êý×齫±»×ª»»³É×Ö·û´® <literal>"Array"</literal>£¬Òò´ËÄúÎÞ·¨Í¨¹ý + <function>echo</function> »òÕß <function>print</function> + º¯ÊýÀ´Êä³öÊý×éµÄÄÚÈÝ¡£Çë²Î¿¼ÏÂÎÄÒÔ»ñÈ¡¸ü¶àÌáʾ¡£ </para> <para> - ¶ÔÏ󽫱»×ª»»³É×Ö·û´® <literal>"Object"</literal>¡£Èç¹ûÄúÒòΪµ÷ÊÔÐèÒª£¬ÐèÒª½«¶ÔÏóµÄ³ÉÔ±±äÁ¿´òÓ¡³öÀ´£¬ÇëÔĶÁÏÂÎÄ¡£Èç¹ûÄúÏ£ÍûµÃµ½¸Ã¶ÔÏóËùÒÀ¸½µÄÀàµÄÃû³Æ£¬ÇëʹÓú¯Êý <function>get_class</function>¡£ + ¶ÔÏ󽫱»×ª»»³É×Ö·û´® + <literal>"Object"</literal>¡£Èç¹ûÄúÒòΪµ÷ÊÔÐèÒª£¬ÐèÒª½«¶ÔÏóµÄ³ÉÔ±±äÁ¿´òÓ¡³öÀ´£¬ÇëÔĶÁÏÂÎÄ¡£Èç¹ûÄúÏ£ÍûµÃµ½¸Ã¶ÔÏóËùÒÀ¸½µÄÀàµÄÃû³Æ£¬ÇëʹÓú¯Êý + <function>get_class</function>¡£ </para> <para> - ×ÊÔ´ÀàÐͽ«»áÒÔ <literal>"Resource id #1"</literal> µÄ¸ñʽ±»×ª»»³É×Ö·û´®£¬ÆäÖÐ <literal>1</literal> ÊÇ PHP ÔÚÔËÐÐʱ¸ø×ÊÔ´Ö¸¶¨µÄΨһ±êʶ¡£Èç¹ûÄúÏ£Íû»ñÈ¡×ÊÔ´µÄÀàÐÍ£¬ÇëʹÓú¯Êý <function>get_resource_type</function>. + ×ÊÔ´ÀàÐͽ«»áÒÔ <literal>"Resource id #1"</literal> µÄ¸ñʽ±»×ª»»³É×Ö·û´®£¬ÆäÖÐ + <literal>1</literal> ÊÇ PHP + ÔÚÔËÐÐʱ¸ø×ÊÔ´Ö¸¶¨µÄΨһ±êʶ¡£Èç¹ûÄúÏ£Íû»ñÈ¡×ÊÔ´µÄÀàÐÍ£¬ÇëʹÓú¯Êý + <function>get_resource_type</function>. </para> <para> &null; ½«±»×ª»»³É¿Õ×Ö·û´®¡£ </para> <para> - ÕýÈçÒÔÉÏËùʾ£¬½«Êý×é¡¢¶ÔÏó»òÕß×ÊÔ´´òÓ¡³öÀ´£¬²¢²»ÄܸøÄúÌṩÈκιØÓÚÕâЩֵ±¾ÉíµÄÓÐÓõÄÐÅÏ¢¡£Çë²ÎÔĺ¯Êý <function>print_r</function> ºÍ <function>var_dump</function>£¬¶ÔÓÚµ÷ÊÔÀ´Ëµ£¬ÕâЩÊǸüºÃµÄ´òÓ¡ÖµµÄ·½·¨¡£ + ÕýÈçÒÔÉÏËùʾ£¬½«Êý×é¡¢¶ÔÏó»òÕß×ÊÔ´´òÓ¡³öÀ´£¬²¢²»ÄܸøÄúÌṩÈκιØÓÚÕâЩֵ±¾ÉíµÄÓÐÓõÄÐÅÏ¢¡£Çë²ÎÔĺ¯Êý <function>print_r</function> ºÍ <function>var_dump</function>£¬¶ÔÓÚµ÷ÊÔÀ´Ëµ£¬ÕâЩÊǸüºÃµÄ´òÓ¡ÖµµÄ·½·¨¡£ </para> <para> - Äú¿ÉÒÔ½« PHP µÄֵת»»Îª×Ö·û´®ÒÔÓÀ¾ÃµØ´¢´æËüÃÇ¡£ÕâÖÖ·½·¨±»³ÆÎª´®Ðл¯£¬Äú¿ÉÒÔÓú¯Êý <function>serialize</function> À´Íê³É¸Ã²Ù×÷¡£Èç¹ûÄúÔÚ°²×° PHP ʱ½¨Á¢ÁË <link linkend="ref.wddx">WDDX</link> Ö§³Ö£¬Äú»¹¿ÉÒÔ½« PHP µÄÖµ´®Ðл¯Îª XML ½á¹¹¡£ + Äú¿ÉÒÔ½« PHP µÄֵת»»Îª×Ö·û´®ÒÔÓÀ¾ÃµØ´¢´æËüÃÇ¡£ÕâÖÖ·½·¨±»³ÆÎª´®Ðл¯£¬Äú¿ÉÒÔÓú¯Êý <function>serialize</function> À´Íê³É¸Ã²Ù×÷¡£Èç¹ûÄúÔÚ°²×° PHP ʱ½¨Á¢ÁË <link linkend="ref.wddx">WDDX</link> Ö§³Ö£¬Äú»¹¿ÉÒÔ½« PHP µÄÖµ´®Ðл¯Îª XML ½á¹¹¡£ </para> </sect2> @@ -868,14 +967,16 @@ <informalexample> <programlisting role="php"> <![CDATA[ -$foo = 1 + "10.5"; // $foo is float (11.5) -$foo = 1 + "-1.3e3"; // $foo is float (-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) +<?php +$foo = 1 + "10.5"; // $foo is float (11.5) +$foo = 1 + "-1.3e3"; // $foo is float (-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 = 4 + "10.2 Little Piggies"; // $foo is float (14.2) -$foo = "10.0 pigs " + 1; // $foo is float (11) -$foo = "10.0 pigs " + 1.0; // $foo is float (11) +$foo = "10.0 pigs " + 1; // $foo is float (11) +$foo = "10.0 pigs " + 1.0; // $foo is float (11) +?> ]]> </programlisting> </informalexample> @@ -887,14 +988,16 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php echo "\$foo==$foo; type is " . gettype ($foo) . "<br />\n"; +?> ]]> </programlisting> </informalexample> </para> <para> - ²»ÒªÖ¸ÍûÔÚ½«Ò»¸ö×Ö·ûת»»³ÉÕûÐÍʱÄܹ»µÃµ½¸Ã×Ö·ûµÄ±àÂ루Äú¿ÉÄÜÒ²»áÔÚ C ÖÐÕâô×ö£©¡£Èç¹ûÄúÏ£ÍûÔÚ×Ö·û±àÂëºÍ×Ö·ûÖ®¼äת»»£¬ÇëʹÓà <function>ord</function> ºÍ <function>chr</function> º¯Êý¡£ + ²»ÒªÖ¸ÍûÔÚ½«Ò»¸ö×Ö·ûת»»³ÉÕûÐÍʱÄܹ»µÃµ½¸Ã×Ö·ûµÄ±àÂ루Äú¿ÉÄÜÒ²»áÔÚ C ÖÐÕâô×ö£©¡£Èç¹ûÄúÏ£ÍûÔÚ×Ö·û±àÂëºÍ×Ö·ûÖ®¼äת»»£¬ÇëʹÓà <function>ord</function> ºÍ <function>chr</function> º¯Êý¡£ </para> </sect2> @@ -933,7 +1036,12 @@ <informalexample> <programlisting role="php"> <![CDATA[ -array("foo" => "bar", 12 => true); +<?php +$arr = array("foo" => "bar", 12 => true); + +echo $arr["foo"]; // bar +echo $arr[12]; // 1 +?> ]]> </programlisting> </informalexample> @@ -948,7 +1056,13 @@ <informalexample> <programlisting role="php"> <![CDATA[ -array("somearray" => array(6 => 5, 13 => 9, "a" => 43)); +<?php +$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42)); + +echo $arr["somearray"][6]; // 5 +echo $arr["somearray"][13]; // 9 +echo $arr["somearray"]["a"]; // 42 +?> ]]> </programlisting> </informalexample> @@ -960,11 +1074,13 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php // This array is the same as ... array(5 => 43, 32, 56, "b" => 12); // ...this array array(5 => 43, 6 => 32, 7 => 56, "b" => 12); +?> ]]> </programlisting> </informalexample> @@ -996,18 +1112,16 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $arr = array(5 => 1, 12 => 2); - $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script - $arr["x"] = 42; // This adds a new element to // the array with key "x" - unset($arr[5]); // This removes the element from the array - unset($arr); // This deletes the whole array -]]> +?> +]]> </programlisting> </informalexample> </para> @@ -1025,13 +1139,17 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?PHP $a = array( 1 => 'one', 2 => 'two', 3 => 'three' ); unset( $a[2] ); /* ½«²úÉúÒ»¸öÊý×飬¶¨ÒåΪ $a = array( 1=>'one', 3=>'three'); ¶ø²»ÊÇ $a = array( 1 => 'one', 2 => 'three'); -*/ +*/ +$b = array_values($a); +// Now b is array(1 => 'one', 2 =>'three') +?> ]]> </programlisting> </informalexample> @@ -1053,20 +1171,82 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $foo[bar] = 'enemy'; echo $foo[bar]; // etc +?> ]]> </programlisting> </informalexample> ÕâÑùÊÇ´íµÄ£¬µ«¿ÉÒÔÆð×÷Óá£ÄÇôΪʲô´íÁËÄØ£¿Ô­ÒòÊÇ´Ë´úÂëÖÐÓÐÒ»¸ö䶨ÒåµÄ³£Á¿£¨bar£©¶ø²»ÊÇ×Ö·û´®£¨'bar'£­×¢ÒâÒýºÅ£©£¬¶ø PHP ¿ÉÄÜ»áÔÚÒÔºó¶¨Òå´Ë³£Á¿£¬²»ÐÒµÄÊÇÄãµÄ´úÂëÖÐÓÐͬÑùµÄÃû×Ö¡£Ëü¿ÉÒÔÆð×÷Óã¬ÒòΪ䶨Òå³£Á¿»á±»×ª»»ÎªÍ¬ÑùÃû×ÖµÄ×Ö·û´®¡£ </para> <para> + ÑÝʾ´ËЧӦµÄ¸ü¶àÀý×Ó£º + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php +// ÏÔʾËùÓдíÎó +error_reporting(E_ALL); + +$arr = array('fruit' => 'apple', 'veggie' => 'carrot'); + +// ÕýÈ· +print $arr['fruit']; // apple +print $arr['veggie']; // carrot + +// ²»ÕýÈ·¡£This works but also throws a PHP error of +// level E_NOTICE because of an undefined constant named fruit +// +// Notice: Use of undefined constant fruit - assumed 'fruit' in... +print $arr[fruit]; // apple + +// Let's define a constant to demonstrate what's going on. We +// will assign value 'veggie' to a constant named fruit. +define('fruit','veggie'); + +// Notice the difference now +print $arr['fruit']; // apple +print $arr[fruit]; // carrot + +// The following is okay as it's inside a string. Constants are not +// looked for within strings so no E_NOTICE error here +print "Hello $arr[fruit]"; // Hello apple + +// With one exception, braces surrounding arrays within strings +// allows constants to be looked for +print "Hello {$arr[fruit]}"; // Hello carrot +print "Hello {$arr['fruit']}"; // Hello apple + +// This will not work, results in a parse error such as: +// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING' +// This of course applies to using autoglobals in strings as well +print "Hello $arr['fruit']"; +print "Hello $_GET['foo']"; + +// Concatenation is another option +print "Hello " . $arr['fruit']; // Hello apple +?> +]]> + </programlisting> + </informalexample> + </para> + <para> + µ±´ò¿ª <function>error_reporting</function> À´ÏÔʾ + <constant>E_NOTICE</constant> ¼¶±ðµÄ´íÎó£¨ÀýÈ罫ÆäÉèΪ + <constant>E_ALL</constant>£©Ê±½«¿´µ½ÕâЩ´íÎó¡£Ä¬ÈÏÇé¿öÏ + <link linkend="ini.error-reporting">error_reporting</link> + ±»¹Ø±Õ²»ÏÔʾÕâЩ¡£ + </para> + <para> ºÍÔÚ<link linkend="language.types.array.syntax">Óï·¨</link>Ò»½ÚÖй涨µÄÒ»Ñù£¬ÔÚ·½À¨ºÅ£¨¡°<literal>[</literal>¡±ºÍ¡°<literal>]</literal>¡±£©Ö®¼ä±ØÐëÓÐÒ»¸ö±í´ïʽ¡£ÕâÒâζ×ÅÄã¿ÉÒÔÕâÑùд£º <informalexample> <programlisting role="php"> <![CDATA[ -echo $arr[ foo(true) ]; +<?php +echo $arr[somefunc($bar)]; +?> ]]> </programlisting> </informalexample> @@ -1074,9 +1254,11 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $error_descriptions[E_ERROR] = "A fatal error has occured"; $error_descriptions[E_WARNING] = "PHP issued a warning"; $error_descriptions[E_NOTICE] = "This is just an informal notice"; +?> ]]> </programlisting> </informalexample> @@ -1084,16 +1266,19 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $error_descriptions[1] = "A fatal error has occured"; $error_descriptions[2] = "PHP issued a warning"; $error_descriptions[8] = "This is just an informal notice"; +?> ]]> </programlisting> </informalexample> ÒòΪ <literal>E_ERROR</literal> µÈÓÚ <literal>1</literal>£¬µÈµÈ¡£ </para> <para> - ÄÇô <literal>$foo[bar]</literal> Ôõô¿ÉÄÜÆð×÷ÓõÄÄØ£¿ËüÆð×÷ÓÃÊÇÒòΪ¸ù¾ÝÓï·¨µÄÔ¤ÆÚ£¬<literal>bar</literal> ±»µ±³ÉÁËÒ»¸ö³£Á¿±í´ïʽ¡£È»¶ø£¬ÔÚÕâ¸öÀý×ÓÖв»´æÔÚÃûΪ + ÈçͬÎÒÃÇÔÚÒÔÉÏÀý×ÓÖнâÊ͵ÄÄÇÑù£¬<literal>$foo[bar]</literal> + Æð×÷Óõ«ÆäʵÊÇ´íÎóµÄ¡£ËüÆð×÷ÓÃÊÇÒòΪ¸ù¾ÝÓï·¨µÄÔ¤ÆÚ£¬<literal>bar</literal> ±»µ±³ÉÁËÒ»¸ö³£Á¿±í´ïʽ¡£È»¶ø£¬ÔÚÕâ¸öÀý×ÓÖв»´æÔÚÃûΪ <literal>bar</literal> µÄ³£Á¿¡£PHP ¾Í¼Ù¶¨ÄãÖ¸µÄÊÇ×ÖÃæÉ쵀 <literal>bar</literal>£¬Ò²¾ÍÊÇ×Ö·û´® <literal>"bar"</literal>£¬µ«ÄãÍü¼ÇдÒýºÅÁË¡£ </para> @@ -1104,44 +1289,10 @@ <literal>empty</literal> ºÍ <literal>default</literal> ÕâÁ½¸ö´ÊÁË£¬ÒòΪËûÃÇÊÇ<link linkend="reserved">±£Áô×Ö</link>¡£ </para> <note> - <para> - µ±Äã°Ñ <link linkend="function.error-reporting">error_reporting</link> ÉèΪ - <literal>E_ALL</literal> ʱ£¬Ä㽫»á¿´µ½ÎÞÂÛºÎʱʹÓÃÁË䶨ÒåµÄ - <literal>index</literal> ʱ£¬PHP ¶¼»á²úÉú֪ͨ¡£ - Çë¿´ÒÔϽű¾£º - <informalexample> - <programlisting role="php"> -<![CDATA[ -<?php - -// Turn on the display of all errors -error_reporting(E_ALL); - -// Define the test array -$abc = array("x" => "y"); - -// Access element with the *bad* method -echo $abc[x]; - -?> -]]> - </programlisting> - </informalexample> - Êä³öΪ£º - <informalexample> - <programlisting> -<![CDATA[ -<br /> -<b>Notice</b>: Use of undefined constant x - assumed 'x' in <b>/path/to/script.php</b> on -line <b>10</b><br /> -]]> - </programlisting> - </informalexample> - </para> - </note> - <note> <simpara> - ÔÚË«ÒýºÅ <type>string</type> ÖУ¬ÁíÒ»ÖÖÓï·¨ÊǺϷ¨µÄ¡£Ï¸½Ú¼û<link linkend="language.types.string.parsing">×Ö·û´®ÖеıäÁ¿½âÎö</link>¡£ + ÖØÉêÒ»´Î£¬ÔÚË«ÒýºÅ×Ö·û´®ÖУ¬²»¸øË÷Òý¼ÓÉÏÒýºÅÊǺϷ¨µÄÒò´Ë + <literal>"$foo[bar]"</literal>ÊǺϷ¨µÄ¡£ÖÁÓÚΪʲô²Î¼ûÒÔÉϵÄÀý×ÓºÍ<link + linkend="language.types.string.parsing">×Ö·û´®ÖеıäÁ¿½âÎö</link>ÖеĽâÊÍ¡£ </simpara> </note> </sect4> @@ -1152,15 +1303,15 @@ <title>ת»»ÎªÊý×é</title> <para> - ¶ÔÓÚÈκεÄÀàÐÍ£ºÕûÐÍ¡¢¸¡µã¡¢×Ö·û´®¡¢²¼¶ûºÍ×ÊÔ´£¬Èç¹ûÄú½«Ò»¸öֵת»»ÎªÊý×飬Äú½«µÃµ½Ò»¸ö½öÓÐÒ»¸öÔªËØµÄÊý×飨ÆäϱêΪ 0£©£¬¼´´¿Á¿ÖµµÄÆðʼֵ¡£ + ¶ÔÓÚÈκεÄÀàÐÍ£ºÕûÐÍ¡¢¸¡µã¡¢×Ö·û´®¡¢²¼¶ûºÍ×ÊÔ´£¬Èç¹ûÄú½«Ò»¸öֵת»»ÎªÊý×飬Äú½«µÃµ½Ò»¸ö½öÓÐÒ»¸öÔªËØµÄÊý×飨ÆäϱêΪ 0£©£¬¸ÃÔªËØ¼´Îª´Ë±êÁ¿µÄÖµ¡£ </para> - + <para> - Èç¹ûÄú½«Ò»¸ö¶ÔÏóת»»³ÉÒ»¸öÊý×飬ÄúËùµÃµ½µÄÊý×éµÄÔªËØÎª¸Ã¶ÔÏóµÄÊôÐÔ£¨³ÉÔ±±äÁ¿£©£¬Æä¼üÃûΪ³ÉÔ±±äÁ¿Ãû¡£ + Èç¹ûÄú½«Ò»¸ö¶ÔÏóת»»³ÉÒ»¸öÊý×飬ÄúËùµÃµ½µÄÊý×éµÄÔªËØÎª¸Ã¶ÔÏóµÄÊôÐÔ£¨³ÉÔ±±äÁ¿£©£¬Æä¼üÃûΪ³ÉÔ±±äÁ¿Ãû¡£ </para> - + <para> - Èç¹ûÄú½«Ò»¸ö &null; ֵת»»³ÉÊý×飬Äú½«µÃµ½Ò»¸ö¿ÕÊý×é¡£ + Èç¹ûÄú½«Ò»¸ö &null; ֵת»»³ÉÊý×飬Äú½«µÃµ½Ò»¸ö¿ÕÊý×é¡£ </para> </sect2> @@ -1170,9 +1321,10 @@ PHP ÖеÄÊý×éÀàÐÍÓзdz£¶àµÄÓÃ;£¬Òò´ËÕâÀïÓÐһЩÀý×ÓչʾÊý×éµÄÍêÕûÍþÁ¦¡£ </para> <para> - <informalexample> - <programlisting role="php"> + <informalexample> + <programlisting role="php"> <![CDATA[ +<?php // this $a = array( 'color' => 'red', 'taste' => 'sweet', @@ -1193,15 +1345,17 @@ $b[] = 'c'; // will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'), // or simply array('a', 'b', 'c') +?> ]]> - </programlisting> - </informalexample> - </para> + </programlisting> + </informalexample> + </para> <example> <title>ʹÓà array()</title> <programlisting role="php"> <![CDATA[ +<?php // Array as (property-)map $map = array( 'version' => 4, 'OS' => 'Linux', @@ -1227,9 +1381,9 @@ '02' => 77, // key = '02' 0 => 12 // the value 10 will be overwritten by 12 ); - // empty array -$empty = array(); +$empty = array(); +?> ]]> <!-- TODO example of - overwriting keys @@ -1243,6 +1397,7 @@ <title>¼¯ºÏ</title> <programlisting role="php"> <![CDATA[ +<?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { @@ -1255,6 +1410,7 @@ Do you like green? Do you like yellow? */ +?> ]]> </programlisting> </example> @@ -1271,6 +1427,7 @@ <title>¼¯ºÏ</title> <programlisting role="php"> <![CDATA[ +<?php foreach ($colors as $key => $color) { // won't work: //$color = strtoupper($color); @@ -1289,6 +1446,7 @@ [3] => YELLOW ) */ +?> ]]> </programlisting> </example> @@ -1299,6 +1457,7 @@ <title>»ùÓÚÒ»µÄÊý×é</title> <programlisting role="php"> <![CDATA[ +<?php $firstquarter = array(1 => 'January', 'February', 'March'); print_r($firstquarter); @@ -1310,6 +1469,7 @@ [3] => 'March' ) */ +?> ]]> </programlisting> </example> @@ -1318,12 +1478,14 @@ <title>Ìî³äÊý×é</title> <programlisting role="php"> <![CDATA[ +<?php // fill an array with all items from a directory $handle = opendir('.'); while ($file = readdir($handle)) { $files[] = $file; } -closedir($handle); +closedir($handle); +?> ]]> </programlisting> </example> @@ -1334,8 +1496,10 @@ <title>Êý×éÅÅÐò</title> <programlisting role="php"> <![CDATA[ +<?php sort($files); print_r($files); +?> ]]> </programlisting> </example> @@ -1346,6 +1510,7 @@ <title>µÝ¹éºÍ¶àάÊý×é</title> <programlisting role="php"> <![CDATA[ +<?php $fruits = array ( "fruits" => array ( "a" => "orange", "b" => "banana", "c" => "apple" @@ -1369,7 +1534,8 @@ unset($fruits["holes"][0]); // remove "first" // Create a new multi-dimensional array -$juices["apple"]["green"] = "good"; +$juices["apple"]["green"] = "good"; +?> ]]> </programlisting> </example> @@ -1379,6 +1545,7 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $arr1 = array(2, 3); $arr2 = $arr1; $arr2[] = 4; // $arr2 is changed, @@ -1386,6 +1553,7 @@ $arr3 = &$arr1; $arr3[] = 4; // now $arr1 and $arr3 are the same +?> ]]> </programlisting> </informalexample> @@ -1432,8 +1600,12 @@ Èç¹û½«Ò»¸ö¶ÔÏóת»»³É¶ÔÏó£¬Ëü½«²»»áÓÐÈκα仯¡£Èç¹ûÆäËüÈκÎÀàÐ͵ÄÖµ±»×ª»»³É¶ÔÏó£¬ÄÚÖÃÀà <literal>stdClass</literal> µÄÒ»¸öʵÀý½«±»½¨Á¢¡£Èç¹û¸ÃֵΪ &null;£¬ÔòеÄʵÀýΪ¿Õ¡£¶ÔÓÚÈÎºÎÆäËüµÄÖµ£¬ÃûΪ <literal>scalar</literal> µÄ³ÉÔ±±äÁ¿½«°üº¬¸ÃÖµ¡£ <informalexample> <programlisting role="php"> +<![CDATA[ +<?php $obj = (object) 'ciao'; echo $obj-&gt;scalar; // outputs 'ciao' +?> +]]> </programlisting> </informalexample> </para> @@ -1515,7 +1687,11 @@ &null; ÀàÐÍÖ»ÓÐÒ»¸öÖµ£¬¾ÍÊÇ´óСдÃô¸ÐµÄ¹Ø¼ü×Ö &null;¡£ <informalexample> <programlisting role="php"> -$var = NULL; +<![CDATA[ +<?php +$var = NULL; +?> +]]> </programlisting> </informalexample> </para> @@ -1549,10 +1725,10 @@ <sect2 id="language.types.callback"> <title>callback</title> <para> - ÓÐЩÖîÈç <function>call_user_function</function> »ò <function>usort</function> µÄº¯Êý½ÓÊÜÓû§×Ô¶¨ÒåµÄº¯Êý×÷Ϊһ¸ö²ÎÊý¡£Callback º¯Êý²»½ö¿ÉÒÔÊÇÒ»¸ö¼òµ¥µÄº¯Êý£¬Ëü»¹¿ÉÒÔÊÇÒ»¸ö¶ÔÏóµÄ·½·¨£¬°üÀ¨¾²Ì¬ÀàµÄ·½·¨¡£ + ÓÐЩÖîÈç <function>call_user_function</function> »ò <function>usort</function> µÄº¯Êý½ÓÊÜÓû§×Ô¶¨ÒåµÄº¯Êý×÷Ϊһ¸ö²ÎÊý¡£Callback º¯Êý²»½ö¿ÉÒÔÊÇÒ»¸ö¼òµ¥µÄº¯Êý£¬Ëü»¹¿ÉÒÔÊÇÒ»¸ö¶ÔÏóµÄ·½·¨£¬°üÀ¨¾²Ì¬ÀàµÄ·½·¨¡£ </para> <para> - Ò»¸ö PHP º¯ÊýÓú¯ÊýÃû×Ö·û´®À´´«µÝ¡£Äú¿ÉÒÔ´«µÝÈκÎÄÚ½¨µÄ»òÕßÓû§×Ô¶¨ÒåµÄº¯Êý£¬³ýÁË + Ò»¸ö PHP º¯ÊýÓú¯ÊýÃû×Ö·û´®À´´«µÝ¡£Äú¿ÉÒÔ´«µÝÈκÎÄÚ½¨µÄ»òÕßÓû§×Ô¶¨ÒåµÄº¯Êý£¬³ýÁË <function>array</function>£¬ <function>echo</function>£¬ <function>empty</function>£¬ @@ -1564,16 +1740,16 @@ <function>unset</function>¡£ </para> <para> - Ò»¸ö¶ÔÏóµÄ·½·¨ÒÔÊý×éµÄÐÎʽÀ´´«µÝ£¬Êý×éµÄ 0 ϱêÖ¸Ã÷¶ÔÏóÃû£¬Ï±ê 1 Ö¸Ã÷·½·¨Ãû¡£ + Ò»¸ö¶ÔÏóµÄ·½·¨ÒÔÊý×éµÄÐÎʽÀ´´«µÝ£¬Êý×éµÄ 0 ϱêÖ¸Ã÷¶ÔÏóÃû£¬Ï±ê 1 Ö¸Ã÷·½·¨Ãû¡£ </para> <para> - ¶ÔÓÚûÓÐʵÀý»¯Îª¶ÔÏóµÄ¾²Ì¬À࣬Ҫ´«µÝÆä·½·¨£¬½«Êý×é 0 ϱêÖ¸Ã÷µÄ¶ÔÏóÃû»»³É¸ÃÀàµÄÃû³Æ¼´¿É¡£ + ¶ÔÓÚûÓÐʵÀý»¯Îª¶ÔÏóµÄ¾²Ì¬À࣬Ҫ´«µÝÆä·½·¨£¬½«Êý×é 0 ϱêÖ¸Ã÷µÄ¶ÔÏóÃû»»³É¸ÃÀàµÄÃû³Æ¼´¿É¡£ </para> <para> <example> <title> - Callback º¯ÊýʵÀý + Callback º¯ÊýʵÀý </title> <programlisting role="php"> <![CDATA[ @@ -1619,15 +1795,17 @@ PHP µÄ×Ô¶¯ÀàÐÍת»»µÄÒ»¸öÀý×ÓÊǼӺš°+¡±¡£Èç¹ûÈκÎÒ»¸öÔËËãÊýÊǸ¡µãÊý£¬ÔòËùÓеÄÔËËãÊý¶¼±»µ±³É¸¡µãÊý£¬½á¹ûÒ²ÊǸ¡µãÊý¡£·ñÔòÔËËãÊý»á±»½âÊÍΪÕûÊý£¬½á¹ûÒ²ÊÇÕûÊý¡£×¢ÒâÕⲢûÓиıäÕâЩÔËËãÊý±¾ÉíµÄÀàÐÍ£»¸Ä±äµÄ½öÊÇÕâЩÔËËãÊýÈçºÎ±»ÇóÖµ¡£ <informalexample> <programlisting role="php"> +<![CDATA[ +<?php $foo = "0"; // $foo is string (ASCII 48) -<!-- bad example, no real operator (must be used with variable, modifies it too) -$foo++; // $foo is the string "1" (ASCII 49) ---> $foo += 2; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a float (3.3) $foo = 5 + "10 Little Piggies"; // $foo is integer (15) $foo = 5 + "10 Small Pigs"; // $foo is integer (15) -<!-- +?> +]]> +<!-- bad example, no real operator (must be used with variable, modifies it too) + $foo++; // $foo is the string "1" (ASCII 49) TODO: explain ++/- - behaviour with strings @@ -1665,8 +1843,12 @@ <para> <informalexample> <programlisting role="php"> +<![CDATA[ +<?php $a = "1"; // $a ÊÇ×Ö·û´® $a[0] = "f"; // ÊÇ×Ö·û´®Æ«ÒÆÁ¿Â𣿽á¹û»áÊÇʲô£¿ +?> +]]> </programlisting> </informalexample> </para> @@ -1675,14 +1857,18 @@ Ó¦¸Ã±ä³ÉÒ»¸öµÚÒ»¸öÔªËØÊÇ¡°f¡±µÄÊý×éÄØ£¬»¹ÊÇ¡°f¡±³ÉÁË×Ö·û´® $a µÄµÚÒ»¸ö×Ö·û£¿ </para> <para> - Ŀǰ°æ±¾µÄ PHP ½«ÒÔÉϵڶþ¸ö¸³ÖµÀí½â³É×Ö·û´®µÄÆ«ÒÆÁ¿±êʶ£¬¼´ $a ±ä³ÉÁË "f"£¬¾¡¹ÜÈç´Ë£¬ÕâÖÖ×Ô¶¯×ª»»µÄµØ½á¹ûÓ¦¸Ã±»ÈÏΪ䶨Òå¡£PHP 4 ÒýÈëÁËÐµĻ¨À¨ºÅÓï·¨À´·ÃÎÊ×Ö·û´®µÄ×Ö·û£¬ÇëʹÓøÃÓï·¨À´Ìæ´úÒÔÉϵIJÙ×÷£º + Ŀǰ°æ±¾µÄ PHP ½«ÒÔÉϵڶþ¸ö¸³ÖµÀí½â³É×Ö·û´®µÄÆ«ÒÆÁ¿±êʶ£¬¼´ $a ±ä³ÉÁË "f"£¬¾¡¹ÜÈç´Ë£¬ÕâÖÖ×Ô¶¯×ª»»µÄµØ½á¹ûÓ¦¸Ã±»ÈÏΪ䶨Òå¡£PHP 4 ÒýÈëÁËÐµĻ¨À¨ºÅÓï·¨À´·ÃÎÊ×Ö·û´®µÄ×Ö·û£¬ÇëʹÓøÃÓï·¨À´Ìæ´úÒÔÉϵIJÙ×÷£º <informalexample> <programlisting role="php"> +<![CDATA[ +<?php $a = "abc"; // $a Ϊһ¸ö×Ö·û´® $a{1} = "f"; // $a ĿǰΪ "afc" +?> +]]> </programlisting> </informalexample> - Çë²ÎÔÄÌâΪ¡°<link linkend="language.types.string.substr">ÓÃ×Ö·û·ÃÎÊ×Ö·û´®</link>¡±µÄÕ½ÚÒÔ»ñÈ¡¸ü¶àÐÅÏ¢¡£ + Çë²ÎÔÄÌâΪ¡°<link linkend="language.types.string.substr">ÓÃ×Ö·û·ÃÎÊ×Ö·û´®</link>¡±µÄÕ½ÚÒÔ»ñÈ¡¸ü¶àÐÅÏ¢¡£ </para> </note> @@ -1693,8 +1879,12 @@ PHP ÖеÄÀàÐÍÇ¿ÖÆ×ª»»ºÍ C Öеķdz£Ïñ£ºÔÚҪת»»µÄ±äÁ¿Ö®Ç°¼ÓÉÏÓÃÀ¨ºÅÀ¨ÆðÀ´µÄÄ¿±êÀàÐÍ¡£ <informalexample> <programlisting role="php"> +<![CDATA[ +<?php $foo = 10; // $foo is an integer $bar = (boolean) $foo; // $bar is a boolean +?> +]]> </programlisting> </informalexample> </para> @@ -1726,16 +1916,22 @@ ×¢ÒâÔÚÀ¨ºÅÄÚÔÊÐíÓпոñºÍÖÆ±í·û£¬ËùÒÔÏÂÃæÁ½¸öÀý×Ó¹¦ÄÜÏàͬ£º <informalexample> <programlisting role="php"> +<![CDATA[ +<?php $foo = (int) $bar; $foo = ( int ) $bar; +?> +]]> </programlisting> </informalexample> </para> <note> <para> - ΪÁ˽«Ò»¸ö±äÁ¿»¹Ô­Îª×Ö·û´®£¬Äú»¹¿ÉÒÔ½«±äÁ¿·ÅÖÃÔÚË«ÒýºÅÖÖ¡£ + ΪÁ˽«Ò»¸ö±äÁ¿»¹Ô­Îª×Ö·û´®£¬Äú»¹¿ÉÒÔ½«±äÁ¿·ÅÖÃÔÚË«ÒýºÅÖÖ¡£ <informalexample> <programlisting role="php"> +<![CDATA[ +<?php $foo = 10; // $foo is an integer $str = "$foo"; // $str is a string $fst = (string) $foo; // $fst is also a string @@ -1744,6 +1940,8 @@ if ($fst === $str) { echo "they are the same"; } +?> +]]> </programlisting> </informalexample> </para>
« previous php.doc.zh (#1071) next »