cvs: phpdoc-zh /language types.xml
| From: | Gregory Song | Date: | Sat, 28 Dec 2002 07:20:30 +0000 |
| Subject: | cvs: phpdoc-zh /language types.xml | ||
| Groups: | php.doc.zh | ||
| Request: | Send a blank email to doc-zh+get-936@lists.php.net to get a copy of this message | ||
gregory Sat Dec 28 02:20:30 2002 EDT
Modified files:
/phpdoc-zh/language types.xml
Log:
Up to date with the En version.
Index: phpdoc-zh/language/types.xml diff -u phpdoc-zh/language/types.xml:1.5 phpdoc-zh/language/types.xml:1.6 --- phpdoc-zh/language/types.xml:1.5 Sun Sep 29 23:25:33 2002 +++ phpdoc-zh/language/types.xml Sat Dec 28 02:20:29 2002 @@ -1,7 +1,8 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.5 $ --> -<!-- $Author: dallas $ --> -<!-- EN-Revision: 1.88 Maintainer: dallas Status: ready --> +<!-- $Revision: 1.6 $ --> +<!-- $Author: gregory $ --> +<!-- EN-Revision: 1.98 Maintainer: dallas Status: ready --> +<!-- CREDITS: Gregory --> <chapter id="language.types"> <title>ÀàÐÍ</title> @@ -9,11 +10,11 @@ <title>½éÉÜ</title> <simpara> - PHP Ö§³Ö°ËÖÖÔʼÀàÐÍ¡£<!-- (all types are primitive in php) --> + PHP Ö§³Ö°ËÖÖÔʼÀàÐÍ¡£ </simpara> <para> - ËÄÖÖ±êÁ¿ÀàÐÍ£º <!-- (basic, can't be split into parts) --> + ËÄÖÖ±êÁ¿ÀàÐÍ£º <itemizedlist> @@ -78,26 +79,33 @@ </listitem> </itemizedlist> - </para> - - <note> - <simpara> - ±¾ÊÖ²áÖÐÄã¾³£»áÅöµ½ <literal>mixed</literal> ²ÎÊý¡£Õâ¸öαÀàÐͱíʾ¸Ã²ÎÊýÓжàÖÖ¿ÉÄܵÄÀàÐÍ¡£ - </simpara> - <!-- - - Just an idea, maybe useful for some func-defs? - (at least it is for the operator-defs) - - <simpara> - In parameter definitions you can also encounter the 'number' pseudo-type, - that indicates a parameter that is either <type>integer</type> or - <type>float</type>. - </simpara> - --> - </note> + ΪÁËÈ·±£´úÂëµÄÒ×¶ÁÐÔ£¬±¾Êֲỹ½éÉÜÁËһЩ<link linkend="language.pseudo-types">αÀàÐÍ</link>£º + <itemizedlist> + + <listitem> + <simpara> + <link linkend="language.types.mixed">»ìºÍ£¨mixed£©</link> + </simpara> + </listitem> + + <listitem> + <simpara> + <link linkend="language.types.number">Êý×Ö£¨number£©</link> + </simpara> + </listitem> + + <listitem> + <simpara> + <link linkend="language.types.mixed">»ØÀ¡£¨callback£©</link> + </simpara> + </listitem> + + </itemizedlist> + Äú¿ÉÄÜ»¹»á¶Áµ½Ò»Ð©¹ØÓÚ¡°Ë«¾«¶È£¨double£©¡±ÀàÐ͵IJο¼¡£Êµ¼ÊÉÏ double ºÍ float ÊÇÏàͬµÄ£¬ÓÉÓÚһЩÀúÊ·µÄÔÒò£¬ÕâÁ½¸öÃû³ÆÍ¬Ê±´æÔÚ¡£ + </para> + <simpara> ±äÁ¿µÄÀàÐÍͨ³£²»ÊÇÓɳÌÐòÔ±É趨µÄ£¬È·ÇеØËµ£¬ÊÇÓÉ PHP ¸ù¾Ý¸Ã±äÁ¿Ê¹ÓõÄÉÏÏÂÎÄÔÚÔËÐÐʱ¾ö¶¨µÄ¡£ </simpara> @@ -105,20 +113,40 @@ <simpara> Èç¹ûÄãÏë²é¿´Ä³¸ö<link linkend="language.expressions">±í´ïʽ</link>µÄÖµºÍÀàÐÍ£¬Óà <function>var_dump</function>¡£ </simpara> - <simpara> - Èç¹ûÄãÖ»ÊÇÏëµÃµ½Ò»¸öÒ×¶Á¶®µÄÀàÐ͵ıí´ï·½Ê½ÓÃÓÚµ÷ÊÔ£¬Óà <function>gettype</function>¡£Òª²é¿´Ä³¸öÀàÐÍ£¬<emphasis>²»Òª</emphasis>Óà <function>gettype</function>£¬¶øÓà <literal>is_<replaceable>type</replaceable></literal> º¯Êý¡£ - </simpara> - <!-- TODO: example(s) would be great --> + <para> + Èç¹ûÄãÖ»ÊÇÏëµÃµ½Ò»¸öÒ×¶Á¶®µÄÀàÐ͵ıí´ï·½Ê½ÓÃÓÚµ÷ÊÔ£¬Óà <function>gettype</function>¡£Òª²é¿´Ä³¸öÀàÐÍ£¬<emphasis>²»Òª</emphasis>Óà <function>gettype</function>£¬¶øÓà <literal>is_<replaceable>type</replaceable></literal> º¯Êý¡£ÒÔÏÂÊÇһЩ·¶Àý£º + <informalexample> + <programlisting role="php"> +<![CDATA[ +$bool = TRUE; // a boolean +$str = "foo"; // a string +$int = 12; // an integer + +echo gettype($bool); // prints out "boolean" +echo gettype($str); // prints out "string" + +// If this is an integer, increment it by four +if (is_int($int)) { + $int += 4; +} + +// If $bool is a string, print it out +// (does not print out anything) +if (is_string($bool)) { + echo "String: $bool"; +} +]]> + </programlisting> + </informalexample> + </para> </note> <simpara> Èç¹ûÄãÒª½«Ò»¸ö±äÁ¿Ç¿ÖÆ×ª»»ÎªÄ³ÀàÐÍ£¬¿ÉÒÔ¶ÔÆäʹÓÃ<link linkend="language.types.typecasting">Ç¿ÖÆ×ª»»</link>»òÕß <function>settype</function> º¯Êý¡£ </simpara> <simpara> - ×¢Òâ±äÁ¿¸ù¾ÝÆäµ±Ê±µÄÀàÐÍÔÚÌØ¶¨³¡ºÏÏ»á±íÏÖ³ö²»Í¬µÄ·ç¸ñ¡£¸ü¶àÐÅÏ¢¼û<link linkend="language.types.type-juggling">ÀàÐÍÏ··¨</link>¡£ + ×¢Òâ±äÁ¿¸ù¾ÝÆäµ±Ê±µÄÀàÐÍÔÚÌØ¶¨³¡ºÏÏ»á±íÏÖ³ö²»Í¬µÄÖµ¡£¸ü¶àÐÅÏ¢¼û<link linkend="language.types.type-juggling">ÀàÐÍÏ··¨</link>¡£ </simpara> - - </sect1> <sect1 id="language.types.boolean"> @@ -138,11 +166,10 @@ <title>Óï·¨</title> <para> ÒªÖ¸¶¨Ò»¸ö²¼¶ûÖµ£¬Ê¹Óùؼü×Ö &true; »ò &false;¡£Á½¸ö¶¼ÊÇ´óСд²»Ãô¸ÐµÄ¡£ - <!-- technically they are just constants --> <informalexample> <programlisting role="php"> <![CDATA[ -$foo = True; // assign the value TRUE to $foo +$foo = True; // assign the value TRUE to $foo ]]> </programlisting> </informalexample> @@ -153,17 +180,18 @@ <informalexample> <programlisting role="php"> <![CDATA[ -// == is an operator which returns a boolean +// == is an operator which test +// equality and returns a boolean if ($action == "show_version") { echo "The version is 1.23"; } -// this is not necessary: +// this is not necessary... if ($show_separators == TRUE) { echo "<hr>\n"; } -// because you can simply type this: +// ...because you can simply type if ($show_separators) { echo "<hr>\n"; } @@ -200,7 +228,7 @@ linkend="language.types.string">×Ö·û´®</link> "0"</simpara> </listitem> <listitem> - <simpara>ûÓе¥ÔªµÄ<link linkend="language.types.array">Êý×é</link></simpara> + <simpara>ûÓгÉÔ±±äÁ¿µÄ<link linkend="language.types.array">Êý×é</link></simpara> </listitem> <listitem> <simpara>ûÓе¥ÔªµÄ<link linkend="language.types.object">¶ÔÏó</link></simpara> @@ -216,8 +244,19 @@ <literal>-1</literal> ºÍÆäËü·ÇÁãÖµ£¨²»ÂÛÕý¸º£©Ò»Ñù£¬±»ÈÏΪÊÇ &true;£¡ </simpara> </warning> - <!-- TODO: add a few examples, for the people only looking at - the examples... --> + <informalexample> + <programlisting role="php"> +<![CDATA[ +echo gettype((bool) ""); // bool(false) +echo gettype((bool) 1); // bool(true) +echo gettype((bool) -2); // bool(true) +echo gettype((bool) "foo"); // bool(true) +echo gettype((bool) 2.3e5); // bool(true) +echo gettype((bool) array(12)); // bool(true) +echo gettype((bool) array()); // bool(false) +]]> + </programlisting> + </informalexample> </para> </sect2> @@ -253,20 +292,23 @@ ]]> </programlisting> </example> - <!-- + ÔÚ×ÖÃæÉÏ£¬ÕûÐͱäÁ¿ÕýʽµÄ½á¹¹¿ÉÒÔΪ£º + <informalexample> + <programlisting> +<![CDATA[ +decimal : [1-9][0-9]* + | 0 - decimal : [1-9][0-9]* - | 0 - - hexadecimal : 0[xX][0-9a-fA-F]+ - - octal : 0[0-7]+ - - integer : [+-]?decimal - | [+-]?hexadecimal - | [+-]?octal - - --> +hexadecimal : 0[xX][0-9a-fA-F]+ + +octal : 0[0-7]+ + +integer : [+-]?decimal + | [+-]?hexadecimal + | [+-]?octal +]]> + </programlisting> + </informalexample> ÕûÐÍÊýµÄ×Ö³¤ºÍƽ̨Óйأ¬¾¡¹Üͨ³£×î´óÖµÊÇ´óÔ¼¶þÊ®ÒÚ£¨32 λÓзûºÅ£©¡£PHP ²»Ö§³ÖÎÞ·ûºÅÕûÊý¡£ </para> </sect2> @@ -308,12 +350,13 @@ </warning> </para> <para> - PHP ÖÐûÓÐÕû³ýµÄÔËËã·û¡£<literal>1/2</literal> ²úÉú³ö <type>float</type> <literal>0.5</literal>¡£ + PHP ÖÐûÓÐÕû³ýµÄÔËËã·û¡£<literal>1/2</literal> ²úÉú³ö<type>¸¡µãÊý</type> <literal>0.5</literal>¡£Äú¿ÉÒÔ×ÜÊÇÉáÆúСÊý²¿·Ö£¬»òÕßʹÓà <function>round</function> º¯Êý¡£ <informalexample> <programlisting role="php"> <![CDATA[ -var_dump( 25/7 ); -// Êä³öΪ£ºfloat(3.5714285714286) +var_dump(25/7); // float(3.5714285714286) +var_dump((int) (25/7)); // int(3) +var_dump(round(25/7)); // float(4) ]]> </programlisting> </informalexample> @@ -324,7 +367,7 @@ <sect2 id="language.types.integer.casting"> <title>ת»»ÎªÕûÐÎ</title> <simpara> - ÒªÃ÷ʾµØ½«Ò»¸öֵת»»Îª <type>integer</type>£¬Óà <literal>(int)</literal> »ò <literal>(integer)</literal> Ç¿ÖÆ×ª»»¡£²»¹ý´ó¶àÊýÇé¿ö϶¼²»ÐèÒªÇ¿ÖÆ×ª»»£¬ÒòΪµ±ÔËËã·û£¬º¯Êý»òÁ÷³Ì¿ØÖÆÐèÒªÒ»¸ö <type>integer</type> ²ÎÊýʱ£¬Öµ»á×Ô¶¯×ª»»¡£ + ÒªÃ÷ʾµØ½«Ò»¸öֵת»»Îª <type>integer</type>£¬Óà <literal>(int)</literal> »ò <literal>(integer)</literal> Ç¿ÖÆ×ª»»¡£²»¹ý´ó¶àÊýÇé¿ö϶¼²»ÐèÒªÇ¿ÖÆ×ª»»£¬ÒòΪµ±ÔËËã·û£¬º¯Êý»òÁ÷³Ì¿ØÖÆÐèÒªÒ»¸ö <type>integer</type> ²ÎÊýʱ£¬Öµ»á×Ô¶¯×ª»»¡£Äú»¹¿ÉÒÔͨ¹ýº¯Êý <function>intval</function> À´½«Ò»¸öֵת»»³ÉÕûÐÍ¡£ </simpara> <simpara> ²Î¼û<link linkend="language.types.type-juggling">ÀàÐÍÏ··¨</link>¡£ @@ -356,7 +399,7 @@ <informalexample> <programlisting role="php"> <![CDATA[ -echo (int) ( (0.1+0.7) * 10 ); // echoes 7! +echo (int) ( (0.1+0.7) * 10 ); // ÏÔʾ 7£¡ ]]> </programlisting> </informalexample> @@ -367,7 +410,7 @@ <sect3 id="language.types.integer.casting.from-string"> <title>´Ó×Ö·û´®×ª»»</title> <simpara> - ²Î¼û<link linkend="language.types.string.conversion">×Ö·û´®×ª»»</link>¡£ + ²Î¼û<link linkend="language.types.string.conversion">×Ö·û´®×ª»»ÎªÊý×Ö</link>¡£ </simpara> </sect3> @@ -381,16 +424,7 @@ </simpara> </caution> </para> - <!-- - - IMO, it would more sense as (int) $arr returned the - number of elements in $arr. This won't break anything, - since this behaviour was never defined before, and - (bool)(int) $arr will still behave the same. - - --> - </sect3> - + </sect3> </sect2> </sect1> @@ -398,18 +432,22 @@ <title>¸¡µãÐÍ</title> <para> ¸¡µãÊý£¨Ò²½Ð¡°floats¡±£¬¡°doubles¡±»ò¡°real numbers¡±£©¿ÉÒÔÓÃÒÔÏÂÈκÎÓï·¨¶¨Ò壺 - <synopsis> + <informalexample> + <programlisting role="php"> $a = 1.234; $a = 1.2e3; $a = 7E-10; - </synopsis> - <!-- - -LNUM [0-9]+ -DNUM ([0-9]*[\.][0-9]+)|([0-9]+[\.][0-9]*) -EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) - - --> + </programlisting> + </informalexample> + ÐÎʽÉÏ£º + <informalexample> + <programlisting role="php"> +LNUM [0-9]+ +DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*) +EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM}) + </programlisting> + </informalexample> ¸¡µãÊýµÄ×Ö³¤ºÍƽ̨Ïà¹Ø£¬¾¡¹Üͨ³£×î´óÖµÊÇ 1.8e308 ²¢¾ßÓÐ 14 λʮ½øÖÆÊý×ֵľ«¶È£¨64 λ IEEE ¸ñʽ£©¡£ </para> + <warning id="warn.float-precision"> <title>¸¡µãÊý¾«¶È</title> <para> @@ -423,15 +461,21 @@ linkend="ref.bc">ÈÎÒ⾫¶ÈÊýѧº¯Êý¿â</link>»òÕß <link linkend="ref.gmp">gmp º¯Êý¿â</link>¡£ </para> </warning> + + <sect2 id="language.types.float.casting"> + <title>ת»»Îª¸¡µãÊý</title> + + <para> + Èç¹ûÄúÏ£ÍûÁ˽âÓйغÎʱºÍÈçºÎ½«×Ö·û´®×ª»»³É¸¡µãÊýµÄÐÅÏ¢£¬Çë²éÔıêÌâΪ¡°<link linkend="language.types.string.conversion">½«×Ö·û´®×ª»»ÎªÊý×Ö</link>¡±µÄÓйØÕ½ڡ£¶ÔÓÚÆäËüÀàÐ͵ÄÖµ£¬ÆäÇé¿öÀàËÆÓÚÏȽ«Öµ×ª»»³ÉÕûÐÍ£¬È»ºóÔÙת»»³É¸¡µã¡£Çë²ÎÔÄ¡°<link linkend="language.types.integer.casting">ת»»ÎªÕûÐÍ</link>¡±ÓйØÕ½ÚÒÔ»ñÈ¡¸ü¶àÐÅÏ¢¡£ + </para> + </sect2> </sect1> <sect1 id="language.types.string"> <title>×Ö·û´®</title> <para> - <type>string</type> ÊÇһϵÁÐ×Ö·û¡£ÔÚ PHP ÖУ¬×Ö·ûºÍ×Ö½ÚÒ»Ñù£¬Ò²¾ÍÊÇ˵£¬Ò»¹²ÓÐ 256 ÖÖ²»Í¬×Ö·ûµÄ¿ÉÄÜÐÔ¡£ÕâÒ²°µÊ¾ PHP ¶Ô Unicode ûÓб¾µØÖ§³Ö¡£ - <!-- how about unicode? will we support that eventually? Are - there current any ways to work with unicode? - --> + <type>string</type> ÊÇһϵÁÐ×Ö·û¡£ÔÚ PHP ÖУ¬×Ö·ûºÍ×Ö½ÚÒ»Ñù£¬Ò²¾ÍÊÇ˵£¬Ò»¹²ÓÐ 256 ÖÖ²»Í¬×Ö·ûµÄ¿ÉÄÜÐÔ¡£ÕâÒ²°µÊ¾ PHP ¶Ô Unicode ûÓб¾µØÖ§³Ö¡£Çë²ÎÔĺ¯Êý <function>utf8_enncode</function> + ºÍ <function>utf8_decode</function> ÒÔÁ˽âÓÐ¹Ø Unicode Ö§³Ö¡£ </para> <note> <simpara> @@ -483,8 +527,8 @@ <programlisting role="php"> <![CDATA[ echo 'this is a simple string'; -echo 'You can also have embedded newlines in strings, -like this way.'; +echo 'You can also have embedded newlines in strings +this way'; echo 'Arnold once said: "I\'ll be back"'; // output: ... "I'll be back" echo 'Are you sure you want to delete C:\\*.*?'; @@ -620,8 +664,8 @@ ¶¨½ç·ûÖ§³ÖÊÇ PHP 4 ÖмÓÈëµÄ¡£ </para> </note> - </sect3> + <sect3 id="language.types.string.parsing"> <title>±äÁ¿½âÎö</title> <simpara> @@ -633,8 +677,8 @@ </simpara> <simpara> ¸´ÔÓÓï·¨ÊÇ PHP 4 Òý½øµÄ£¬¿ÉÒÔÓû¨À¨ºÅÀ¨ÆðÒ»¸ö±í´ïʽ¡£ - <!-- XXX was it? and starting with what version exactly? --> </simpara> + <sect4 id="language.types.string.parsing.simple"> <title>¼òµ¥Óï·¨</title> <simpara> @@ -645,7 +689,7 @@ <![CDATA[ $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 $beers"; // won't work, 's' is a valid character for varnames echo "He drank some ${beer}s"; // works ]]> </programlisting> @@ -653,17 +697,14 @@ <simpara> ͬÑùÒ²¿ÉÒÔ½âÎöÊý×éË÷Òý»òÕß¶ÔÏóÊôÐÔ¡£¶ÔÓÚÊý×éË÷Òý£¬ÓÒ·½À¨ºÅ£¨<literal>]</literal>£©±êÖ¾×ÅË÷ÒýµÄ½áÊø¡£¶ÔÏóÊôÐÔÔòºÍ¼òµ¥±äÁ¿ÊÊÓÃͬÑùµÄ¹æÔò£¬¾¡¹Ü¶ÔÓÚ¶ÔÏóÊôÐÔûÓÐÏñ±äÁ¿ÄÇÑùµÄС¼¼ÇÉ¡£ <!-- XXX isn't &true; :(, this would be the trick - Also, - the same trick with curly-braces works if you - want to limit the greediness of parsers (aren't they - paying them enough or something?). + Also, the same trick with curly-braces works if you + want to limit the greediness of parsers. --> - </simpara> <informalexample> <programlisting role="php"> <![CDATA[ -$fruits = array( 'strawberry' => 'red' , 'banana' => 'yellow' ); +$fruits = array('strawberry' => 'red', 'banana' => 'yellow'); // note that this works differently outside string-quotes. echo "A banana is $fruits[banana]."; @@ -686,6 +727,7 @@ ¶ÔÓÚÈκθü¸´ÔÓµÄÇé¿ö£¬Ó¦¸ÃʹÓø´ÔÓÓï·¨¡£ </simpara> </sect4> + <sect4 id="language.types.string.parsing.complex"> <title>¸´ÔÓ£¨»¨À¨ºÅ£©Óï·¨</title> <simpara> @@ -701,7 +743,7 @@ echo "This is { $great}"; // ²»ÐУ¬Êä³öΪ£ºThis is { fantastic} echo "This is {$great}"; // ¿ÉÒÔ£¬Êä³öΪ£ºThis is fantastic echo "This square is {$square->width}00 centimeters broad."; -echo "This works: {$arr[4][3]}"; +echo "This works: {$arr[4][3]}"; // This is wrong for the same reason // as $foo[bar] is wrong outside a string. @@ -736,35 +778,15 @@ <example> <title>һЩ×Ö·û´®Àý×Ó</title> <programlisting role="php"> -<!-- TODO: either move these examples to a example section, -as with arrays, or distribute them under the applicable -sections. --> <![CDATA[ <?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 */ +// Get the first character of a string $str = 'This is a test.'; $first = $str{0}; -/* Get the last character of a string. */ +// Get the last character of a string. $str = 'This is still a test.'; -$last = $str{strlen($str)-1}; +$last = $str{strlen($str)-1}; ?> ]]> </programlisting> @@ -775,7 +797,7 @@ </sect2><!-- end syntax --> <sect2 id="language.types.string.useful-funcs"> - <title>ʵÓú¯Êý</title><!-- and operators --> + <title>ʵÓú¯Êý¼°²Ù×÷·û</title> <para> ×Ö·û´®¿ÉÒÔÓá°.¡±£¨µã£©ÔËËã·ûÁ¬½Ó¡£×¢ÒâÕâÀï²»ÄÜÓá°+¡±£¨¼Ó£©ÔËËã·û¡£¸ü¶àÐÅÏ¢²Î¼û<link linkend="language.operators.string">×Ö·û´®ÔËËã·û</link>¡£ @@ -795,8 +817,44 @@ ×îºó£¬Èç¹û»¹ÊÇÕÒ²»µ½ÄãÒªµÄº¯Êý£¬²Î¼û<link linkend="ref.ctype">×Ö·ûÀàÐͺ¯Êý¿â</link>¡£ </simpara> </sect2> - <sect2 id="language.types.string.conversion"> + + <sect2 id="language.types.string.casting"> <title>×Ö·û´®×ª»»</title> + + <para> + Äú¿ÉÒÔÓà <literal>(string)</literal> ±ê¼Ç»òÕß <function>strval</function> º¯Êý½«Ò»¸öֵת»»Îª×Ö·û´®¡£µ±Ä³±í´ïʽÐèÒª×Ö·û´®Ê±£¬×Ö·û´®µÄת»»»áÔÚ±í´ïʽ·¶Î§ÄÚ×Ô¶¯Íê³É¡£ÀýÈçµ±ÄãʹÓà <function>echo</function> »òÕß <function>print</function> º¯Êýʱ£¬»òÕß½«Ò»¸ö±äÁ¿ÖµÓëÒ»¸ö×Ö·û´®½øÐбȽϵÄʱºò¡£ + </para> + + <para> + ²¼¶ûÖµ &true; ½«±»×ª»»Îª×Ö·û´® <literal>"1"</literal>£¬¶øÖµ &false; ½«±»±íʾΪ <literal>""</literal>£¨¼´¿Õ×Ö·û´®£©¡£ÕâÑùÄú¾Í¿ÉÒÔËæÒâµØÔÚ²¼¶ûÖµºÍ×Ö·û´®Ö®¼ä½øÐбȽϡ£ + </para> + <para> + ÕûÊý»ò¸¡µãÊýÊýÖµÔÚת»»³É×Ö·û´®Ê±£¬×Ö·û´®¼´Îª±íʾÕâЩÊýÖµÊý×Ö£¨¸¡µãÊý»¹°üº¬ÓÐÖ¸Êý²¿·Ö£©¡£ + </para> + <para> + Êý×齫±»×ª»»³É×Ö·û´® <literal>"Array"</literal>£¬Òò´ËÄúÎÞ·¨Í¨¹ý <function>echo</function> »òÕß <function>print</function> º¯ÊýÀ´Êä³öÊý×éµÄÄÚÈÝ¡£Çë²Î¿¼ÏÂÎÄÒÔ»ñÈ¡¸ü¶àÌáʾ¡£ + </para> + <para> + ¶ÔÏ󽫱»×ª»»³É×Ö·û´® <literal>"Object"</literal>¡£Èç¹ûÄúÒòΪµ÷ÊÔÐèÒª£¬ÐèÒª½«¶ÔÏóµÄ³ÉÔ±±äÁ¿´òÓ¡³öÀ´£¬ÇëÔĶÁÏÂÎÄ¡£Èç¹ûÄúÏ£ÍûµÃµ½¸Ã¶ÔÏóËùÒÀ¸½µÄÀàµÄÃû³Æ£¬ÇëʹÓú¯Êý <function>get_class</function>¡£ + </para> + <para> + ×ÊÔ´ÀàÐͽ«»áÒÔ <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>£¬¶ÔÓÚµ÷ÊÔÀ´Ëµ£¬ÕâЩÊǸüºÃµÄ´òÓ¡ÖµµÄ·½·¨¡£ + </para> + + <para> + Äú¿ÉÒÔ½« PHP µÄֵת»»Îª×Ö·û´®ÒÔÓÀ¾ÃµØ´¢´æËüÃÇ¡£ÕâÖÖ·½·¨±»³ÆÎª´®Ðл¯£¬Äú¿ÉÒÔÓú¯Êý <function>serialize</function> À´Íê³É¸Ã²Ù×÷¡£Èç¹ûÄúÔÚ°²×° PHP ʱ½¨Á¢ÁË <link linkend="ref.wddx">WDDX</link> Ö§³Ö£¬Äú»¹¿ÉÒÔ½« PHP µÄÖµ´®Ðл¯Îª XML ½á¹¹¡£ + </para> + </sect2> + + <sect2 id="language.types.string.conversion"> + <title>×Ö·û´®×ª»»ÎªÊýÖµ</title> <simpara> µ±Ò»¸ö×Ö·û´®±»µ±×÷Êý×ÖÀ´Çóֵʱ£¬¸ù¾ÝÒÔϹæÔòÀ´¾ö¶¨½á¹ûµÄÀàÐͺÍÖµ¡£ @@ -835,6 +893,10 @@ </informalexample> </para> + <para> + ²»ÒªÖ¸ÍûÔÚ½«Ò»¸ö×Ö·ûת»»³ÉÕûÐÍʱÄܹ»µÃµ½¸Ã×Ö·ûµÄ±àÂ루Äú¿ÉÄÜÒ²»áÔÚ C ÖÐÕâô×ö£©¡£Èç¹ûÄúÏ£ÍûÔÚ×Ö·û±àÂëºÍ×Ö·ûÖ®¼äת»»£¬ÇëʹÓà <function>ord</function> ºÍ <function>chr</function> º¯Êý¡£ + </para> + </sect2> </sect1><!-- end string --> @@ -846,8 +908,6 @@ </para> <para> ½âÊÍÕâЩ½á¹¹³¬³öÁ˱¾ÊÖ²áµÄ·¶Î§£¬µ«¶ÔÓÚÿÖֽṹÄãÖÁÉٻᷢÏÖÒ»¸öÀý×Ó¡£ÒªµÃµ½ÕâЩ½á¹¹µÄ¸ü¶àÐÅÏ¢£¬ÎÒÃǽ¨ÒéÄã²Î¿¼Óйش˹ãÀ«Ö÷ÌâµÄÍâ²¿Öø×÷¡£ - <!-- like goodrich&tamassia: datastructures and algorithmes. - Only, the subtitle is: in Java, and it's quite academic too --> </para> <sect2 id="language.types.array.syntax"> @@ -860,38 +920,63 @@ <literal><replaceable>key</replaceable> => <replaceable>value</replaceable></literal> ²ÎÊý¶Ô¡£ </para> <para> + <synopsis> +array( <optional> <replaceable>key</replaceable> => </optional> <replaceable +>value</replaceable> + , ... + ) +// <replaceable>key</replaceable> ²»ÊÇ<type>×Ö·û´®</type>¾ÍÊÇ<type>ÕûÊý</type> +// <replaceable>value</replaceable> ¿ÉÒÔΪÈκÎÖµ + </synopsis> + </para> + <para> + <informalexample> + <programlisting role="php"> +<![CDATA[ +array("foo" => "bar", 12 => true); +]]> + </programlisting> + </informalexample> + </para> + <para> <varname>key</varname> ¿ÉÒÔÊÇ <type>integer</type> »òÕß <type>string</type>¡£Èç¹û¼üÃûÊÇÒ»¸ö <type>integer</type> µÄ±ê×¼±í´ï·½·¨£¬Ôò±»½âÊÍΪÕûÊý£¨ÀýÈç <literal>"8"</literal> ½«±»½âÊÍΪ - <literal>8</literal>£¬¶ø <literal>"08"</literal> ½«±»½âÊÍΪ <literal>"08"</literal>£©¡£ + <literal>8</literal>£¬¶ø <literal>"08"</literal> ½«±»½âÊÍΪ <literal>"08"</literal>£©¡£PHP ÖÐÊý×éϱêµÄ±äÁ¿ÀàÐͲ»»á¶ÔÊý×éÔì³ÉÓ°Ï죬Êý×éµÄÀàÐÍÖ»ÓÐÒ»ÖÖ£¬Ëü¿ÉÒÔͬʱ°üº¬ÕûÐͺÍ×Ö·û´®Ð͵Äϱꡣ </para> <para> Öµ¿ÉÒÔÊÇÈκÎÖµ¡£ + <informalexample> + <programlisting role="php"> +<![CDATA[ +array("somearray" => array(6 => 5, 13 => 9, "a" => 43)); +]]> + </programlisting> + </informalexample> </para> <para> Èç¹ûÊ¡ÂÔ¼üÃû£¬ÔòÈ¡µ±Ç°×î´óµÄÕûÊýË÷Òý£¬¶øÐµļüÃû½«ÊǸÃÖµ¼ÓÒ»¡£ÕûÊý¿ÉÒÔΪ¸º£¬ËùÒÔ¶ÔÓÚ¸ºµÄË÷ÒýÒ²ÊÇÕâÑù¡£ÀýÈç×î´óµÄË÷ÒýÊÇ <literal>-6</literal> ½«µ¼ÖÂеļüÃûÊÇ <literal>-5</literal>¡£Èç¹û»¹²»´æÔÚÕûÊýË÷Òý£¬Ôò¼üÃû½«Îª <literal>0</literal>£¨Á㣩¡£Èç¹ûÄãÖ¸¶¨µÄ¼üÃûÒѾÓÐÁËÖµ£¬Ôò¸ÃÖµ»á±»¸²¸Ç¡£ + <informalexample> + <programlisting role="php"> +<![CDATA[ +// 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> </para> <para> - ʹÓà <literal>true</literal> ×÷Ϊ¼üÃû½«Ê¹ <type>integer</type> <literal>1</literal> ³ÉΪ¼üÃû¡£Ê¹Óà - <literal>false</literal> ×÷Ϊ¼üÃû½«Ê¹ <type>integer</type> <literal>0</literal> ³ÉΪ¼üÃû¡£Ê¹Óà + ʹÓà &true; ×÷Ϊ¼üÃû½«Ê¹ <type>integer</type> <literal>1</literal> ³ÉΪ¼üÃû¡£Ê¹Óà + &false; ×÷Ϊ¼üÃû½«Ê¹ <type>integer</type> <literal>0</literal> ³ÉΪ¼üÃû¡£Ê¹Óà <literal>NULL</literal> ×÷Ϊ¼üÃû½«µÈͬÓÚʹÓÿÕ×Ö·û´®¡£Ê¹ÓÿÕ×Ö·û´®×÷Ϊ¼üÃû½«Ð½¨£¨»ò¸²¸Ç£©Ò»¸öÓÿÕ×Ö·û´®×÷Ϊ¼üÃûµÄÖµ£¬ÕâºÍÓÿյķ½À¨ºÅ²»Ò»Ñù¡£ </para> <para> ²»ÄÜÓÃÊý×éºÍ¶ÔÏó×÷Ϊ¼üÃû¡£ÕâÑù×ö»áµ¼ÖÂÒ»¸ö¾¯¸æ£º<literal>Illegal offset type</literal>¡£ - </para> - - <para> - <synopsis> -array( <optional> <replaceable>key</replaceable> => </optional> <replaceable ->value</replaceable> - , ... - ) -// <replaceable>key</replaceable> is either <type>string</type - > or nonnegative <type>integer</type> -// <replaceable>value</replaceable> can be anything - </synopsis> - </para> + </para> </sect3> <sect3 id="language.types.array.syntax.modifying"> @@ -904,15 +989,29 @@ <synopsis> $arr[<replaceable>key</replaceable>] = <replaceable>value</replaceable>; $arr[] = <replaceable>value</replaceable>; -// <replaceable>key</replaceable> is either <type>string</type> or nonnegative <type>integer</type> -// <replaceable>value</replaceable> can be anything +// <replaceable>key</replaceable> ²»ÊÇ <type>string</type> ¾ÍÊǷǸºµÄ <type>integer</type> +// <replaceable>value</replaceable> ¿ÉÒÔΪÈκÎÖµ¡£ </synopsis> Èç¹û <varname>$arr</varname> »¹²»´æÔÚ£¬½«»áн¨Ò»¸ö¡£ÕâÒ²ÊÇÒ»ÖÖ¶¨ÒåÊý×éµÄÌæ»»·½·¨¡£Òª¸Ä±äÒ»¸öÖµ£¬Ö»Òª¸øËü¸³Ò»¸öÐÂÖµ¡£Èç¹ûҪɾ³ýÒ»¸ö¼üÃû£¯Öµ¶Ô£¬Òª¶ÔËüÓà <function>unset</function>¡£ - </para> + <informalexample> + <programlisting role="php"> +<![CDATA[ +$arr = array(5 => 1, 12 => 2); - </sect3> - +$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> + </sect3> </sect2><!-- end syntax --> <sect2 id="language.types.array.useful-funcs"> @@ -942,7 +1041,6 @@ <para> <link linkend="control-structures.foreach">foreach</link> ¿ØÖƽṹÊÇרÃÅÓÃÓÚÊý×éµÄ¡£ËüÌṩÁËÒ»¸ö¼òµ¥µÄ·½·¨À´±éÀúÊý×é¡£ </para> - </sect2> <sect2 id="language.types.array.donts"> @@ -976,9 +1074,9 @@ <informalexample> <programlisting role="php"> <![CDATA[ -$error_descriptions[E_ERROR] = "A fatal error has occured"; +$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"; +$error_descriptions[E_NOTICE] = "This is just an informal notice"; ]]> </programlisting> </informalexample> @@ -1002,15 +1100,44 @@ <sect4> <title>ÄÇôΪʲôÕâÑù×ö²»ºÃ£¿</title> <para> - ÔÚδÀ´µÄijһʱ¿Ì£¬PHP ¿ª·¢Ð¡×é¿ÉÄÜ»áÏëÐÂÔöÒ»¸ö³£Á¿»òÕ߹ؼü×Ö£¬ÄÇÄã¾ÍÓÐÂé·³ÁË¡£ÀýÈçÄãÒѾ²»ÄÜÕâÑùÓà + ÔÚδÀ´µÄijһʱ¿Ì£¬PHP ¿ª·¢Ð¡×é¿ÉÄÜ»áÏëÐÂÔöÒ»¸ö³£Á¿»òÕ߹ؼü×Ö£¬»òÕßÄú¿ÉÄÜÏ£ÍûÔÚÒÔºóÔÚÄúµÄ³ÌÐòÖÐÒýÈëеij£Á¿£¬ÄÇÄã¾ÍÓÐÂé·³ÁË¡£ÀýÈçÄãÒѾ²»ÄÜÕâÑùÓà <literal>empty</literal> ºÍ <literal>default</literal> ÕâÁ½¸ö´ÊÁË£¬ÒòΪËûÃÇÊÇ<link linkend="reserved">±£Áô×Ö</link>¡£ </para> <note> - <simpara> + <para> µ±Äã°Ñ <link linkend="function.error-reporting">error_reporting</link> ÉèΪ <literal>E_ALL</literal> ʱ£¬Ä㽫»á¿´µ½ÎÞÂÛºÎʱʹÓÃÁË䶨ÒåµÄ - <literal>index</literal> ʱ£¬PHP ¶¼»á²úÉú֪ͨ£¨ÔÚÄãµÄ½Å±¾ÖмÓÈëÒ»ÐÐ <literal>error_reporting(E_ALL);</literal>£©¡£ - </simpara> + <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> @@ -1021,6 +1148,22 @@ </sect3> </sect2> + <sect2 id="language.types.array.casting"> + <title>ת»»ÎªÊý×é</title> + + <para> + ¶ÔÓÚÈκεÄÀàÐÍ£ºÕûÐÍ¡¢¸¡µã¡¢×Ö·û´®¡¢²¼¶ûºÍ×ÊÔ´£¬Èç¹ûÄú½«Ò»¸öֵת»»ÎªÊý×飬Äú½«µÃµ½Ò»¸ö½öÓÐÒ»¸öÔªËØµÄÊý×飨ÆäϱêΪ 0£©£¬¼´´¿Á¿ÖµµÄÆðʼֵ¡£ + </para> + + <para> + Èç¹ûÄú½«Ò»¸ö¶ÔÏóת»»³ÉÒ»¸öÊý×飬ÄúËùµÃµ½µÄÊý×éµÄÔªËØÎª¸Ã¶ÔÏóµÄÊôÐÔ£¨³ÉÔ±±äÁ¿£©£¬Æä¼üÃûΪ³ÉÔ±±äÁ¿Ãû¡£ + </para> + + <para> + Èç¹ûÄú½«Ò»¸ö &null; ֵת»»³ÉÊý×飬Äú½«µÃµ½Ò»¸ö¿ÕÊý×é¡£ + </para> + </sect2> + <sect2 id="language.types.array.examples"> <title>Àý×Ó</title> <para> @@ -1031,24 +1174,24 @@ <programlisting role="php"> <![CDATA[ // this -$a = array( 'color' => 'red' - , 'taste' => 'sweet' - , 'shape' => 'round' - , 'name' => 'apple' - , 4 // key will be 0 +$a = array( 'color' => 'red', + 'taste' => 'sweet', + 'shape' => 'round', + 'name' => 'apple', + 4 // key will be 0 ); // is completely equivalent with $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; -$a['name'] = 'apple'; +$a['name'] = 'apple'; $a[] = 4; // key will be 0 $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; -// will result in the array array( 0 => 'a' , 1 => 'b' , 2 => 'c' ), +// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'), // or simply array('a', 'b', 'c') ]]> </programlisting> @@ -1060,40 +1203,38 @@ <programlisting role="php"> <![CDATA[ // Array as (property-)map -$map = array( 'version' => 4 - , 'OS' => 'Linux' - , 'lang' => 'english' - , 'short_tags' => true +$map = array( 'version' => 4, + 'OS' => 'Linux', + 'lang' => 'english', + 'short_tags' => true ); // strictly numerical keys -$array = array( 7 - , 8 - , 0 - , 156 - , -10 +$array = array( 7, + 8, + 0, + 156, + -10 ); -// this is the same as array( 0 => 7, 1 => 8, ...) +// this is the same as array(0 => 7, 1 => 8, ...) -$switching = array( 10 // key = 0 - , 5 => 6 - , 3 => 7 - , 'a' => 4 - , 11 // key = 6 (maximum of integer-indices was 5) - , '8' => 2 // key = 8 (integer!) - , '02' => 77 // key = '02' - , 0 => 12 // the value 10 will be overwritten by 12 +$switching = array( 10, // key = 0 + 5 => 6, + 3 => 7, + 'a' => 4, + 11, // key = 6 (maximum of integer-indices was 5) + '8' => 2, // key = 8 (integer!) + '02' => 77, // key = '02' + 0 => 12 // the value 10 will be overwritten by 12 ); // empty array $empty = array(); ]]> <!-- TODO example of -- mixed keys - overwriting keys -- integer keys as string - using vars/functions as key/values -- mixed skipping +- warning about references --> </programlisting> </example> @@ -1102,9 +1243,9 @@ <title>¼¯ºÏ</title> <programlisting role="php"> <![CDATA[ -$colors = array('red','blue','green','yellow'); +$colors = array('red', 'blue', 'green', 'yellow'); -foreach ( $colors as $color ) { +foreach ($colors as $color) { echo "Do you like $color?\n"; } @@ -1174,13 +1315,12 @@ </example> </para> <example> - <title>Ìî³äÕæÕýµÄÊý×é</title> + <title>Ìî³äÊý×é</title> <programlisting role="php"> <![CDATA[ // fill an array with all items from a directory $handle = opendir('.'); -while ($file = readdir($handle)) -{ +while ($file = readdir($handle)) { $files[] = $file; } closedir($handle); @@ -1188,7 +1328,7 @@ </programlisting> </example> <para> - Êý×éÊÇÓÐÐòµÄ¡£ÄãÒ²¿ÉÒÔʹÓò»Í¬µÄÅÅÐòº¯ÊýÀ´¸Ä±ä˳Ðò¡£¸ü¶àÐÅÏ¢²Î¼û<link linkend="ref.array">Êý×麯Êý¿â</link>¡£ + Êý×éÊÇÓÐÐòµÄ¡£ÄãÒ²¿ÉÒÔʹÓò»Í¬µÄÅÅÐòº¯ÊýÀ´¸Ä±ä˳Ðò¡£¸ü¶àÐÅÏ¢²Î¼û<link linkend="ref.array">Êý×麯Êý¿â</link>¡£Äú¿ÉÒÔÓà <function>count</function> º¯ÊýÀ´Êý³öÊý×éÖÐÔªËØµÄ¸öÊý¡£ </para> <example> <title>Êý×éÅÅÐò</title> @@ -1206,228 +1346,51 @@ <title>µÝ¹éºÍ¶àάÊý×é</title> <programlisting role="php"> <![CDATA[ -$fruits = array ( "fruits" => array ( "a" => "orange" - , "b" => "banana" - , "c" => "apple" - ) - , "numbers" => array ( 1 - , 2 - , 3 - , 4 - , 5 - , 6 - ) - , "holes" => array ( "first" - , 5 => "second" - , "third" +$fruits = array ( "fruits" => array ( "a" => "orange", + "b" => "banana", + "c" => "apple" + ), + "numbers" => array ( 1, + 2, + 3, + 4, + 5, + 6, + ), + "holes" => array ( "first", + 5 => "second", + "third" ) ); + +// Some examples to address values in the array above +echo $fruits["holes"][5]; // prints "second" +echo $fruits["fruits"]["a"]; // prints "orange" +unset($fruits["holes"][0]); // remove "first" + +// Create a new multi-dimensional array +$juices["apple"]["green"] = "good"; ]]> -<!-- quite duplicate... -$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" - ) -); ---> </programlisting> </example> - </sect2> - - <!-- TODO - <sect2> - <title>Misc</title> - - </sect2> - - - example multi-dim with $arr[bla][bla] syntax - - converting to array - - warning about references - - note that assigning is copy (usually...) - - - --> - -<!-- there is no such thing as multi/singel dim arrays (at least in PHP4) - <sect2 id="language.types.array.single-dim"> - <title>Single Dimension Arrays</title> - - <para> - PHP supports both scalar and associative arrays. In fact, there - is no difference between the two. You can create an array using - the - - <function>list</function> - - Nope - - - - - or <function>array</function> - functions, or you can explicitly set each array element value. - <informalexample> - <programlisting role="php"> -$a[0] = "abc"; -$a[1] = "def"; -$b["foo"] = 13; - </programlisting> - </informalexample> - </para> - <para> - You can also create an array by simply adding values to the - array. When you assign a value to an array variable using empty - brackets, the value will be added onto the end of the array. - <informalexample> - <programlisting role="php"> -$a[] = "hello"; // $a[2] == "hello" -$a[] = "world"; // $a[3] == "world" - </programlisting> - </informalexample> - </para> - <para> - Arrays may be sorted using the <function>asort</function>, - <function>arsort</function>, <function>ksort</function>, - <function>rsort</function>, <function>sort</function>, - <function>uasort</function>, <function>usort</function>, and - <function>uksort</function> functions depending on the type of - sort you want. - </para> - <para> - You can count the number of items in an array using the - <function>count</function> function. - </para> - <para> - You can traverse an array using <function>next</function> and - <function>prev</function> functions. Another common way to - traverse an array is to use the <function>each</function> - function. - </para> - </sect2> - - <sect2 id="language.types.array.multi-dim"> - <title>Multi-Dimensional Arrays</title> - - <para> - Multi-dimensional arrays are actually pretty simple. For each - dimension of the array, you add another [key] value to the end: - <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> - In PHP 3 it is not possible to reference multidimensional arrays - directly within strings. For instance, the following will not - have the desired result: - <informalexample> - <programlisting role="php"> -$a[3]['bar'] = 'Bob'; -echo "This won't work: $a[3][bar]"; - </programlisting> - </informalexample> - In PHP 3, the above will output <computeroutput>This won't work: - Array[bar]</computeroutput>. The string concatenation operator, - however, can be used to overcome this: - <informalexample> - <programlisting role="php"> -$a[3]['bar'] = 'Bob'; -echo "This will work: " . $a[3]['bar']; - </programlisting> - </informalexample> - </para> <para> - In PHP 4, however, the whole problem may be circumvented by - enclosing the array reference (inside the string) in curly - braces: + ÄúÐèҪעÒâÊý×éµÄ¸³Öµ×ÜÊÇ»áÉæ¼°µ½ÖµµÄ¿½±´¡£ÄúÐèÒªÔÚ¸´ÖÆÊý×éʱÓÃÖ¸Ïò·ûºÅ£¨&£©¡£ <informalexample> <programlisting role="php"> -$a[3]['bar'] = 'Bob'; -echo "This will work: {$a[3][bar]}"; - </programlisting> - </informalexample> - </para> - <para> - You can "fill up" multi-dimensional arrays in many ways, but the - trickiest one to understand is how to use the - <function>array</function> command for associative arrays. These - two snippets of code fill up the one-dimensional array in the - same way: - <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> - The <function>array</function> function can be nested for - multi-dimensional arrays: - <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" -?> +<![CDATA[ +$arr1 = array(2, 3); +$arr2 = $arr1; +$arr2[] = 4; // $arr2 is changed, + // $arr1 is still array(2,3) + +$arr3 = &$arr1; +$arr3[] = 4; // now $arr1 and $arr3 are the same +]]> </programlisting> </informalexample> </para> - </sect2> - - --> </sect1> <sect1 id="language.types.object"> @@ -1461,7 +1424,19 @@ <simpara> ÍêÕûµÄÌÖÂÛ¼û<link linkend="language.oop">ÀàÓë¶ÔÏó</link>Ò»Õ¡£ </simpara> - + </sect2> + <sect2 id="language.types.object.casting"> + <title>ת»»Îª¶ÔÏó</title> + + <para> + Èç¹û½«Ò»¸ö¶ÔÏóת»»³É¶ÔÏó£¬Ëü½«²»»áÓÐÈκα仯¡£Èç¹ûÆäËüÈκÎÀàÐ͵ÄÖµ±»×ª»»³É¶ÔÏó£¬ÄÚÖÃÀà <literal>stdClass</literal> µÄÒ»¸öʵÀý½«±»½¨Á¢¡£Èç¹û¸ÃֵΪ &null;£¬ÔòеÄʵÀýΪ¿Õ¡£¶ÔÓÚÈÎºÎÆäËüµÄÖµ£¬ÃûΪ <literal>scalar</literal> µÄ³ÉÔ±±äÁ¿½«°üº¬¸ÃÖµ¡£ + <informalexample> + <programlisting role="php"> +$obj = (object) 'ciao'; +echo $obj->scalar; // outputs 'ciao' + </programlisting> + </informalexample> + </para> </sect2> </sect1> @@ -1478,6 +1453,14 @@ </simpara> </note> + <sect2 id="language.types.resource.casting"> + <title>ת»»Îª×ÊÔ´</title> + + <para> + ÓÉÓÚ×ÊÔ´ÀàÐͱäÁ¿±£´æÓÐΪ´ò¿ªÎļþ¡¢Êý¾Ý¿âÁ¬½Ó¡¢Í¼Ðλ²¼ÇøÓòµÈµÄÌØÊâ¾ä±ú£¬ÄúÎÞ·¨½«ÆäËüÀàÐ͵Äֵת»»Îª×ÊÔ´¡£ + </para> + </sect2> + <sect2 id="language.types.resource.self-destruct"> <title>ÊÍ·Å×ÊÔ´</title> @@ -1542,6 +1525,87 @@ </sect2> </sect1> + + <sect1 id="language.pseudo-types"> + <title>±¾ÎĵµÖÐʹÓõÄαÀàÐÍ</title> + + <sect2 id="language.types.mixed"> + <title>mixed</title> + <para> + <literal>mixed</literal> ˵Ã÷Ò»¸ö²ÎÊý¿ÉÒÔ½ÓÊܶàÖÖ²»Í¬µÄ£¨µ«²¢²»±ØÐëÊÇËùÓеģ©ÀàÐÍ¡£ + </para> + <para> + <function>gettype</function> ±íÃ÷¿ÉÒÔ½ÓÊÜËùÓÐµÄ PHP ÀàÐÍ£¬ÀýÈç <function>str_replace</function> ½«½ÓÊÜ×Ö·û´®ºÍÊý×é¡£ + </para> + </sect2> + + <sect2 id="language.types.number"> + <title>number</title> + <para> + <literal>number</literal> ˵Ã÷Ò»¸ö²ÎÊý¿ÉÒÔÊÇ <type>integer</type> »òÕß <type>float</type>¡£ + </para> + </sect2> + + <sect2 id="language.types.callback"> + <title>callback</title> + <para> + ÓÐЩÖîÈç <function>call_user_function</function> »ò <function>usort</function> µÄº¯Êý½ÓÊÜÓû§×Ô¶¨ÒåµÄº¯Êý×÷Ϊһ¸ö²ÎÊý¡£Callback º¯Êý²»½ö¿ÉÒÔÊÇÒ»¸ö¼òµ¥µÄº¯Êý£¬Ëü»¹¿ÉÒÔÊÇÒ»¸ö¶ÔÏóµÄ·½·¨£¬°üÀ¨¾²Ì¬ÀàµÄ·½·¨¡£ + </para> + <para> + Ò»¸ö PHP º¯ÊýÓú¯ÊýÃû×Ö·û´®À´´«µÝ¡£Äú¿ÉÒÔ´«µÝÈκÎÄÚ½¨µÄ»òÕßÓû§×Ô¶¨ÒåµÄº¯Êý£¬³ýÁË + <function>array</function>£¬ + <function>echo</function>£¬ + <function>empty</function>£¬ + <function>eval</function>£¬ + <function>exit</function>£¬ + <function>isset</function>£¬ + <function>list</function>£¬ + <function>print</function> ºÍ + <function>unset</function>¡£ + </para> + <para> + Ò»¸ö¶ÔÏóµÄ·½·¨ÒÔÊý×éµÄÐÎʽÀ´´«µÝ£¬Êý×éµÄ 0 ϱêÖ¸Ã÷¶ÔÏóÃû£¬Ï±ê 1 Ö¸Ã÷·½·¨Ãû¡£ + </para> + <para> + ¶ÔÓÚûÓÐʵÀý»¯Îª¶ÔÏóµÄ¾²Ì¬À࣬Ҫ´«µÝÆä·½·¨£¬½«Êý×é 0 ϱêÖ¸Ã÷µÄ¶ÔÏóÃû»»³É¸ÃÀàµÄÃû³Æ¼´¿É¡£ + </para> + + <para> + <example> + <title> + Callback º¯ÊýʵÀý + </title> + <programlisting role="php"> +<![CDATA[ +<?php + +// simple callback example +function foobar() { + echo "hello world!"; +} +call_user_function("foobar"); + +// method callback examples +class foo { + function bar() { + echo "hello world!"; + } +} + +$foo = new foo; + +call_user_function(array($foo, "bar")); // object method call + +call_user_function(array("foo", "bar")); // static class method call + +?> +]]> + </programlisting> + </example> + </para> + + </sect2> + </sect1> <sect1 id="language.types.type-juggling"> <title>ÀàÐÍÏ··¨</title> @@ -1585,7 +1649,7 @@ </informalexample> </para> <simpara> - Èç¹ûÉÏÃæÁ½¸öÀý×Ó¿´ÉÏÈ¥¹Å¹ÖµÄ»°£¬²Î¼û<link linkend="language.types.string.conversion">×Ö·û´®×ª»»</link>¡£ + Èç¹ûÉÏÃæÁ½¸öÀý×Ó¿´ÉÏÈ¥¹Å¹ÖµÄ»°£¬²Î¼û<link linkend="language.types.string.conversion">×Ö·û´®×ª»»ÎªÊýÖµ</link>¡£ </simpara> <simpara> Èç¹ûÄãÒªÇ¿ÖÆ½«Ò»¸ö±äÁ¿µ±×÷ijÖÖÀàÐÍÀ´ÇóÖµ£¬²Î¼û<link @@ -1607,14 +1671,18 @@ </informalexample> </para> <para> - ÓÉÓÚ PHP Ö§³Öͨ¹ýÆ«ÒÆÁ¿½øÐеÄ×Ö·û´®Ë÷Òý£¬ÕâºÍÊý×éË÷ÒýµÄÓï·¨Ò»Ñù¡£ÒÔÉϵÄÀý×ӾͲúÉúÁËÒ»¸öÎÊÌ⣺$a + ÓÉÓÚһЩÀúÊ·ÔÒò£¬PHP Ö§³Öͨ¹ýÆ«ÒÆÁ¿½øÐеÄ×Ö·û´®Ë÷Òý£¬ÕâºÍÊý×éË÷ÒýµÄÓï·¨Ò»Ñù¡£ÒÔÉϵÄÀý×ӾͲúÉúÁËÒ»¸öÎÊÌ⣺$a Ó¦¸Ã±ä³ÉÒ»¸öµÚÒ»¸öÔªËØÊÇ¡°f¡±µÄÊý×éÄØ£¬»¹ÊÇ¡°f¡±³ÉÁË×Ö·û´® $a µÄµÚÒ»¸ö×Ö·û£¿ </para> <para> - ÓÉÓÚ´ËÔÒò£¬ÔÚ PHP 3.0.12 ºÍ PHP 4.0b3-RC4 ÖÐÕâÖÖ×Ô¶¯×ª»»µÄ½á¹û±»µ±³É䶨Òå¡£½â¾ö·½°¸ÈÔÔÚÌÖÂÛÖ®ÖС£ - </para> - <para> - £¨ÒëÕß×¢£º£©PHP 4 ÖÐÉÏÃæµÄÒ»¸öÀý×Ó³ö´í£¬µÚ¶þ¸öÀý×ӵĽá¹û½«ÊÇ×Ö·û´® $a = 'f'¡£ + Ŀǰ°æ±¾µÄ PHP ½«ÒÔÉϵڶþ¸ö¸³ÖµÀí½â³É×Ö·û´®µÄÆ«ÒÆÁ¿±êʶ£¬¼´ $a ±ä³ÉÁË "f"£¬¾¡¹ÜÈç´Ë£¬ÕâÖÖ×Ô¶¯×ª»»µÄµØ½á¹ûÓ¦¸Ã±»ÈÏΪ䶨Òå¡£PHP 4 ÒýÈëÁËÐµĻ¨À¨ºÅÓï·¨À´·ÃÎÊ×Ö·û´®µÄ×Ö·û£¬ÇëʹÓøÃÓï·¨À´Ìæ´úÒÔÉϵIJÙ×÷£º + <informalexample> + <programlisting role="php"> +$a = "abc"; // $a Ϊһ¸ö×Ö·û´® +$a{1} = "f"; // $a ĿǰΪ "afc" + </programlisting> + </informalexample> + Çë²ÎÔÄÌâΪ¡°<link linkend="language.types.string.substr">ÓÃ×Ö·û·ÃÎÊ×Ö·û´®</link>¡±µÄÕ½ÚÒÔ»ñÈ¡¸ü¶àÐÅÏ¢¡£ </para> </note> @@ -1626,7 +1694,7 @@ <informalexample> <programlisting role="php"> $foo = 10; // $foo is an integer -$bar = (float) $foo; // $bar is a float +$bar = (boolean) $foo; // $bar is a boolean </programlisting> </informalexample> </para> @@ -1653,12 +1721,6 @@ </listitem> </itemizedlist> </para> - <note> - <simpara> - ³ýÁËÓÃÇ¿ÖÆ×ª»»½«±äÁ¿×ª»»³É×Ö·û´®Ö®Í⣬»¹¿ÉÒÔ½«±äÁ¿ÓÃË«ÒýºÅÒýÆðÀ´¡£ - <!-- TODO: example --> - </simpara> - </note> <para> ×¢ÒâÔÚÀ¨ºÅÄÚÔÊÐíÓпոñºÍÖÆ±í·û£¬ËùÒÔÏÂÃæÁ½¸öÀý×Ó¹¦ÄÜÏàͬ£º @@ -1669,6 +1731,24 @@ </programlisting> </informalexample> </para> + <note> + <para> + ΪÁ˽«Ò»¸ö±äÁ¿»¹ÔΪ×Ö·û´®£¬Äú»¹¿ÉÒÔ½«±äÁ¿·ÅÖÃÔÚË«ÒýºÅÖÖ¡£ + <informalexample> + <programlisting role="php"> +$foo = 10; // $foo is an integer +$str = "$foo"; // $str is a string +$fst = (string) $foo; // $fst is also a string + +// This prints out that "they are the same" +if ($fst === $str) { + echo "they are the same"; +} + </programlisting> + </informalexample> + </para> + </note> + <para> µ±ÔÚijЩÀàÐÍÖ®¼äÇ¿ÖÆ×ª»»Ê±È·ÇеػᷢÉúʲô¿ÉÄܲ»ÊǺÜÃ÷ÏÔ¡£¸ü¶àÐÅÏ¢¼ûÈçÏÂС½Ú£º @@ -1679,63 +1759,31 @@ <listitem> <simpara><link linkend="language.types.integer.casting">ת»»ÎªÕûÐÍ</link></simpara> </listitem> - <!-- don't exist yet <listitem> - <simpara><link linkend="language.types.float.casting">Converting to - float</link></simpara> + <simpara><link linkend="language.types.float.casting">ת»»Îª¸¡µãÐÍ</link></simpara> </listitem> <listitem> - <simpara><link linkend="language.types.string.casting">Converting to - string</link></simpara> + <simpara><link linkend="language.types.string.casting">ת»»Îª×Ö·û´®</link></simpara> </listitem> <listitem> - <simpara><link linkend="language.types.array.casting">Converting to - array</link></simpara> + <simpara><link linkend="language.types.array.casting">ת»»ÎªÊý×é</link></simpara> </listitem> <listitem> - <simpara><link linkend="language.types.object.casting">Converting to - object</link></simpara> + <simpara><link linkend="language.types.object.casting">ת»»Îª¶ÔÏó</link></simpara> </listitem> <listitem> - <simpara><link linkend="language.types.resource.casting">Converting to - resource</link></simpara> + <simpara><link linkend="language.types.resource.casting">ת»»Îª×ÊÔ´</link></simpara> </listitem> + <!-- don't exist yet <listitem> <simpara><link linkend="language.types.null.casting">Converting to &null;</link></simpara> </listitem> --> </itemizedlist> - - </para> - <para> - <!-- TODO: move to 'converting to string' --> - µ±Ç¿Öƽ«Êý×éת»»Îª×Ö·û´®Ê±£¬½á¹û½«ÊÇ <literal>Array</literal> Õâ¸ö´Ê¡£µ±Ç¿Öƽ«¶ÔÏóת»»Îª×Ö·û´®Ê±£¬½á¹û½«ÊÇ <literal>Object</literal> Õâ¸ö´Ê¡£ - </para> - <para> - µ±Ç¿Öƽ«Ò»¸ö±êÁ¿»ò×Ö·û´®×ª»»ÎªÊý×éʱ£¬±äÁ¿½«³ÉΪÊý×éµÄµÚÒ»¸öÔªËØ£º - <informalexample> - <programlisting role="php"> -$var = 'ciao'; -$arr = (array) $var; -echo $arr[0]; // outputs 'ciao' - </programlisting> - </informalexample> </para> - <para> - µ±Ç¿Öƽ«Ò»¸ö±êÁ¿»ò×Ö·û´®×ª»»Îª¶ÔÏóʱ£¬±äÁ¿»á³ÉΪ¶ÔÏóµÄÊôÐÔ£¬ÊôÐÔÃûΪ¡°scalar¡±£º - <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
Index: phpdoc-zh/language/types.xml diff -u phpdoc-zh/language/types.xml:1.5 phpdoc-zh/language/types.xml:1.6 --- phpdoc-zh/language/types.xml:1.5 Sun Sep 29 23:25:33 2002 +++ phpdoc-zh/language/types.xml Sat Dec 28 02:20:29 2002 @@ -1,7 +1,8 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.5 $ --> -<!-- $Author: dallas $ --> -<!-- EN-Revision: 1.88 Maintainer: dallas Status: ready --> +<!-- $Revision: 1.6 $ --> +<!-- $Author: gregory $ --> +<!-- EN-Revision: 1.98 Maintainer: dallas Status: ready --> +<!-- CREDITS: Gregory --> <chapter id="language.types"> <title>ÀàÐÍ</title> @@ -9,11 +10,11 @@ <title>½éÉÜ</title> <simpara> - PHP Ö§³Ö°ËÖÖÔʼÀàÐÍ¡£<!-- (all types are primitive in php) --> + PHP Ö§³Ö°ËÖÖÔʼÀàÐÍ¡£ </simpara> <para> - ËÄÖÖ±êÁ¿ÀàÐÍ£º <!-- (basic, can't be split into parts) --> + ËÄÖÖ±êÁ¿ÀàÐÍ£º <itemizedlist> @@ -78,26 +79,33 @@ </listitem> </itemizedlist> - </para> - - <note> - <simpara> - ±¾ÊÖ²áÖÐÄã¾³£»áÅöµ½ <literal>mixed</literal> ²ÎÊý¡£Õâ¸öαÀàÐͱíʾ¸Ã²ÎÊýÓжàÖÖ¿ÉÄܵÄÀàÐÍ¡£ - </simpara> - <!-- - - Just an idea, maybe useful for some func-defs? - (at least it is for the operator-defs) - - <simpara> - In parameter definitions you can also encounter the 'number' pseudo-type, - that indicates a parameter that is either <type>integer</type> or - <type>float</type>. - </simpara> - --> - </note> + ΪÁËÈ·±£´úÂëµÄÒ×¶ÁÐÔ£¬±¾Êֲỹ½éÉÜÁËһЩ<link linkend="language.pseudo-types">αÀàÐÍ</link>£º + <itemizedlist> + + <listitem> + <simpara> + <link linkend="language.types.mixed">»ìºÍ£¨mixed£©</link> + </simpara> + </listitem> + + <listitem> + <simpara> + <link linkend="language.types.number">Êý×Ö£¨number£©</link> + </simpara> + </listitem> + + <listitem> + <simpara> + <link linkend="language.types.mixed">»ØÀ¡£¨callback£©</link> + </simpara> + </listitem> + + </itemizedlist> + Äú¿ÉÄÜ»¹»á¶Áµ½Ò»Ð©¹ØÓÚ¡°Ë«¾«¶È£¨double£©¡±ÀàÐ͵IJο¼¡£Êµ¼ÊÉÏ double ºÍ float ÊÇÏàͬµÄ£¬ÓÉÓÚһЩÀúÊ·µÄÔÒò£¬ÕâÁ½¸öÃû³ÆÍ¬Ê±´æÔÚ¡£ + </para> + <simpara> ±äÁ¿µÄÀàÐÍͨ³£²»ÊÇÓɳÌÐòÔ±É趨µÄ£¬È·ÇеØËµ£¬ÊÇÓÉ PHP ¸ù¾Ý¸Ã±äÁ¿Ê¹ÓõÄÉÏÏÂÎÄÔÚÔËÐÐʱ¾ö¶¨µÄ¡£ </simpara> @@ -105,20 +113,40 @@ <simpara> Èç¹ûÄãÏë²é¿´Ä³¸ö<link linkend="language.expressions">±í´ïʽ</link>µÄÖµºÍÀàÐÍ£¬Óà <function>var_dump</function>¡£ </simpara> - <simpara> - Èç¹ûÄãÖ»ÊÇÏëµÃµ½Ò»¸öÒ×¶Á¶®µÄÀàÐ͵ıí´ï·½Ê½ÓÃÓÚµ÷ÊÔ£¬Óà <function>gettype</function>¡£Òª²é¿´Ä³¸öÀàÐÍ£¬<emphasis>²»Òª</emphasis>Óà <function>gettype</function>£¬¶øÓà <literal>is_<replaceable>type</replaceable></literal> º¯Êý¡£ - </simpara> - <!-- TODO: example(s) would be great --> + <para> + Èç¹ûÄãÖ»ÊÇÏëµÃµ½Ò»¸öÒ×¶Á¶®µÄÀàÐ͵ıí´ï·½Ê½ÓÃÓÚµ÷ÊÔ£¬Óà <function>gettype</function>¡£Òª²é¿´Ä³¸öÀàÐÍ£¬<emphasis>²»Òª</emphasis>Óà <function>gettype</function>£¬¶øÓà <literal>is_<replaceable>type</replaceable></literal> º¯Êý¡£ÒÔÏÂÊÇһЩ·¶Àý£º + <informalexample> + <programlisting role="php"> +<![CDATA[ +$bool = TRUE; // a boolean +$str = "foo"; // a string +$int = 12; // an integer + +echo gettype($bool); // prints out "boolean" +echo gettype($str); // prints out "string" + +// If this is an integer, increment it by four +if (is_int($int)) { + $int += 4; +} + +// If $bool is a string, print it out +// (does not print out anything) +if (is_string($bool)) { + echo "String: $bool"; +} +]]> + </programlisting> + </informalexample> + </para> </note> <simpara> Èç¹ûÄãÒª½«Ò»¸ö±äÁ¿Ç¿ÖÆ×ª»»ÎªÄ³ÀàÐÍ£¬¿ÉÒÔ¶ÔÆäʹÓÃ<link linkend="language.types.typecasting">Ç¿ÖÆ×ª»»</link>»òÕß <function>settype</function> º¯Êý¡£ </simpara> <simpara> - ×¢Òâ±äÁ¿¸ù¾ÝÆäµ±Ê±µÄÀàÐÍÔÚÌØ¶¨³¡ºÏÏ»á±íÏÖ³ö²»Í¬µÄ·ç¸ñ¡£¸ü¶àÐÅÏ¢¼û<link linkend="language.types.type-juggling">ÀàÐÍÏ··¨</link>¡£ + ×¢Òâ±äÁ¿¸ù¾ÝÆäµ±Ê±µÄÀàÐÍÔÚÌØ¶¨³¡ºÏÏ»á±íÏÖ³ö²»Í¬µÄÖµ¡£¸ü¶àÐÅÏ¢¼û<link linkend="language.types.type-juggling">ÀàÐÍÏ··¨</link>¡£ </simpara> - - </sect1> <sect1 id="language.types.boolean"> @@ -138,11 +166,10 @@ <title>Óï·¨</title> <para> ÒªÖ¸¶¨Ò»¸ö²¼¶ûÖµ£¬Ê¹Óùؼü×Ö &true; »ò &false;¡£Á½¸ö¶¼ÊÇ´óСд²»Ãô¸ÐµÄ¡£ - <!-- technically they are just constants --> <informalexample> <programlisting role="php"> <![CDATA[ -$foo = True; // assign the value TRUE to $foo +$foo = True; // assign the value TRUE to $foo ]]> </programlisting> </informalexample> @@ -153,17 +180,18 @@ <informalexample> <programlisting role="php"> <![CDATA[ -// == is an operator which returns a boolean +// == is an operator which test +// equality and returns a boolean if ($action == "show_version") { echo "The version is 1.23"; } -// this is not necessary: +// this is not necessary... if ($show_separators == TRUE) { echo "<hr>\n"; } -// because you can simply type this: +// ...because you can simply type if ($show_separators) { echo "<hr>\n"; } @@ -200,7 +228,7 @@ linkend="language.types.string">×Ö·û´®</link> "0"</simpara> </listitem> <listitem> - <simpara>ûÓе¥ÔªµÄ<link linkend="language.types.array">Êý×é</link></simpara> + <simpara>ûÓгÉÔ±±äÁ¿µÄ<link linkend="language.types.array">Êý×é</link></simpara> </listitem> <listitem> <simpara>ûÓе¥ÔªµÄ<link linkend="language.types.object">¶ÔÏó</link></simpara> @@ -216,8 +244,19 @@ <literal>-1</literal> ºÍÆäËü·ÇÁãÖµ£¨²»ÂÛÕý¸º£©Ò»Ñù£¬±»ÈÏΪÊÇ &true;£¡ </simpara> </warning> - <!-- TODO: add a few examples, for the people only looking at - the examples... --> + <informalexample> + <programlisting role="php"> +<![CDATA[ +echo gettype((bool) ""); // bool(false) +echo gettype((bool) 1); // bool(true) +echo gettype((bool) -2); // bool(true) +echo gettype((bool) "foo"); // bool(true) +echo gettype((bool) 2.3e5); // bool(true) +echo gettype((bool) array(12)); // bool(true) +echo gettype((bool) array()); // bool(false) +]]> + </programlisting> + </informalexample> </para> </sect2> @@ -253,20 +292,23 @@ ]]> </programlisting> </example> - <!-- + ÔÚ×ÖÃæÉÏ£¬ÕûÐͱäÁ¿ÕýʽµÄ½á¹¹¿ÉÒÔΪ£º + <informalexample> + <programlisting> +<![CDATA[ +decimal : [1-9][0-9]* + | 0 - decimal : [1-9][0-9]* - | 0 - - hexadecimal : 0[xX][0-9a-fA-F]+ - - octal : 0[0-7]+ - - integer : [+-]?decimal - | [+-]?hexadecimal - | [+-]?octal - - --> +hexadecimal : 0[xX][0-9a-fA-F]+ + +octal : 0[0-7]+ + +integer : [+-]?decimal + | [+-]?hexadecimal + | [+-]?octal +]]> + </programlisting> + </informalexample> ÕûÐÍÊýµÄ×Ö³¤ºÍƽ̨Óйأ¬¾¡¹Üͨ³£×î´óÖµÊÇ´óÔ¼¶þÊ®ÒÚ£¨32 λÓзûºÅ£©¡£PHP ²»Ö§³ÖÎÞ·ûºÅÕûÊý¡£ </para> </sect2> @@ -308,12 +350,13 @@ </warning> </para> <para> - PHP ÖÐûÓÐÕû³ýµÄÔËËã·û¡£<literal>1/2</literal> ²úÉú³ö <type>float</type> <literal>0.5</literal>¡£ + PHP ÖÐûÓÐÕû³ýµÄÔËËã·û¡£<literal>1/2</literal> ²úÉú³ö<type>¸¡µãÊý</type> <literal>0.5</literal>¡£Äú¿ÉÒÔ×ÜÊÇÉáÆúСÊý²¿·Ö£¬»òÕßʹÓà <function>round</function> º¯Êý¡£ <informalexample> <programlisting role="php"> <![CDATA[ -var_dump( 25/7 ); -// Êä³öΪ£ºfloat(3.5714285714286) +var_dump(25/7); // float(3.5714285714286) +var_dump((int) (25/7)); // int(3) +var_dump(round(25/7)); // float(4) ]]> </programlisting> </informalexample> @@ -324,7 +367,7 @@ <sect2 id="language.types.integer.casting"> <title>ת»»ÎªÕûÐÎ</title> <simpara> - ÒªÃ÷ʾµØ½«Ò»¸öֵת»»Îª <type>integer</type>£¬Óà <literal>(int)</literal> »ò <literal>(integer)</literal> Ç¿ÖÆ×ª»»¡£²»¹ý´ó¶àÊýÇé¿ö϶¼²»ÐèÒªÇ¿ÖÆ×ª»»£¬ÒòΪµ±ÔËËã·û£¬º¯Êý»òÁ÷³Ì¿ØÖÆÐèÒªÒ»¸ö <type>integer</type> ²ÎÊýʱ£¬Öµ»á×Ô¶¯×ª»»¡£ + ÒªÃ÷ʾµØ½«Ò»¸öֵת»»Îª <type>integer</type>£¬Óà <literal>(int)</literal> »ò <literal>(integer)</literal> Ç¿ÖÆ×ª»»¡£²»¹ý´ó¶àÊýÇé¿ö϶¼²»ÐèÒªÇ¿ÖÆ×ª»»£¬ÒòΪµ±ÔËËã·û£¬º¯Êý»òÁ÷³Ì¿ØÖÆÐèÒªÒ»¸ö <type>integer</type> ²ÎÊýʱ£¬Öµ»á×Ô¶¯×ª»»¡£Äú»¹¿ÉÒÔͨ¹ýº¯Êý <function>intval</function> À´½«Ò»¸öֵת»»³ÉÕûÐÍ¡£ </simpara> <simpara> ²Î¼û<link linkend="language.types.type-juggling">ÀàÐÍÏ··¨</link>¡£ @@ -356,7 +399,7 @@ <informalexample> <programlisting role="php"> <![CDATA[ -echo (int) ( (0.1+0.7) * 10 ); // echoes 7! +echo (int) ( (0.1+0.7) * 10 ); // ÏÔʾ 7£¡ ]]> </programlisting> </informalexample> @@ -367,7 +410,7 @@ <sect3 id="language.types.integer.casting.from-string"> <title>´Ó×Ö·û´®×ª»»</title> <simpara> - ²Î¼û<link linkend="language.types.string.conversion">×Ö·û´®×ª»»</link>¡£ + ²Î¼û<link linkend="language.types.string.conversion">×Ö·û´®×ª»»ÎªÊý×Ö</link>¡£ </simpara> </sect3> @@ -381,16 +424,7 @@ </simpara> </caution> </para> - <!-- - - IMO, it would more sense as (int) $arr returned the - number of elements in $arr. This won't break anything, - since this behaviour was never defined before, and - (bool)(int) $arr will still behave the same. - - --> - </sect3> - + </sect3> </sect2> </sect1> @@ -398,18 +432,22 @@ <title>¸¡µãÐÍ</title> <para> ¸¡µãÊý£¨Ò²½Ð¡°floats¡±£¬¡°doubles¡±»ò¡°real numbers¡±£©¿ÉÒÔÓÃÒÔÏÂÈκÎÓï·¨¶¨Ò壺 - <synopsis> + <informalexample> + <programlisting role="php"> $a = 1.234; $a = 1.2e3; $a = 7E-10; - </synopsis> - <!-- - -LNUM [0-9]+ -DNUM ([0-9]*[\.][0-9]+)|([0-9]+[\.][0-9]*) -EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) - - --> + </programlisting> + </informalexample> + ÐÎʽÉÏ£º + <informalexample> + <programlisting role="php"> +LNUM [0-9]+ +DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*) +EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM}) + </programlisting> + </informalexample> ¸¡µãÊýµÄ×Ö³¤ºÍƽ̨Ïà¹Ø£¬¾¡¹Üͨ³£×î´óÖµÊÇ 1.8e308 ²¢¾ßÓÐ 14 λʮ½øÖÆÊý×ֵľ«¶È£¨64 λ IEEE ¸ñʽ£©¡£ </para> + <warning id="warn.float-precision"> <title>¸¡µãÊý¾«¶È</title> <para> @@ -423,15 +461,21 @@ linkend="ref.bc">ÈÎÒ⾫¶ÈÊýѧº¯Êý¿â</link>»òÕß <link linkend="ref.gmp">gmp º¯Êý¿â</link>¡£ </para> </warning> + + <sect2 id="language.types.float.casting"> + <title>ת»»Îª¸¡µãÊý</title> + + <para> + Èç¹ûÄúÏ£ÍûÁ˽âÓйغÎʱºÍÈçºÎ½«×Ö·û´®×ª»»³É¸¡µãÊýµÄÐÅÏ¢£¬Çë²éÔıêÌâΪ¡°<link linkend="language.types.string.conversion">½«×Ö·û´®×ª»»ÎªÊý×Ö</link>¡±µÄÓйØÕ½ڡ£¶ÔÓÚÆäËüÀàÐ͵ÄÖµ£¬ÆäÇé¿öÀàËÆÓÚÏȽ«Öµ×ª»»³ÉÕûÐÍ£¬È»ºóÔÙת»»³É¸¡µã¡£Çë²ÎÔÄ¡°<link linkend="language.types.integer.casting">ת»»ÎªÕûÐÍ</link>¡±ÓйØÕ½ÚÒÔ»ñÈ¡¸ü¶àÐÅÏ¢¡£ + </para> + </sect2> </sect1> <sect1 id="language.types.string"> <title>×Ö·û´®</title> <para> - <type>string</type> ÊÇһϵÁÐ×Ö·û¡£ÔÚ PHP ÖУ¬×Ö·ûºÍ×Ö½ÚÒ»Ñù£¬Ò²¾ÍÊÇ˵£¬Ò»¹²ÓÐ 256 ÖÖ²»Í¬×Ö·ûµÄ¿ÉÄÜÐÔ¡£ÕâÒ²°µÊ¾ PHP ¶Ô Unicode ûÓб¾µØÖ§³Ö¡£ - <!-- how about unicode? will we support that eventually? Are - there current any ways to work with unicode? - --> + <type>string</type> ÊÇһϵÁÐ×Ö·û¡£ÔÚ PHP ÖУ¬×Ö·ûºÍ×Ö½ÚÒ»Ñù£¬Ò²¾ÍÊÇ˵£¬Ò»¹²ÓÐ 256 ÖÖ²»Í¬×Ö·ûµÄ¿ÉÄÜÐÔ¡£ÕâÒ²°µÊ¾ PHP ¶Ô Unicode ûÓб¾µØÖ§³Ö¡£Çë²ÎÔĺ¯Êý <function>utf8_enncode</function> + ºÍ <function>utf8_decode</function> ÒÔÁ˽âÓÐ¹Ø Unicode Ö§³Ö¡£ </para> <note> <simpara> @@ -483,8 +527,8 @@ <programlisting role="php"> <![CDATA[ echo 'this is a simple string'; -echo 'You can also have embedded newlines in strings, -like this way.'; +echo 'You can also have embedded newlines in strings +this way'; echo 'Arnold once said: "I\'ll be back"'; // output: ... "I'll be back" echo 'Are you sure you want to delete C:\\*.*?'; @@ -620,8 +664,8 @@ ¶¨½ç·ûÖ§³ÖÊÇ PHP 4 ÖмÓÈëµÄ¡£ </para> </note> - </sect3> + <sect3 id="language.types.string.parsing"> <title>±äÁ¿½âÎö</title> <simpara> @@ -633,8 +677,8 @@ </simpara> <simpara> ¸´ÔÓÓï·¨ÊÇ PHP 4 Òý½øµÄ£¬¿ÉÒÔÓû¨À¨ºÅÀ¨ÆðÒ»¸ö±í´ïʽ¡£ - <!-- XXX was it? and starting with what version exactly? --> </simpara> + <sect4 id="language.types.string.parsing.simple"> <title>¼òµ¥Óï·¨</title> <simpara> @@ -645,7 +689,7 @@ <![CDATA[ $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 $beers"; // won't work, 's' is a valid character for varnames echo "He drank some ${beer}s"; // works ]]> </programlisting> @@ -653,17 +697,14 @@ <simpara> ͬÑùÒ²¿ÉÒÔ½âÎöÊý×éË÷Òý»òÕß¶ÔÏóÊôÐÔ¡£¶ÔÓÚÊý×éË÷Òý£¬ÓÒ·½À¨ºÅ£¨<literal>]</literal>£©±êÖ¾×ÅË÷ÒýµÄ½áÊø¡£¶ÔÏóÊôÐÔÔòºÍ¼òµ¥±äÁ¿ÊÊÓÃͬÑùµÄ¹æÔò£¬¾¡¹Ü¶ÔÓÚ¶ÔÏóÊôÐÔûÓÐÏñ±äÁ¿ÄÇÑùµÄС¼¼ÇÉ¡£ <!-- XXX isn't &true; :(, this would be the trick - Also, - the same trick with curly-braces works if you - want to limit the greediness of parsers (aren't they - paying them enough or something?). + Also, the same trick with curly-braces works if you + want to limit the greediness of parsers. --> - </simpara> <informalexample> <programlisting role="php"> <![CDATA[ -$fruits = array( 'strawberry' => 'red' , 'banana' => 'yellow' ); +$fruits = array('strawberry' => 'red', 'banana' => 'yellow'); // note that this works differently outside string-quotes. echo "A banana is $fruits[banana]."; @@ -686,6 +727,7 @@ ¶ÔÓÚÈκθü¸´ÔÓµÄÇé¿ö£¬Ó¦¸ÃʹÓø´ÔÓÓï·¨¡£ </simpara> </sect4> + <sect4 id="language.types.string.parsing.complex"> <title>¸´ÔÓ£¨»¨À¨ºÅ£©Óï·¨</title> <simpara> @@ -701,7 +743,7 @@ echo "This is { $great}"; // ²»ÐУ¬Êä³öΪ£ºThis is { fantastic} echo "This is {$great}"; // ¿ÉÒÔ£¬Êä³öΪ£ºThis is fantastic echo "This square is {$square->width}00 centimeters broad."; -echo "This works: {$arr[4][3]}"; +echo "This works: {$arr[4][3]}"; // This is wrong for the same reason // as $foo[bar] is wrong outside a string. @@ -736,35 +778,15 @@ <example> <title>һЩ×Ö·û´®Àý×Ó</title> <programlisting role="php"> -<!-- TODO: either move these examples to a example section, -as with arrays, or distribute them under the applicable -sections. --> <![CDATA[ <?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 */ +// Get the first character of a string $str = 'This is a test.'; $first = $str{0}; -/* Get the last character of a string. */ +// Get the last character of a string. $str = 'This is still a test.'; -$last = $str{strlen($str)-1}; +$last = $str{strlen($str)-1}; ?> ]]> </programlisting> @@ -775,7 +797,7 @@ </sect2><!-- end syntax --> <sect2 id="language.types.string.useful-funcs"> - <title>ʵÓú¯Êý</title><!-- and operators --> + <title>ʵÓú¯Êý¼°²Ù×÷·û</title> <para> ×Ö·û´®¿ÉÒÔÓá°.¡±£¨µã£©ÔËËã·ûÁ¬½Ó¡£×¢ÒâÕâÀï²»ÄÜÓá°+¡±£¨¼Ó£©ÔËËã·û¡£¸ü¶àÐÅÏ¢²Î¼û<link linkend="language.operators.string">×Ö·û´®ÔËËã·û</link>¡£ @@ -795,8 +817,44 @@ ×îºó£¬Èç¹û»¹ÊÇÕÒ²»µ½ÄãÒªµÄº¯Êý£¬²Î¼û<link linkend="ref.ctype">×Ö·ûÀàÐͺ¯Êý¿â</link>¡£ </simpara> </sect2> - <sect2 id="language.types.string.conversion"> + + <sect2 id="language.types.string.casting"> <title>×Ö·û´®×ª»»</title> + + <para> + Äú¿ÉÒÔÓà <literal>(string)</literal> ±ê¼Ç»òÕß <function>strval</function> º¯Êý½«Ò»¸öֵת»»Îª×Ö·û´®¡£µ±Ä³±í´ïʽÐèÒª×Ö·û´®Ê±£¬×Ö·û´®µÄת»»»áÔÚ±í´ïʽ·¶Î§ÄÚ×Ô¶¯Íê³É¡£ÀýÈçµ±ÄãʹÓà <function>echo</function> »òÕß <function>print</function> º¯Êýʱ£¬»òÕß½«Ò»¸ö±äÁ¿ÖµÓëÒ»¸ö×Ö·û´®½øÐбȽϵÄʱºò¡£ + </para> + + <para> + ²¼¶ûÖµ &true; ½«±»×ª»»Îª×Ö·û´® <literal>"1"</literal>£¬¶øÖµ &false; ½«±»±íʾΪ <literal>""</literal>£¨¼´¿Õ×Ö·û´®£©¡£ÕâÑùÄú¾Í¿ÉÒÔËæÒâµØÔÚ²¼¶ûÖµºÍ×Ö·û´®Ö®¼ä½øÐбȽϡ£ + </para> + <para> + ÕûÊý»ò¸¡µãÊýÊýÖµÔÚת»»³É×Ö·û´®Ê±£¬×Ö·û´®¼´Îª±íʾÕâЩÊýÖµÊý×Ö£¨¸¡µãÊý»¹°üº¬ÓÐÖ¸Êý²¿·Ö£©¡£ + </para> + <para> + Êý×齫±»×ª»»³É×Ö·û´® <literal>"Array"</literal>£¬Òò´ËÄúÎÞ·¨Í¨¹ý <function>echo</function> »òÕß <function>print</function> º¯ÊýÀ´Êä³öÊý×éµÄÄÚÈÝ¡£Çë²Î¿¼ÏÂÎÄÒÔ»ñÈ¡¸ü¶àÌáʾ¡£ + </para> + <para> + ¶ÔÏ󽫱»×ª»»³É×Ö·û´® <literal>"Object"</literal>¡£Èç¹ûÄúÒòΪµ÷ÊÔÐèÒª£¬ÐèÒª½«¶ÔÏóµÄ³ÉÔ±±äÁ¿´òÓ¡³öÀ´£¬ÇëÔĶÁÏÂÎÄ¡£Èç¹ûÄúÏ£ÍûµÃµ½¸Ã¶ÔÏóËùÒÀ¸½µÄÀàµÄÃû³Æ£¬ÇëʹÓú¯Êý <function>get_class</function>¡£ + </para> + <para> + ×ÊÔ´ÀàÐͽ«»áÒÔ <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>£¬¶ÔÓÚµ÷ÊÔÀ´Ëµ£¬ÕâЩÊǸüºÃµÄ´òÓ¡ÖµµÄ·½·¨¡£ + </para> + + <para> + Äú¿ÉÒÔ½« PHP µÄֵת»»Îª×Ö·û´®ÒÔÓÀ¾ÃµØ´¢´æËüÃÇ¡£ÕâÖÖ·½·¨±»³ÆÎª´®Ðл¯£¬Äú¿ÉÒÔÓú¯Êý <function>serialize</function> À´Íê³É¸Ã²Ù×÷¡£Èç¹ûÄúÔÚ°²×° PHP ʱ½¨Á¢ÁË <link linkend="ref.wddx">WDDX</link> Ö§³Ö£¬Äú»¹¿ÉÒÔ½« PHP µÄÖµ´®Ðл¯Îª XML ½á¹¹¡£ + </para> + </sect2> + + <sect2 id="language.types.string.conversion"> + <title>×Ö·û´®×ª»»ÎªÊýÖµ</title> <simpara> µ±Ò»¸ö×Ö·û´®±»µ±×÷Êý×ÖÀ´Çóֵʱ£¬¸ù¾ÝÒÔϹæÔòÀ´¾ö¶¨½á¹ûµÄÀàÐͺÍÖµ¡£ @@ -835,6 +893,10 @@ </informalexample> </para> + <para> + ²»ÒªÖ¸ÍûÔÚ½«Ò»¸ö×Ö·ûת»»³ÉÕûÐÍʱÄܹ»µÃµ½¸Ã×Ö·ûµÄ±àÂ루Äú¿ÉÄÜÒ²»áÔÚ C ÖÐÕâô×ö£©¡£Èç¹ûÄúÏ£ÍûÔÚ×Ö·û±àÂëºÍ×Ö·ûÖ®¼äת»»£¬ÇëʹÓà <function>ord</function> ºÍ <function>chr</function> º¯Êý¡£ + </para> + </sect2> </sect1><!-- end string --> @@ -846,8 +908,6 @@ </para> <para> ½âÊÍÕâЩ½á¹¹³¬³öÁ˱¾ÊÖ²áµÄ·¶Î§£¬µ«¶ÔÓÚÿÖֽṹÄãÖÁÉٻᷢÏÖÒ»¸öÀý×Ó¡£ÒªµÃµ½ÕâЩ½á¹¹µÄ¸ü¶àÐÅÏ¢£¬ÎÒÃǽ¨ÒéÄã²Î¿¼Óйش˹ãÀ«Ö÷ÌâµÄÍâ²¿Öø×÷¡£ - <!-- like goodrich&tamassia: datastructures and algorithmes. - Only, the subtitle is: in Java, and it's quite academic too --> </para> <sect2 id="language.types.array.syntax"> @@ -860,38 +920,63 @@ <literal><replaceable>key</replaceable> => <replaceable>value</replaceable></literal> ²ÎÊý¶Ô¡£ </para> <para> + <synopsis> +array( <optional> <replaceable>key</replaceable> => </optional> <replaceable +>value</replaceable> + , ... + ) +// <replaceable>key</replaceable> ²»ÊÇ<type>×Ö·û´®</type>¾ÍÊÇ<type>ÕûÊý</type> +// <replaceable>value</replaceable> ¿ÉÒÔΪÈκÎÖµ + </synopsis> + </para> + <para> + <informalexample> + <programlisting role="php"> +<![CDATA[ +array("foo" => "bar", 12 => true); +]]> + </programlisting> + </informalexample> + </para> + <para> <varname>key</varname> ¿ÉÒÔÊÇ <type>integer</type> »òÕß <type>string</type>¡£Èç¹û¼üÃûÊÇÒ»¸ö <type>integer</type> µÄ±ê×¼±í´ï·½·¨£¬Ôò±»½âÊÍΪÕûÊý£¨ÀýÈç <literal>"8"</literal> ½«±»½âÊÍΪ - <literal>8</literal>£¬¶ø <literal>"08"</literal> ½«±»½âÊÍΪ <literal>"08"</literal>£©¡£ + <literal>8</literal>£¬¶ø <literal>"08"</literal> ½«±»½âÊÍΪ <literal>"08"</literal>£©¡£PHP ÖÐÊý×éϱêµÄ±äÁ¿ÀàÐͲ»»á¶ÔÊý×éÔì³ÉÓ°Ï죬Êý×éµÄÀàÐÍÖ»ÓÐÒ»ÖÖ£¬Ëü¿ÉÒÔͬʱ°üº¬ÕûÐͺÍ×Ö·û´®Ð͵Äϱꡣ </para> <para> Öµ¿ÉÒÔÊÇÈκÎÖµ¡£ + <informalexample> + <programlisting role="php"> +<![CDATA[ +array("somearray" => array(6 => 5, 13 => 9, "a" => 43)); +]]> + </programlisting> + </informalexample> </para> <para> Èç¹ûÊ¡ÂÔ¼üÃû£¬ÔòÈ¡µ±Ç°×î´óµÄÕûÊýË÷Òý£¬¶øÐµļüÃû½«ÊǸÃÖµ¼ÓÒ»¡£ÕûÊý¿ÉÒÔΪ¸º£¬ËùÒÔ¶ÔÓÚ¸ºµÄË÷ÒýÒ²ÊÇÕâÑù¡£ÀýÈç×î´óµÄË÷ÒýÊÇ <literal>-6</literal> ½«µ¼ÖÂеļüÃûÊÇ <literal>-5</literal>¡£Èç¹û»¹²»´æÔÚÕûÊýË÷Òý£¬Ôò¼üÃû½«Îª <literal>0</literal>£¨Á㣩¡£Èç¹ûÄãÖ¸¶¨µÄ¼üÃûÒѾÓÐÁËÖµ£¬Ôò¸ÃÖµ»á±»¸²¸Ç¡£ + <informalexample> + <programlisting role="php"> +<![CDATA[ +// 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> </para> <para> - ʹÓà <literal>true</literal> ×÷Ϊ¼üÃû½«Ê¹ <type>integer</type> <literal>1</literal> ³ÉΪ¼üÃû¡£Ê¹Óà - <literal>false</literal> ×÷Ϊ¼üÃû½«Ê¹ <type>integer</type> <literal>0</literal> ³ÉΪ¼üÃû¡£Ê¹Óà + ʹÓà &true; ×÷Ϊ¼üÃû½«Ê¹ <type>integer</type> <literal>1</literal> ³ÉΪ¼üÃû¡£Ê¹Óà + &false; ×÷Ϊ¼üÃû½«Ê¹ <type>integer</type> <literal>0</literal> ³ÉΪ¼üÃû¡£Ê¹Óà <literal>NULL</literal> ×÷Ϊ¼üÃû½«µÈͬÓÚʹÓÿÕ×Ö·û´®¡£Ê¹ÓÿÕ×Ö·û´®×÷Ϊ¼üÃû½«Ð½¨£¨»ò¸²¸Ç£©Ò»¸öÓÿÕ×Ö·û´®×÷Ϊ¼üÃûµÄÖµ£¬ÕâºÍÓÿյķ½À¨ºÅ²»Ò»Ñù¡£ </para> <para> ²»ÄÜÓÃÊý×éºÍ¶ÔÏó×÷Ϊ¼üÃû¡£ÕâÑù×ö»áµ¼ÖÂÒ»¸ö¾¯¸æ£º<literal>Illegal offset type</literal>¡£ - </para> - - <para> - <synopsis> -array( <optional> <replaceable>key</replaceable> => </optional> <replaceable ->value</replaceable> - , ... - ) -// <replaceable>key</replaceable> is either <type>string</type - > or nonnegative <type>integer</type> -// <replaceable>value</replaceable> can be anything - </synopsis> - </para> + </para> </sect3> <sect3 id="language.types.array.syntax.modifying"> @@ -904,15 +989,29 @@ <synopsis> $arr[<replaceable>key</replaceable>] = <replaceable>value</replaceable>; $arr[] = <replaceable>value</replaceable>; -// <replaceable>key</replaceable> is either <type>string</type> or nonnegative <type>integer</type> -// <replaceable>value</replaceable> can be anything +// <replaceable>key</replaceable> ²»ÊÇ <type>string</type> ¾ÍÊǷǸºµÄ <type>integer</type> +// <replaceable>value</replaceable> ¿ÉÒÔΪÈκÎÖµ¡£ </synopsis> Èç¹û <varname>$arr</varname> »¹²»´æÔÚ£¬½«»áн¨Ò»¸ö¡£ÕâÒ²ÊÇÒ»ÖÖ¶¨ÒåÊý×éµÄÌæ»»·½·¨¡£Òª¸Ä±äÒ»¸öÖµ£¬Ö»Òª¸øËü¸³Ò»¸öÐÂÖµ¡£Èç¹ûҪɾ³ýÒ»¸ö¼üÃû£¯Öµ¶Ô£¬Òª¶ÔËüÓà <function>unset</function>¡£ - </para> + <informalexample> + <programlisting role="php"> +<![CDATA[ +$arr = array(5 => 1, 12 => 2); - </sect3> - +$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> + </sect3> </sect2><!-- end syntax --> <sect2 id="language.types.array.useful-funcs"> @@ -942,7 +1041,6 @@ <para> <link linkend="control-structures.foreach">foreach</link> ¿ØÖƽṹÊÇרÃÅÓÃÓÚÊý×éµÄ¡£ËüÌṩÁËÒ»¸ö¼òµ¥µÄ·½·¨À´±éÀúÊý×é¡£ </para> - </sect2> <sect2 id="language.types.array.donts"> @@ -976,9 +1074,9 @@ <informalexample> <programlisting role="php"> <![CDATA[ -$error_descriptions[E_ERROR] = "A fatal error has occured"; +$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"; +$error_descriptions[E_NOTICE] = "This is just an informal notice"; ]]> </programlisting> </informalexample> @@ -1002,15 +1100,44 @@ <sect4> <title>ÄÇôΪʲôÕâÑù×ö²»ºÃ£¿</title> <para> - ÔÚδÀ´µÄijһʱ¿Ì£¬PHP ¿ª·¢Ð¡×é¿ÉÄÜ»áÏëÐÂÔöÒ»¸ö³£Á¿»òÕ߹ؼü×Ö£¬ÄÇÄã¾ÍÓÐÂé·³ÁË¡£ÀýÈçÄãÒѾ²»ÄÜÕâÑùÓà + ÔÚδÀ´µÄijһʱ¿Ì£¬PHP ¿ª·¢Ð¡×é¿ÉÄÜ»áÏëÐÂÔöÒ»¸ö³£Á¿»òÕ߹ؼü×Ö£¬»òÕßÄú¿ÉÄÜÏ£ÍûÔÚÒÔºóÔÚÄúµÄ³ÌÐòÖÐÒýÈëеij£Á¿£¬ÄÇÄã¾ÍÓÐÂé·³ÁË¡£ÀýÈçÄãÒѾ²»ÄÜÕâÑùÓà <literal>empty</literal> ºÍ <literal>default</literal> ÕâÁ½¸ö´ÊÁË£¬ÒòΪËûÃÇÊÇ<link linkend="reserved">±£Áô×Ö</link>¡£ </para> <note> - <simpara> + <para> µ±Äã°Ñ <link linkend="function.error-reporting">error_reporting</link> ÉèΪ <literal>E_ALL</literal> ʱ£¬Ä㽫»á¿´µ½ÎÞÂÛºÎʱʹÓÃÁË䶨ÒåµÄ - <literal>index</literal> ʱ£¬PHP ¶¼»á²úÉú֪ͨ£¨ÔÚÄãµÄ½Å±¾ÖмÓÈëÒ»ÐÐ <literal>error_reporting(E_ALL);</literal>£©¡£ - </simpara> + <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> @@ -1021,6 +1148,22 @@ </sect3> </sect2> + <sect2 id="language.types.array.casting"> + <title>ת»»ÎªÊý×é</title> + + <para> + ¶ÔÓÚÈκεÄÀàÐÍ£ºÕûÐÍ¡¢¸¡µã¡¢×Ö·û´®¡¢²¼¶ûºÍ×ÊÔ´£¬Èç¹ûÄú½«Ò»¸öֵת»»ÎªÊý×飬Äú½«µÃµ½Ò»¸ö½öÓÐÒ»¸öÔªËØµÄÊý×飨ÆäϱêΪ 0£©£¬¼´´¿Á¿ÖµµÄÆðʼֵ¡£ + </para> + + <para> + Èç¹ûÄú½«Ò»¸ö¶ÔÏóת»»³ÉÒ»¸öÊý×飬ÄúËùµÃµ½µÄÊý×éµÄÔªËØÎª¸Ã¶ÔÏóµÄÊôÐÔ£¨³ÉÔ±±äÁ¿£©£¬Æä¼üÃûΪ³ÉÔ±±äÁ¿Ãû¡£ + </para> + + <para> + Èç¹ûÄú½«Ò»¸ö &null; ֵת»»³ÉÊý×飬Äú½«µÃµ½Ò»¸ö¿ÕÊý×é¡£ + </para> + </sect2> + <sect2 id="language.types.array.examples"> <title>Àý×Ó</title> <para> @@ -1031,24 +1174,24 @@ <programlisting role="php"> <![CDATA[ // this -$a = array( 'color' => 'red' - , 'taste' => 'sweet' - , 'shape' => 'round' - , 'name' => 'apple' - , 4 // key will be 0 +$a = array( 'color' => 'red', + 'taste' => 'sweet', + 'shape' => 'round', + 'name' => 'apple', + 4 // key will be 0 ); // is completely equivalent with $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; -$a['name'] = 'apple'; +$a['name'] = 'apple'; $a[] = 4; // key will be 0 $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; -// will result in the array array( 0 => 'a' , 1 => 'b' , 2 => 'c' ), +// will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'), // or simply array('a', 'b', 'c') ]]> </programlisting> @@ -1060,40 +1203,38 @@ <programlisting role="php"> <![CDATA[ // Array as (property-)map -$map = array( 'version' => 4 - , 'OS' => 'Linux' - , 'lang' => 'english' - , 'short_tags' => true +$map = array( 'version' => 4, + 'OS' => 'Linux', + 'lang' => 'english', + 'short_tags' => true ); // strictly numerical keys -$array = array( 7 - , 8 - , 0 - , 156 - , -10 +$array = array( 7, + 8, + 0, + 156, + -10 ); -// this is the same as array( 0 => 7, 1 => 8, ...) +// this is the same as array(0 => 7, 1 => 8, ...) -$switching = array( 10 // key = 0 - , 5 => 6 - , 3 => 7 - , 'a' => 4 - , 11 // key = 6 (maximum of integer-indices was 5) - , '8' => 2 // key = 8 (integer!) - , '02' => 77 // key = '02' - , 0 => 12 // the value 10 will be overwritten by 12 +$switching = array( 10, // key = 0 + 5 => 6, + 3 => 7, + 'a' => 4, + 11, // key = 6 (maximum of integer-indices was 5) + '8' => 2, // key = 8 (integer!) + '02' => 77, // key = '02' + 0 => 12 // the value 10 will be overwritten by 12 ); // empty array $empty = array(); ]]> <!-- TODO example of -- mixed keys - overwriting keys -- integer keys as string - using vars/functions as key/values -- mixed skipping +- warning about references --> </programlisting> </example> @@ -1102,9 +1243,9 @@ <title>¼¯ºÏ</title> <programlisting role="php"> <![CDATA[ -$colors = array('red','blue','green','yellow'); +$colors = array('red', 'blue', 'green', 'yellow'); -foreach ( $colors as $color ) { +foreach ($colors as $color) { echo "Do you like $color?\n"; } @@ -1174,13 +1315,12 @@ </example> </para> <example> - <title>Ìî³äÕæÕýµÄÊý×é</title> + <title>Ìî³äÊý×é</title> <programlisting role="php"> <![CDATA[ // fill an array with all items from a directory $handle = opendir('.'); -while ($file = readdir($handle)) -{ +while ($file = readdir($handle)) { $files[] = $file; } closedir($handle); @@ -1188,7 +1328,7 @@ </programlisting> </example> <para> - Êý×éÊÇÓÐÐòµÄ¡£ÄãÒ²¿ÉÒÔʹÓò»Í¬µÄÅÅÐòº¯ÊýÀ´¸Ä±ä˳Ðò¡£¸ü¶àÐÅÏ¢²Î¼û<link linkend="ref.array">Êý×麯Êý¿â</link>¡£ + Êý×éÊÇÓÐÐòµÄ¡£ÄãÒ²¿ÉÒÔʹÓò»Í¬µÄÅÅÐòº¯ÊýÀ´¸Ä±ä˳Ðò¡£¸ü¶àÐÅÏ¢²Î¼û<link linkend="ref.array">Êý×麯Êý¿â</link>¡£Äú¿ÉÒÔÓà <function>count</function> º¯ÊýÀ´Êý³öÊý×éÖÐÔªËØµÄ¸öÊý¡£ </para> <example> <title>Êý×éÅÅÐò</title> @@ -1206,228 +1346,51 @@ <title>µÝ¹éºÍ¶àάÊý×é</title> <programlisting role="php"> <![CDATA[ -$fruits = array ( "fruits" => array ( "a" => "orange" - , "b" => "banana" - , "c" => "apple" - ) - , "numbers" => array ( 1 - , 2 - , 3 - , 4 - , 5 - , 6 - ) - , "holes" => array ( "first" - , 5 => "second" - , "third" +$fruits = array ( "fruits" => array ( "a" => "orange", + "b" => "banana", + "c" => "apple" + ), + "numbers" => array ( 1, + 2, + 3, + 4, + 5, + 6, + ), + "holes" => array ( "first", + 5 => "second", + "third" ) ); + +// Some examples to address values in the array above +echo $fruits["holes"][5]; // prints "second" +echo $fruits["fruits"]["a"]; // prints "orange" +unset($fruits["holes"][0]); // remove "first" + +// Create a new multi-dimensional array +$juices["apple"]["green"] = "good"; ]]> -<!-- quite duplicate... -$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" - ) -); ---> </programlisting> </example> - </sect2> - - <!-- TODO - <sect2> - <title>Misc</title> - - </sect2> - - - example multi-dim with $arr[bla][bla] syntax - - converting to array - - warning about references - - note that assigning is copy (usually...) - - - --> - -<!-- there is no such thing as multi/singel dim arrays (at least in PHP4) - <sect2 id="language.types.array.single-dim"> - <title>Single Dimension Arrays</title> - - <para> - PHP supports both scalar and associative arrays. In fact, there - is no difference between the two. You can create an array using - the - - <function>list</function> - - Nope - - - - - or <function>array</function> - functions, or you can explicitly set each array element value. - <informalexample> - <programlisting role="php"> -$a[0] = "abc"; -$a[1] = "def"; -$b["foo"] = 13; - </programlisting> - </informalexample> - </para> - <para> - You can also create an array by simply adding values to the - array. When you assign a value to an array variable using empty - brackets, the value will be added onto the end of the array. - <informalexample> - <programlisting role="php"> -$a[] = "hello"; // $a[2] == "hello" -$a[] = "world"; // $a[3] == "world" - </programlisting> - </informalexample> - </para> - <para> - Arrays may be sorted using the <function>asort</function>, - <function>arsort</function>, <function>ksort</function>, - <function>rsort</function>, <function>sort</function>, - <function>uasort</function>, <function>usort</function>, and - <function>uksort</function> functions depending on the type of - sort you want. - </para> - <para> - You can count the number of items in an array using the - <function>count</function> function. - </para> - <para> - You can traverse an array using <function>next</function> and - <function>prev</function> functions. Another common way to - traverse an array is to use the <function>each</function> - function. - </para> - </sect2> - - <sect2 id="language.types.array.multi-dim"> - <title>Multi-Dimensional Arrays</title> - - <para> - Multi-dimensional arrays are actually pretty simple. For each - dimension of the array, you add another [key] value to the end: - <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> - In PHP 3 it is not possible to reference multidimensional arrays - directly within strings. For instance, the following will not - have the desired result: - <informalexample> - <programlisting role="php"> -$a[3]['bar'] = 'Bob'; -echo "This won't work: $a[3][bar]"; - </programlisting> - </informalexample> - In PHP 3, the above will output <computeroutput>This won't work: - Array[bar]</computeroutput>. The string concatenation operator, - however, can be used to overcome this: - <informalexample> - <programlisting role="php"> -$a[3]['bar'] = 'Bob'; -echo "This will work: " . $a[3]['bar']; - </programlisting> - </informalexample> - </para> <para> - In PHP 4, however, the whole problem may be circumvented by - enclosing the array reference (inside the string) in curly - braces: + ÄúÐèҪעÒâÊý×éµÄ¸³Öµ×ÜÊÇ»áÉæ¼°µ½ÖµµÄ¿½±´¡£ÄúÐèÒªÔÚ¸´ÖÆÊý×éʱÓÃÖ¸Ïò·ûºÅ£¨&£©¡£ <informalexample> <programlisting role="php"> -$a[3]['bar'] = 'Bob'; -echo "This will work: {$a[3][bar]}"; - </programlisting> - </informalexample> - </para> - <para> - You can "fill up" multi-dimensional arrays in many ways, but the - trickiest one to understand is how to use the - <function>array</function> command for associative arrays. These - two snippets of code fill up the one-dimensional array in the - same way: - <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> - The <function>array</function> function can be nested for - multi-dimensional arrays: - <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" -?> +<![CDATA[ +$arr1 = array(2, 3); +$arr2 = $arr1; +$arr2[] = 4; // $arr2 is changed, + // $arr1 is still array(2,3) + +$arr3 = &$arr1; +$arr3[] = 4; // now $arr1 and $arr3 are the same +]]> </programlisting> </informalexample> </para> - </sect2> - - --> </sect1> <sect1 id="language.types.object"> @@ -1461,7 +1424,19 @@ <simpara> ÍêÕûµÄÌÖÂÛ¼û<link linkend="language.oop">ÀàÓë¶ÔÏó</link>Ò»Õ¡£ </simpara> - + </sect2> + <sect2 id="language.types.object.casting"> + <title>ת»»Îª¶ÔÏó</title> + + <para> + Èç¹û½«Ò»¸ö¶ÔÏóת»»³É¶ÔÏó£¬Ëü½«²»»áÓÐÈκα仯¡£Èç¹ûÆäËüÈκÎÀàÐ͵ÄÖµ±»×ª»»³É¶ÔÏó£¬ÄÚÖÃÀà <literal>stdClass</literal> µÄÒ»¸öʵÀý½«±»½¨Á¢¡£Èç¹û¸ÃֵΪ &null;£¬ÔòеÄʵÀýΪ¿Õ¡£¶ÔÓÚÈÎºÎÆäËüµÄÖµ£¬ÃûΪ <literal>scalar</literal> µÄ³ÉÔ±±äÁ¿½«°üº¬¸ÃÖµ¡£ + <informalexample> + <programlisting role="php"> +$obj = (object) 'ciao'; +echo $obj->scalar; // outputs 'ciao' + </programlisting> + </informalexample> + </para> </sect2> </sect1> @@ -1478,6 +1453,14 @@ </simpara> </note> + <sect2 id="language.types.resource.casting"> + <title>ת»»Îª×ÊÔ´</title> + + <para> + ÓÉÓÚ×ÊÔ´ÀàÐͱäÁ¿±£´æÓÐΪ´ò¿ªÎļþ¡¢Êý¾Ý¿âÁ¬½Ó¡¢Í¼Ðλ²¼ÇøÓòµÈµÄÌØÊâ¾ä±ú£¬ÄúÎÞ·¨½«ÆäËüÀàÐ͵Äֵת»»Îª×ÊÔ´¡£ + </para> + </sect2> + <sect2 id="language.types.resource.self-destruct"> <title>ÊÍ·Å×ÊÔ´</title> @@ -1542,6 +1525,87 @@ </sect2> </sect1> + + <sect1 id="language.pseudo-types"> + <title>±¾ÎĵµÖÐʹÓõÄαÀàÐÍ</title> + + <sect2 id="language.types.mixed"> + <title>mixed</title> + <para> + <literal>mixed</literal> ˵Ã÷Ò»¸ö²ÎÊý¿ÉÒÔ½ÓÊܶàÖÖ²»Í¬µÄ£¨µ«²¢²»±ØÐëÊÇËùÓеģ©ÀàÐÍ¡£ + </para> + <para> + <function>gettype</function> ±íÃ÷¿ÉÒÔ½ÓÊÜËùÓÐµÄ PHP ÀàÐÍ£¬ÀýÈç <function>str_replace</function> ½«½ÓÊÜ×Ö·û´®ºÍÊý×é¡£ + </para> + </sect2> + + <sect2 id="language.types.number"> + <title>number</title> + <para> + <literal>number</literal> ˵Ã÷Ò»¸ö²ÎÊý¿ÉÒÔÊÇ <type>integer</type> »òÕß <type>float</type>¡£ + </para> + </sect2> + + <sect2 id="language.types.callback"> + <title>callback</title> + <para> + ÓÐЩÖîÈç <function>call_user_function</function> »ò <function>usort</function> µÄº¯Êý½ÓÊÜÓû§×Ô¶¨ÒåµÄº¯Êý×÷Ϊһ¸ö²ÎÊý¡£Callback º¯Êý²»½ö¿ÉÒÔÊÇÒ»¸ö¼òµ¥µÄº¯Êý£¬Ëü»¹¿ÉÒÔÊÇÒ»¸ö¶ÔÏóµÄ·½·¨£¬°üÀ¨¾²Ì¬ÀàµÄ·½·¨¡£ + </para> + <para> + Ò»¸ö PHP º¯ÊýÓú¯ÊýÃû×Ö·û´®À´´«µÝ¡£Äú¿ÉÒÔ´«µÝÈκÎÄÚ½¨µÄ»òÕßÓû§×Ô¶¨ÒåµÄº¯Êý£¬³ýÁË + <function>array</function>£¬ + <function>echo</function>£¬ + <function>empty</function>£¬ + <function>eval</function>£¬ + <function>exit</function>£¬ + <function>isset</function>£¬ + <function>list</function>£¬ + <function>print</function> ºÍ + <function>unset</function>¡£ + </para> + <para> + Ò»¸ö¶ÔÏóµÄ·½·¨ÒÔÊý×éµÄÐÎʽÀ´´«µÝ£¬Êý×éµÄ 0 ϱêÖ¸Ã÷¶ÔÏóÃû£¬Ï±ê 1 Ö¸Ã÷·½·¨Ãû¡£ + </para> + <para> + ¶ÔÓÚûÓÐʵÀý»¯Îª¶ÔÏóµÄ¾²Ì¬À࣬Ҫ´«µÝÆä·½·¨£¬½«Êý×é 0 ϱêÖ¸Ã÷µÄ¶ÔÏóÃû»»³É¸ÃÀàµÄÃû³Æ¼´¿É¡£ + </para> + + <para> + <example> + <title> + Callback º¯ÊýʵÀý + </title> + <programlisting role="php"> +<![CDATA[ +<?php + +// simple callback example +function foobar() { + echo "hello world!"; +} +call_user_function("foobar"); + +// method callback examples +class foo { + function bar() { + echo "hello world!"; + } +} + +$foo = new foo; + +call_user_function(array($foo, "bar")); // object method call + +call_user_function(array("foo", "bar")); // static class method call + +?> +]]> + </programlisting> + </example> + </para> + + </sect2> + </sect1> <sect1 id="language.types.type-juggling"> <title>ÀàÐÍÏ··¨</title> @@ -1585,7 +1649,7 @@ </informalexample> </para> <simpara> - Èç¹ûÉÏÃæÁ½¸öÀý×Ó¿´ÉÏÈ¥¹Å¹ÖµÄ»°£¬²Î¼û<link linkend="language.types.string.conversion">×Ö·û´®×ª»»</link>¡£ + Èç¹ûÉÏÃæÁ½¸öÀý×Ó¿´ÉÏÈ¥¹Å¹ÖµÄ»°£¬²Î¼û<link linkend="language.types.string.conversion">×Ö·û´®×ª»»ÎªÊýÖµ</link>¡£ </simpara> <simpara> Èç¹ûÄãÒªÇ¿ÖÆ½«Ò»¸ö±äÁ¿µ±×÷ijÖÖÀàÐÍÀ´ÇóÖµ£¬²Î¼û<link @@ -1607,14 +1671,18 @@ </informalexample> </para> <para> - ÓÉÓÚ PHP Ö§³Öͨ¹ýÆ«ÒÆÁ¿½øÐеÄ×Ö·û´®Ë÷Òý£¬ÕâºÍÊý×éË÷ÒýµÄÓï·¨Ò»Ñù¡£ÒÔÉϵÄÀý×ӾͲúÉúÁËÒ»¸öÎÊÌ⣺$a + ÓÉÓÚһЩÀúÊ·ÔÒò£¬PHP Ö§³Öͨ¹ýÆ«ÒÆÁ¿½øÐеÄ×Ö·û´®Ë÷Òý£¬ÕâºÍÊý×éË÷ÒýµÄÓï·¨Ò»Ñù¡£ÒÔÉϵÄÀý×ӾͲúÉúÁËÒ»¸öÎÊÌ⣺$a Ó¦¸Ã±ä³ÉÒ»¸öµÚÒ»¸öÔªËØÊÇ¡°f¡±µÄÊý×éÄØ£¬»¹ÊÇ¡°f¡±³ÉÁË×Ö·û´® $a µÄµÚÒ»¸ö×Ö·û£¿ </para> <para> - ÓÉÓÚ´ËÔÒò£¬ÔÚ PHP 3.0.12 ºÍ PHP 4.0b3-RC4 ÖÐÕâÖÖ×Ô¶¯×ª»»µÄ½á¹û±»µ±³É䶨Òå¡£½â¾ö·½°¸ÈÔÔÚÌÖÂÛÖ®ÖС£ - </para> - <para> - £¨ÒëÕß×¢£º£©PHP 4 ÖÐÉÏÃæµÄÒ»¸öÀý×Ó³ö´í£¬µÚ¶þ¸öÀý×ӵĽá¹û½«ÊÇ×Ö·û´® $a = 'f'¡£ + Ŀǰ°æ±¾µÄ PHP ½«ÒÔÉϵڶþ¸ö¸³ÖµÀí½â³É×Ö·û´®µÄÆ«ÒÆÁ¿±êʶ£¬¼´ $a ±ä³ÉÁË "f"£¬¾¡¹ÜÈç´Ë£¬ÕâÖÖ×Ô¶¯×ª»»µÄµØ½á¹ûÓ¦¸Ã±»ÈÏΪ䶨Òå¡£PHP 4 ÒýÈëÁËÐµĻ¨À¨ºÅÓï·¨À´·ÃÎÊ×Ö·û´®µÄ×Ö·û£¬ÇëʹÓøÃÓï·¨À´Ìæ´úÒÔÉϵIJÙ×÷£º + <informalexample> + <programlisting role="php"> +$a = "abc"; // $a Ϊһ¸ö×Ö·û´® +$a{1} = "f"; // $a ĿǰΪ "afc" + </programlisting> + </informalexample> + Çë²ÎÔÄÌâΪ¡°<link linkend="language.types.string.substr">ÓÃ×Ö·û·ÃÎÊ×Ö·û´®</link>¡±µÄÕ½ÚÒÔ»ñÈ¡¸ü¶àÐÅÏ¢¡£ </para> </note> @@ -1626,7 +1694,7 @@ <informalexample> <programlisting role="php"> $foo = 10; // $foo is an integer -$bar = (float) $foo; // $bar is a float +$bar = (boolean) $foo; // $bar is a boolean </programlisting> </informalexample> </para> @@ -1653,12 +1721,6 @@ </listitem> </itemizedlist> </para> - <note> - <simpara> - ³ýÁËÓÃÇ¿ÖÆ×ª»»½«±äÁ¿×ª»»³É×Ö·û´®Ö®Í⣬»¹¿ÉÒÔ½«±äÁ¿ÓÃË«ÒýºÅÒýÆðÀ´¡£ - <!-- TODO: example --> - </simpara> - </note> <para> ×¢ÒâÔÚÀ¨ºÅÄÚÔÊÐíÓпոñºÍÖÆ±í·û£¬ËùÒÔÏÂÃæÁ½¸öÀý×Ó¹¦ÄÜÏàͬ£º @@ -1669,6 +1731,24 @@ </programlisting> </informalexample> </para> + <note> + <para> + ΪÁ˽«Ò»¸ö±äÁ¿»¹ÔΪ×Ö·û´®£¬Äú»¹¿ÉÒÔ½«±äÁ¿·ÅÖÃÔÚË«ÒýºÅÖÖ¡£ + <informalexample> + <programlisting role="php"> +$foo = 10; // $foo is an integer +$str = "$foo"; // $str is a string +$fst = (string) $foo; // $fst is also a string + +// This prints out that "they are the same" +if ($fst === $str) { + echo "they are the same"; +} + </programlisting> + </informalexample> + </para> + </note> + <para> µ±ÔÚijЩÀàÐÍÖ®¼äÇ¿ÖÆ×ª»»Ê±È·ÇеػᷢÉúʲô¿ÉÄܲ»ÊǺÜÃ÷ÏÔ¡£¸ü¶àÐÅÏ¢¼ûÈçÏÂС½Ú£º @@ -1679,63 +1759,31 @@ <listitem> <simpara><link linkend="language.types.integer.casting">ת»»ÎªÕûÐÍ</link></simpara> </listitem> - <!-- don't exist yet <listitem> - <simpara><link linkend="language.types.float.casting">Converting to - float</link></simpara> + <simpara><link linkend="language.types.float.casting">ת»»Îª¸¡µãÐÍ</link></simpara> </listitem> <listitem> - <simpara><link linkend="language.types.string.casting">Converting to - string</link></simpara> + <simpara><link linkend="language.types.string.casting">ת»»Îª×Ö·û´®</link></simpara> </listitem> <listitem> - <simpara><link linkend="language.types.array.casting">Converting to - array</link></simpara> + <simpara><link linkend="language.types.array.casting">ת»»ÎªÊý×é</link></simpara> </listitem> <listitem> - <simpara><link linkend="language.types.object.casting">Converting to - object</link></simpara> + <simpara><link linkend="language.types.object.casting">ת»»Îª¶ÔÏó</link></simpara> </listitem> <listitem> - <simpara><link linkend="language.types.resource.casting">Converting to - resource</link></simpara> + <simpara><link linkend="language.types.resource.casting">ת»»Îª×ÊÔ´</link></simpara> </listitem> + <!-- don't exist yet <listitem> <simpara><link linkend="language.types.null.casting">Converting to &null;</link></simpara> </listitem> --> </itemizedlist> - - </para> - <para> - <!-- TODO: move to 'converting to string' --> - µ±Ç¿Öƽ«Êý×éת»»Îª×Ö·û´®Ê±£¬½á¹û½«ÊÇ <literal>Array</literal> Õâ¸ö´Ê¡£µ±Ç¿Öƽ«¶ÔÏóת»»Îª×Ö·û´®Ê±£¬½á¹û½«ÊÇ <literal>Object</literal> Õâ¸ö´Ê¡£ - </para> - <para> - µ±Ç¿Öƽ«Ò»¸ö±êÁ¿»ò×Ö·û´®×ª»»ÎªÊý×éʱ£¬±äÁ¿½«³ÉΪÊý×éµÄµÚÒ»¸öÔªËØ£º - <informalexample> - <programlisting role="php"> -$var = 'ciao'; -$arr = (array) $var; -echo $arr[0]; // outputs 'ciao' - </programlisting> - </informalexample> </para> - <para> - µ±Ç¿Öƽ«Ò»¸ö±êÁ¿»ò×Ö·û´®×ª»»Îª¶ÔÏóʱ£¬±äÁ¿»á³ÉΪ¶ÔÏóµÄÊôÐÔ£¬ÊôÐÔÃûΪ¡°scalar¡±£º - <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