cvs: phpdoc-zh /language operators.xml
| From: | Dallas Thunder | Date: | Tue, 16 Aug 2005 15:25:55 +0000 |
| Subject: | cvs: phpdoc-zh /language operators.xml | ||
| Groups: | php.doc.zh | ||
| Request: | Send a blank email to doc-zh+get-2337@lists.php.net to get a copy of this message | ||
dallas Tue Aug 16 11:25:55 2005 EDT
Modified files:
/phpdoc-zh/language operators.xml
Log:
Updated.
http://cvs.php.net/diff.php/phpdoc-zh/language/operators.xml?r1=1.10&r2=1.11&ty=u Index: phpdoc-zh/language/operators.xml diff -u phpdoc-zh/language/operators.xml:1.10 phpdoc-zh/language/operators.xml:1.11 --- phpdoc-zh/language/operators.xml:1.10 Sat Aug 16 23:32:29 2003 +++ phpdoc-zh/language/operators.xml Tue Aug 16 11:25:54 2005 @@ -1,20 +1,33 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.10 $ --> +<!-- $Revision: 1.11 $ --> <!-- $Author: dallas $ --> -<!-- EN-Revision: 1.53 Maintainer: dallas Status: ready --> +<!-- EN-Revision: 1.94 Maintainer: dallas Status: ready --> <chapter id="language.operators"> <title>ÔËËã·û</title> <simpara> + ÔËËã·ûÊÇ¿ÉÒÔͨ¹ý¸ø³öµÄÒ»»ò¶à¸öÖµ£¨Óñà³ÌÐл°À´Ëµ£¬±í´ïʽ£©À´²úÉúÁíÒ»¸öÖµ£¨Òò¶øÕû¸ö½á¹¹³ÉΪһ¸ö±í´ïʽ£©µÄ¶«Î÷¡£ËùÒÔ¿ÉÒÔÈÏΪº¯Êý»òÈκλ᷵»ØÒ»¸öÖµ£¨ÀýÈç + print£©µÄ½á¹¹ÊÇÔËËã·û£¬¶øÄÇЩûÓзµ»ØÖµµÄ£¨ÀýÈç echo£©ÊDZðµÄ¶«Î÷¡£ </simpara> + <para> + ÓÐÈýÖÖÀàÐ͵ÄÔËËã·û¡£µÚÒ»ÖÖÊÇÒ»ÔªÔËËã·û£¬Ö»ÔËËãÒ»¸öÖµ£¬ÀýÈç + !£¨È¡·´ÔËËã·û£©»ò ++£¨¼ÓÒ»ÔËËã·û£©¡£µÚ¶þÖÖÊÇÓÐÏÞ¶þÔªÔËËã·û£¬PHP + Ö§³ÖµÄ´ó¶àÊýÔËËã·û¶¼ÊÇÕâÖÖ¡£Áбí¼ûϽÚ<link + linkend="language.operators.precedence">ÔËËã·ûÓÅÏȼ¶</link>¡£ + </para> + <para> + µÚÈýÖÖÊÇÈýÔªÔËËã·û£º?:¡£ËüÓ¦ÓÃÀ´¸ù¾ÝÒ»¸ö±í´ïʽÔÚÁíÁ½¸ö±í´ïʽÖÐÑ¡ÔñÒ»¸ö£¬¶ø²»ÊÇÓÃÀ´Ñ¡ÔñÔÚÁ½¸öÓï¾ä»òÕß³ÌÐò·ÏßÖÐÑ¡Ôñ¡£°ÑÕû¸öÈýÔª±í´ïʽ·ÅÔÚÀ©ºÅÀïÊǸöºÜºÃµÄÖ÷Òâ¡£ + </para> <sect1 id="language.operators.precedence"> <title>ÔËËã·ûÓÅÏȼ¶</title> <para> ÔËËã·ûÓÅÏȼ¶Ö¸¶¨ÁËÁ½¸ö±í´ïʽ°ó¶¨µÃÓжࡰ½ôÃÜ¡±¡£ÀýÈ磬±í´ïʽ <literal>1 + 5 * - 3</literal> µÄ½á¹ûÊÇ <literal>16</literal> ¶ø²»ÊÇ <literal>18</literal> ÊÇÒòΪ³ËºÅ£¨¡°*¡±£©µÄÓÅÏȼ¶±È¼ÓºÅ£¨¡°+¡±£©¸ß¡£±ØÒªÊ±¿ÉÒÔÓÃÀ¨ºÅÀ´Ç¿ÖƸıäÓÅÏȼ¶¡£ÀýÈ磺<literal>(1 + 5) * 3</literal> µÄֵΪ <literal>18</literal>¡£ + 3</literal> µÄ½á¹ûÊÇ <literal>16</literal> ¶ø²»ÊÇ <literal>18</literal> + ÊÇÒòΪ³ËºÅ£¨¡°*¡±£©µÄÓÅÏȼ¶±È¼ÓºÅ£¨¡°+¡±£©¸ß¡£±ØÒªÊ±¿ÉÒÔÓÃÀ¨ºÅÀ´Ç¿ÖƸıäÓÅÏȼ¶¡£ÀýÈ磺<literal>(1 + + 5) * 3</literal> µÄֵΪ <literal>18</literal>¡£Èç¹ûÔËËã·ûÓÅÏȼ¶Ïàͬ£¬ÔòʹÓôÓ×óµ½ÓÒµÄ×óÁªË³Ðò¡£ </para> <para> - ϱí´ÓµÍµ½¸ßÁгöÁËÔËËã·ûµÄÓÅÏȼ¶¡£ + ϱí´Ó¸ßµ½µÍÁгöÁËÔËËã·ûµÄÓÅÏȼ¶¡£Í¬Ò»ÐÐÖеÄÔËËã·û¾ßÓÐÏàͬÓÅÏȼ¶£¬´ËʱËüÃǵĽáºÏ·½Ïò¾ö¶¨Çóֵ˳Ðò¡£ <table> <title>ÔËËã·ûÓÅÏȼ¶</title> <tgroup cols="2"> @@ -22,95 +35,169 @@ <row> <entry>½áºÏ·½Ïò</entry> <entry>ÔËËã·û</entry> + <entry>¸½¼ÓÐÅÏ¢</entry> </row> </thead> <tbody> <row> - <entry>×ó</entry> - <entry>,</entry> + <entry>·Ç½áºÏ</entry> + <entry>new</entry> + <entry><link linkend="language.oop5.basic.new">new</link></entry> </row> <row> - <entry>×ó</entry> - <entry>or</entry> + <entry>ÓÒ</entry> + <entry>[</entry> + <entry><function>array</function></entry> + </row> + <row> + <entry>·Ç½áºÏ</entry> + <entry>++ --</entry> + <entry> + <link linkend="language.operators.increment">µÝÔö£¯µÝ¼õÔËËã·û</link> + </entry> + </row> + <row> + <entry>·Ç½áºÏ</entry> + <entry>! ~ - (int) (float) (string) (array) (object) @</entry> + <entry> + <link linkend="language.types">ÀàÐÍ</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>xor</entry> + <entry>* / %</entry> + <entry> + <link linkend="language.operators.arithmetic">ËãÊýÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>and</entry> + <entry>+ - .</entry> + <entry> + <link linkend="language.operators.arithmetic">ËãÊýÔËËã·û</link>&listendand; + <link linkend="language.operators.string">×Ö·û´®ÔËËã·û</link></entry> </row> <row> - <entry>ÓÒ</entry> - <entry>print</entry> + <entry>×ó</entry> + <entry><< >></entry> + <entry> + <link linkend="language.operators.bitwise">λÔËËã·û</link> + </entry> </row> <row> - <entry>ÓÒ</entry> + <entry>·Ç½áºÏ</entry> + <entry>< <= > >=</entry> <entry> - = += -= *= /= .= %= &= |= ^= ~= <<= >>= + <link linkend="language.operators.comparison">±È½ÏÔËËã·û</link> </entry> </row> <row> - <entry>×ó</entry> - <entry>? :</entry> + <entry>·Ç½áºÏ</entry> + <entry>== != === !==</entry> + <entry> + <link linkend="language.operators.comparison">±È½ÏÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>||</entry> + <entry>&</entry> + <entry> + <link linkend="language.operators.bitwise">λÔËËã·û</link>&listendand; + <link linkend="language.references">ÒýÓÃ</link></entry> </row> <row> <entry>×ó</entry> - <entry>&&</entry> + <entry>^</entry> + <entry> + <link linkend="language.operators.bitwise">λÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> <entry>|</entry> + <entry> + <link linkend="language.operators.bitwise">λÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>^</entry> + <entry>&&</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>&</entry> + <entry>||</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> - <entry>ÎÞ</entry> - <entry>== != === !==</entry> + <entry>×ó</entry> + <entry>? :</entry> + <entry> + <link linkend="language.operators.comparison.ternary">ÈýÔªÔËËã·û</link> + </entry> </row> <row> - <entry>ÎÞ</entry> - <entry>< <= > >=</entry> + <entry>ÓÒ</entry> + <entry> + = += -= *= /= .= %= &= |= ^= <<= >>= + </entry> + <entry> + <link linkend="language.operators.assignment">¸³ÖµÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry><< >></entry> + <entry>and</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>+ - .</entry> + <entry>xor</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>* / %</entry> - </row> - <row> - <entry>ÓÒ</entry> - <entry>! ~ ++ -- (int) (float) (string) (array) (object) @</entry> - </row> - <row> - <entry>ÓÒ</entry> - <entry>[</entry> + <entry>or</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> - <entry>ÎÞ</entry> - <entry>new</entry> + <entry>×ó</entry> + <entry>,</entry> + <entry>¶à´¦Óõ½</entry> </row> </tbody> </tgroup> </table> </para> + <para> + ×óÁª±íʾ±í´ïʽ´Ó×óÏòÓÒÇóÖµ£¬ÓÒÁªÏà·´¡£ + <example> + <title>½áºÏ·½Ïò</title> + <programlisting role="php"> +<![CDATA[ +<?php +$a = 3 * 3 % 5; // (3 * 3) % 5 = 4 +$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2 + +$a = 1; +$b = 2; +$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5 +?> +]]> + </programlisting> + </example> + ʹÓÃÀ¨ºÅ¿ÉÒÔÔöÇ¿´úÂëµÄ¿É¶ÁÐÔ¡£ + </para> <note> <para> ¾¡¹Ü <literal>!</literal> ±È <literal>=</literal> µÄÓÅÏȼ¶¸ß£¬PHP @@ -137,6 +224,11 @@ </thead> <tbody> <row> + <entry>-$a</entry> + <entry>È¡·´</entry> + <entry>$a µÄ¸ºÖµ¡£</entry> + </row> + <row> <entry>$a + $b</entry> <entry>¼Ó·¨</entry> <entry>$a ºÍ $b µÄºÍ¡£</entry> @@ -167,8 +259,14 @@ <simpara> ³ýºÅ£¨¡°/¡±£©×ÜÊÇ·µ»Ø¸¡µãÊý£¬¼´Ê¹Á½¸öÔËËãÊýÊÇÕûÊý£¨»òÓÉ×Ö·û´®×ª»»³ÉµÄÕûÊý£©Ò²ÊÇÕâÑù¡£ </simpara> + <note> + <simpara> + ȡģ <literal>$a % $b</literal> ÔÚ <literal>$a</literal> + Ϊ¸ºÖµÊ±µÄ½á¹ûÒ²ÊǸºÖµ¡£ + </simpara> + </note> <simpara> - Çë²éÔÄÊֲᡰ<link linkend="ref.math">Êýѧº¯Êý</link>¡±ÓйØÕ½ڡ£ + ²Î¼ûÊÖ²áÖеÄ<link linkend="ref.math">Êýѧº¯Êý¿â</link>¡£ </simpara> <!-- @@ -193,27 +291,38 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php + $a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4. + +?> ]]> </programlisting> </informalexample> </para> <para> - ÔÚ»ù±¾¸³ÖµÔËËã·ûÖ®Í⣬»¹ÓÐÊʺÏÓÚËùÓжþÔªËãÊõºÍ×Ö·û´®ÔËËã·ûµÄ¡°×éºÍÔËËã·û¡±£¬Õâ¿ÉÒÔÈÃÄãÔÚÒ»¸ö±í´ïʽÖÐʹÓÃËüµÄÖµ²¢°Ñ±í´ïʽµÄ½á¹û¸³¸øËü£¬ÀýÈ磺 + ÔÚ»ù±¾¸³ÖµÔËËã·ûÖ®Í⣬»¹ÓÐÊʺÏÓÚËùÓÐ<link + linkend="language.operators">¶þÔªËãÊõ</link>ºÍ×Ö·û´®ÔËËã·ûµÄ¡°×éºÍÔËËã·û¡±£¬Õâ¿ÉÒÔÈÃÄãÔÚÒ»¸ö±í´ïʽÖÐʹÓÃËüµÄÖµ²¢°Ñ±í´ïʽµÄ½á¹û¸³¸øËü£¬ÀýÈ磺 <informalexample> <programlisting role="php"> <![CDATA[ +<?php + $a = 3; $a += 5; // sets $a to 8, as if we had said: $a = $a + 5; $b = "Hello "; $b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!"; + +?> ]]> </programlisting> </informalexample> </para> <para> - ×¢Ò⸳ֵÔËË㽫ԱäÁ¿µÄÖµ¿½±´µ½Ð±äÁ¿ÖУ¨´«Öµ¸³Öµ£©£¬ËùÒԸıäÆäÖÐÒ»¸ö²¢²»Ó°ÏìÁíÒ»¸ö¡£ÕâÒ²ÊʺÏÓÚÄãÔÚÔÚ½ôÃÜÑ»·Öп½±´Ò»Ð©ÖµÀýÈç´óÊýÖµ¡£PHP 4 Ö§³ÖÒýÓø³Öµ£¬Óà <computeroutput>$var = - &$othervar;</computeroutput> Óï·¨£¬µ«ÔÚ PHP 3 Öв»¿ÉÄÜÕâÑù×ö¡£¡°ÒýÓø³Öµ¡±Òâζ×ÅÁ½¸ö±äÁ¿¶¼Ö¸Ïòͬһ¸öÊý¾Ý£¬Ã»ÓÐÈκÎÊý¾ÝµÄ¿½±´¡£ÓйØÒýÓõĸü¶àÐÅÏ¢¼û<link + ×¢Ò⸳ֵÔËË㽫ԱäÁ¿µÄÖµ¿½±´µ½Ð±äÁ¿ÖУ¨´«Öµ¸³Öµ£©£¬ËùÒԸıäÆäÖÐÒ»¸ö²¢²»Ó°ÏìÁíÒ»¸ö¡£ÕâÒ²ÊʺÏÓÚÄãÔÚÔÚ½ôÃÜÑ»·Öп½±´Ò»Ð©ÖµÀýÈç´óÊýÖµ¡£×Ô + PHP 4 ÆðÖ§³ÖÒýÓø³Öµ£¬Óà <computeroutput>$var = + &$othervar;</computeroutput> Óï·¨£¬µ«ÔÚ PHP 3 + Öв»¿ÉÄÜÕâÑù×ö¡£¡°ÒýÓø³Öµ¡±Òâζ×ÅÁ½¸ö±äÁ¿¶¼Ö¸Ïòͬһ¸öÊý¾Ý£¬Ã»ÓÐÈκÎÊý¾ÝµÄ¿½±´¡£ÓйØÒýÓõĸü¶àÐÅÏ¢¼û<link linkend="language.references">ÒýÓõÄ˵Ã÷</link>¡£ </para> </sect1> @@ -221,19 +330,20 @@ <sect1 id="language.operators.bitwise"> <title>λÔËËã·û</title> <simpara> - λÔËËã·ûÔÊÐí¶ÔÕûÐÍÊýÖÐÖ¸¶¨µÄλ½øÐÐÖÃλ¡£Èç¹û×óÓÒ²ÎÊý¶¼ÊÇ×Ö·û´®£¬ÔòλÔËËã·û½«²Ù×÷Õâ¸ö×Ö·û´®ÖеÄ×Ö·û¡£ + λÔËËã·ûÔÊÐí¶ÔÕûÐÍÊýÖÐÖ¸¶¨µÄλ½øÐÐÖÃλ¡£Èç¹û×óÓÒ²ÎÊý¶¼ÊÇ×Ö·û´®£¬ÔòλÔËËã·û½«²Ù×÷×Ö·ûµÄ + ASCII Öµ¡£ </simpara> <para> <informalexample> <programlisting role="php"> <![CDATA[ <?php - echo 12 ^ 9; // Outputs '5' +echo 12 ^ 9; // Outputs '5' - echo "12" ^ "9"; // Outputs the Backspace character (ascii 8) +echo "12" ^ "9"; // Outputs the Backspace character (ascii 8) // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8 - echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0 +echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0 // 'a' ^ 'e' = #4 ?> ]]> @@ -285,6 +395,11 @@ </tbody> </tgroup> </table> + <warning> + <para> + ÔÚ 32 λϵͳÉϲ»ÒªÓÒÒÆ³¬¹ý 32 λ¡£²»ÒªÔÚ½á¹û¿ÉÄܳ¬¹ý 32 λµÄÇé¿öÏÂ×óÒÆ¡£ + </para> + </warning> </sect1> <sect1 id="language.operators.comparison"> @@ -312,7 +427,7 @@ <row> <entry>$a === $b</entry> <entry>È«µÈ</entry> - <entry>&true;£¬Èç¹û $a µÈÓÚ $b£¬²¢ÇÒËüÃǵÄÀàÐÍÒ²Ïàͬ¡£(PHP 4 only)</entry> + <entry>&true;£¬Èç¹û $a µÈÓÚ $b£¬²¢ÇÒËüÃǵÄÀàÐÍÒ²Ïàͬ¡££¨PHP 4 Òý½ø£©</entry> </row> <row> <entry>$a != $b</entry> @@ -353,38 +468,176 @@ </tgroup> </table> <para> - ÁíÍâÒ»¸öÌõ¼þÔËËã·ûÊÇ¡°?:¡±£¨»òÈýÔª£©ÔËËã·û£¬ËüºÍ C ÒÔ¼°ºÜ¶àÆäËüÓïÑԵIJÙ×÷Ò»Ñù¡£ + Èç¹û±È½ÏÒ»¸öÕûÊýºÍ×Ö·û´®£¬Ôò×Ö·û´®»á±»<link + linkend="language.types.string.conversion">ת»»ÎªÕûÊý</link>¡£Èç¹û±È½ÏÁ½¸öÊý×Ö×Ö·û´®£¬Ôò×÷ΪÕûÊý±È½Ï¡£´Ë¹æÔòÒ²ÊÊÓÃÓÚ + <link linkend="control-structures.switch">switch</link> Óï¾ä¡£ <informalexample> <programlisting role="php"> <![CDATA[ <?php -// Example usage for: Ternary Operator -$action = (empty($_POST['action'])) ? 'default' : $_POST['action']; +var_dump(0 == "a"); // 0 == 0 -> true +var_dump("1" == "01"); // 1 == 1 -> true -// The above is identical to this if/else statement -if (empty($_POST['action'])) { - $action = 'default'; -} else { - $action = $_POST['action']; +switch ("a") { +case 0: + echo "0"; + break; +case "a": // never reached because "a" is already matched with 0 + echo "a"; + break; } ?> ]]> </programlisting> </informalexample> - ¶ÔÓÚ±í´ïʽ <literal>(expr1) ? (expr2) : (expr3)</literal>£¬Èç¹û <replaceable>expr1</replaceable> µÄֵΪ &true;£¬Ôò´Ë±í´ïʽµÄֵΪ <replaceable>expr2</replaceable>£¬Èç¹û <replaceable>expr1</replaceable> µÄֵΪ &false;£¬Ôò´Ë±í´ïʽµÄֵΪ <replaceable>expr3</replaceable>¡£ </para> + + <para> + Èç¹ûÔËËãÊýµÄÀàÐͲ»Í¬£¬Ôò¸ù¾Ýϱí±È½Ï£¨°´Ë³Ðò£©¡£ + </para> + <table> + <title>±È½Ï²»Í¬ÀàÐÍ</title> + <tgroup cols="3"> + <thead> + <row> + <entry>ÔËËãÊý 1 ÀàÐÍ</entry> + <entry>ÔËËãÊý 1 ÀàÐÍ</entry> + <entry>½á¹û</entry> + </row> + </thead> + <tbody> + <row> + <entry><type>null</type> »ò <type>string</type></entry> + <entry><type>string</type></entry> + <entry>½« &null; ת»»Îª ""£¬½øÐÐÊý×Ö»ò´Ê»ã±È½Ï</entry> + </row> + <row> + <entry><type>bool</type> »ò <type>null</type></entry> + <entry>ÈÎºÎÆäËüÀàÐÍ</entry> + <entry>ת»»Îª <type>bool</type>£¬&false; < &true;</entry> + </row> + <row> + <entry><type>object</type></entry> + <entry><type>object</type></entry> + <entry> + ÄÚÖÃÀà¿ÉÒÔ¶¨Òå×Ô¼ºµÄ±È½Ï£¬²»Í¬À಻ÄܱȽϣ¬ÏàͬÀàºÍÊý×éͬÑù·½Ê½±È½ÏÊôÐÔ£¨PHP + 4 ÖУ©£¬PHP 5 ÓÐÆä×Ô¼ºµÄ<link + linkend="language.oop5.object-comparison">˵Ã÷</link> + </entry> + </row> + <row> + <entry><type>string</type>£¬<type>resource</type> »ò <type>number</type></entry> + <entry><type>string</type>£¬<type>resource</type> »ò <type>number</type></entry> + <entry>½«×Ö·û´®ºÍ×ÊԴת»»³ÉÊý×Ö£¬°´ÆÕͨÊýѧ±È½Ï</entry> + </row> + <row> + <entry><type>array</type></entry> + <entry><type>array</type></entry> + <entry> + ¾ßÓнÏÉÙ³ÉÔ±µÄÊý×é½ÏС£¬Èç¹ûÔËËãÊý 1 Öеļü²»´æÔÚÓÚÔËËãÊý + 2 ÖÐÔòÊý×éÎÞ·¨±È½Ï£¬·ñÔò°¤¸öÖµ±È½Ï£¨¼ûÏÂÀý£© + </entry> + </row> + <row> + <entry><type>array</type></entry> + <entry>ÈÎºÎÆäËüÀàÐÍ</entry> + <entry><type>array</type> ×ÜÊǸü´ó</entry> + </row> + <row> + <entry><type>object</type></entry> + <entry>ÈÎºÎÆäËüÀàÐÍ</entry> + <entry><type>object</type> ×ÜÊǸü´ó</entry> + </row> + </tbody> + </tgroup> + </table> + <para> + <example> + <title>±ê×¼Êý×é±È½Ï´úÂë</title> + <programlisting role="php"> +<![CDATA[ +<?php +// Êý×éÊÇÓñê×¼±È½ÏÔËËã·ûÕâÑù±È½ÏµÄ +function standard_array_compare($op1, $op2) +{ + if (count($op1) < count($op2)) { + return -1; // $op1 < $op2 + } elseif (count($op1) > count($op2)) { + return 1; // $op1 > $op2 + } + foreach ($op1 as $key => $val) { + if (!array_key_exists($key, $op2)) { + return null; // uncomparable + } elseif ($val < $op2[$key]) { + return -1; + } elseif ($val > $op2[$key]) { + return 1; + } + } + return 0; // $op1 == $op2 +} +?> +]]> + </programlisting> + </example> + </para> + <para> - Çë²ÎÔĺ¯Êý <function>strcasecmp</function>¡¢<function>strcmp</function> ¼°¡°<link linkend="language.types">ÀàÐÍ</link>¡±µÄÓйØÕ½ڡ£ + ²Î¼û <function>strcasecmp</function>£¬<function>strcmp</function>£¬<link + linkend="language.operators.array">Êý×éÔËËã·û</link>ºÍ<link linkend="language.types">ÀàÐÍ</link>Ò»Õ¡£ </para> + + <sect2 id="language.operators.comparison.ternary"> + <title>ÈýÔªÔËËã·û</title> + <para> + ÁíÒ»¸öÌõ¼þÔËËã·ûÊÇ¡°?:¡±£¨»òÈýÔª£©ÔËËã·û ¡£ + <example> + <title>¸³Ä¬ÈÏÖµ</title> + <programlisting role="php"> + <![CDATA[ + <?php + // Example usage for: Ternary Operator + $action = (empty($_POST['action'])) ? 'default' : $_POST['action']; + + // The above is identical to this if/else statement + if (empty($_POST['action'])) { + $action = 'default'; + } else { + $action = $_POST['action']; + } + + ?> + ]]> + </programlisting> + </example> + ±í´ïʽ <literal>(expr1) ? (expr2) : (expr3)</literal> ÔÚ + <replaceable>expr1</replaceable> ÇóֵΪ &true; + ʱµÄֵΪ <replaceable>expr2</replaceable>£¬ÔÚ + <replaceable>expr1</replaceable> ÇóֵΪ &false; ʱµÄֵΪ + <replaceable>expr3</replaceable>¡£ + </para> + <note> + <simpara> + ×¢ÒâÈýÔªÔËËã·ûÊǸöÓï¾ä£¬Òò´ËÆäÇóÖµ²»ÊDZäÁ¿£¬¶øÊÇÓï¾äµÄ½á¹û¡£Èç¹ûÏëͨ¹ýÒýÓ÷µ»ØÒ»¸ö±äÁ¿Õâµã¾ÍºÜÖØÒª¡£ÔÚÒ»¸öͨ¹ýÒýÓ÷µ»ØµÄº¯ÊýÖÐÓï¾ä + <literal>return $var == 42 ? $a : $b;</literal> ½«²»Æð×÷Óã¬ÒÔºóµÄ PHP + °æ±¾»áΪ´Ë·¢³öÒ»Ìõ¾¯¸æ¡£ + </simpara> + </note> + </sect2> + </sect1> <sect1 id="language.operators.errorcontrol"> <title>´íÎó¿ØÖÆÔËËã·û</title> <simpara> - PHP Ö§³ÖÒ»¸ö´íÎó¿ØÖÆÔËËã·û£º@¡£µ±½«Æä·ÅÖÃÔÚÒ»¸ö PHP ±í´ïʽ֮ǰ£¬¸Ã±í´ïʽ¿ÉÄܲúÉúµÄÈκδíÎóÐÅÏ¢¶¼±»ºöÂÔµô¡£ + PHP Ö§³ÖÒ»¸ö´íÎó¿ØÖÆÔËËã·û£º@¡£µ±½«Æä·ÅÖÃÔÚÒ»¸ö PHP + ±í´ïʽ֮ǰ£¬¸Ã±í´ïʽ¿ÉÄܲúÉúµÄÈκδíÎóÐÅÏ¢¶¼±»ºöÂÔµô¡£ </simpara> <simpara> - Èç¹û¼¤»îÁË <link linkend="ini.track-errors">track_errors</link> ÌØÐÔ£¬±í´ïʽËù²úÉúµÄÈκδíÎóÐÅÏ¢¶¼±»´æ·ÅÔÚ±äÁ¿ <link linkend="reserved.variables.phperrormsg">$php_errormsg</link> ÖС£´Ë±äÁ¿ÔÚÿ´Î³ö´íʱ¶¼»á±»¸²¸Ç£¬ËùÒÔÈç¹ûÏëÓÃËüµÄ»°¾ÍÒª¾¡Ôç¼ì²é¡£ + Èç¹û¼¤»îÁË <link linkend="ini.track-errors"><option>track_errors</option></link> + ÌØÐÔ£¬±í´ïʽËù²úÉúµÄÈκδíÎóÐÅÏ¢¶¼±»´æ·ÅÔÚ±äÁ¿ + <link linkend="reserved.variables.phperrormsg">$php_errormsg</link> + ÖС£´Ë±äÁ¿ÔÚÿ´Î³ö´íʱ¶¼»á±»¸²¸Ç£¬ËùÒÔÈç¹ûÏëÓÃËüµÄ»°¾ÍÒª¾¡Ôç¼ì²é¡£ </simpara> <para> <informalexample> @@ -406,17 +659,16 @@ </para> <note> <simpara> - @ ÔËËã·ûÖ»¶Ô<link linkend="language.expressions">±í´ïʽ</link>ÓÐЧ¡£¶ÔÐÂÊÖÀ´ËµÒ»¸ö¼òµ¥µÄ¹æÔò¾ÍÊÇ£ºÈç¹ûÄãÄÜ´Óij´¦µÃµ½Öµ£¬Äã¾ÍÄÜÔÚËüÇ°Ãæ¼ÓÉÏ @ ÔËËã·û¡£ÀýÈ磬Äã¿ÉÒÔ°ÑËü·ÅÔÚ±äÁ¿£¬º¯ÊýºÍ <function>include</function> µ÷Ó㬳£Á¿£¬µÈµÈ֮ǰ¡£²»ÄܰÑËü·ÅÔÚº¯Êý»òÀàµÄ¶¨Òå֮ǰ£¬Ò²²»ÄÜÓÃÓÚÌõ¼þ½á¹¹ÀýÈç <literal>if</literal> ºÍ <literal>foreach</literal> µÈ¡£ + @ ÔËËã·ûÖ»¶Ô<link linkend="language.expressions">±í´ïʽ</link>ÓÐЧ¡£¶ÔÐÂÊÖÀ´ËµÒ»¸ö¼òµ¥µÄ¹æÔò¾ÍÊÇ£ºÈç¹ûÄÜ´Óij´¦µÃµ½Öµ£¬¾ÍÄÜÔÚËüÇ°Ãæ¼ÓÉÏ + @ ÔËËã·û¡£ÀýÈ磬¿ÉÒÔ°ÑËü·ÅÔÚ±äÁ¿£¬º¯ÊýºÍ <function>include</function> + µ÷Ó㬳£Á¿£¬µÈµÈ֮ǰ¡£²»ÄܰÑËü·ÅÔÚº¯Êý»òÀàµÄ¶¨Òå֮ǰ£¬Ò²²»ÄÜÓÃÓÚÌõ¼þ½á¹¹ÀýÈç + <literal>if</literal> ºÍ <literal>foreach</literal> µÈ¡£ </simpara> </note> <simpara> - ²Î¼û <function>error_reporting</function> ¼°ÊÖ²áÖС°<link linkend="ref.errorfunc">´íÎó´¦Àí¼°ÈÕÖ¾º¯Êý</link>¡±µÄÓйØÕ½ڡ£ + ²Î¼û <function>error_reporting</function> + ¼°ÊÖ²áÖÐ<link linkend="ref.errorfunc">´íÎó´¦Àí¼°ÈÕÖ¾º¯Êý</link>µÄÓйØÕ½ڡ£ </simpara> - <note> - <para> - ´íÎó¿ØÖÆÇ°×º¡°@¡±²»»áÆÁ±Î½âÎö´íÎóµÄÐÅÏ¢¡£ - </para> - </note> <warning> <para> ĿǰµÄ¡°@¡±´íÎó¿ØÖÆÔËËã·ûǰ׺ÉõÖÁʹµ¼Ö½ű¾ÖÕÖ¹µÄÑÏÖØ´íÎóµÄ´íÎ󱨸æÒ²Ê§Ð§¡£ÕâÒâζ×ÅÈç¹ûÄãÔÚij¸ö²»´æÔÚ»òÀàÐÍ´íÎóµÄº¯Êýµ÷ÓÃǰÓÃÁË¡°@¡±À´ÒÖÖÆ´íÎóÐÅÏ¢£¬Äǽű¾»áûÓÐÈκμ£ÏóÏÔʾÔÒò¶øËÀÔÚÄÇÀï¡£ @@ -450,12 +702,18 @@ </sect1> <sect1 id="language.operators.increment"> - <title>¼ÓÒ»£¯¼õÒ»ÔËËã·û</title> + <title>µÝÔö£¯µÝ¼õÔËËã·û</title> <para> - PHP Ö§³Ö C ·ç¸ñµÄǰ£¯ºó¼ÓÒ»Óë¼õÒ»ÔËËã·û¡£ + PHP Ö§³Ö C ·ç¸ñµÄǰ£¯ºóµÝÔöÓëµÝ¼õÔËËã·û¡£ </para> + <note> + <simpara> + µÝÔö£¯µÝ¼õÔËËã·û²»Ó°Ïì²¼¶ûÖµ¡£µÝ¼õ + &null; ֵҲûÓÐЧ¹û£¬µ«ÊǵÝÔö &null; µÄ½á¹ûÊÇ <literal>1</literal>¡£ + </simpara> + </note> <table> - <title>¼ÓÒ»£¯¼õÒ»ÔËËã·û</title> + <title>µÝÔö£¯µÝ¼õÔËËã·û</title> <tgroup cols="3"> <thead> <row> @@ -519,32 +777,37 @@ </informalexample> </para> <para> - ÔÚ´¦Àí×Ö·û±äÁ¿µÄËãÊýÔËËãʱ£¬PHP ÑØÏ®ÁË Perl µÄϰ¹ß£¬¶ø·Ç C µÄ¡£ÀýÈ磬ÔÚ Perl ÖÐ 'Z'+1 ½«µÃµ½ 'AA'£¬¶øÔÚ C ÖУ¬'Z'+1 ½«µÃµ½ '[' { ord('Z') == 90, ord('[') == 91 )¡£ + ÔÚ´¦Àí×Ö·û±äÁ¿µÄËãÊýÔËËãʱ£¬PHP ÑØÏ®ÁË Perl µÄϰ¹ß£¬¶ø·Ç C µÄ¡£ÀýÈ磬ÔÚ + Perl ÖÐ 'Z'+1 ½«µÃµ½ 'AA'£¬¶øÔÚ C ÖУ¬'Z'+1 ½«µÃµ½ + '['£¨ord('Z') == 90£¬ord('[') == 91£©¡£×¢Òâ×Ö·û±äÁ¿Ö»ÄܵÝÔö£¬²»Äܵݼõ¡£ <example> <title>Éæ¼°×Ö·û±äÁ¿µÄËãÊýÔËËã</title> <programlisting role="php"> <![CDATA[ <?php $i = 'W'; -for($n=0; $n<6; $n++) - echo ++$i . "\n"; - -/* - Produces the output similar to the following: - +for ($n=0; $n<6; $n++) { + echo ++$i . "\n"; +} +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ X Y Z AA AB AC - -*/ -?> ]]> - </programlisting> + </screen> </example> </para> + <para> + µÝÔö»òµÝ¼õ²¼¶ûֵûÓÐЧ¹û¡£ + </para> </sect1> <sect1 id="language.operators.logical"> @@ -610,41 +873,94 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $a = "Hello "; $b = $a . "World!"; // now $b contains "Hello World!" $a = "Hello "; $a .= "World!"; // now $a contains "Hello World!" +?> ]]> </programlisting> </informalexample> </para> <para> - Çë²ÎÔÄÊÖ²áÖС°<link linkend="language.types.string">×Ö·û´®ÀàÐÍ</link>¡±ºÍ¡°<link linkend="ref.strings">×Ö·û´®º¯Êý</link>¡±µÄÓйØÕ½ڡ£ + ²Î¼û<link linkend="language.types.string">×Ö·û´®ÀàÐÍ</link>ºÍ<link linkend="ref.strings">×Ö·û´®º¯Êý</link>Õ½ڡ£ </para> </sect1> <sect1 id="language.operators.array"> <title>Êý×éÔËËã·û</title> - <simpara> - PHP ½öÓеÄÒ»¸öÊý×éÔËËã·ûÊÇ <literal>+</literal> ÔËËã·û¡£Ëü°ÑÓұߵÄÊý×鸽¼Óµ½×ó±ßµÄÊý×éºó£¬µ«ÊÇÖØ¸´µÄ¼üÖµ²»»á±»¸²¸Ç¡£ - </simpara> + <table> + <title>Êý×éÔËËã·û</title> + <tgroup cols="3"> + <thead> + <row> + <entry>Àý×Ó</entry> + <entry>Ãû³Æ</entry> + <entry>½á¹û</entry> + </row> + </thead> + <tbody> + <row> + <entry>$a + $b</entry> + <entry>ÁªºÏ</entry> + <entry>$a ºÍ $b µÄÁªºÏ¡£</entry> + </row> + <row> + <entry>$a == $b</entry> + <entry>ÏàµÈ</entry> + <entry>Èç¹û $a ºÍ $b ¾ßÓÐÏàͬµÄ¼ü£¯Öµ¶ÔÔòΪ &true;¡£</entry> + </row> + <row> + <entry>$a === $b</entry> + <entry>È«µÈ</entry> + <entry>Èç¹û $a ºÍ $b ¾ßÓÐÏàͬµÄ¼ü£¯Öµ¶Ô²¢ÇÒ˳ÐòºÍÀàÐͶ¼ÏàͬÔòΪ &true;¡£</entry> + </row> + <row> + <entry>$a != $b</entry> + <entry>²»µÈ</entry> + <entry>Èç¹û $a ²»µÈÓÚ $b ÔòΪ &true;¡£</entry> + </row> + <row> + <entry>$a <> $b</entry> + <entry>²»µÈ</entry> + <entry>Èç¹û $a ²»µÈÓÚ $b ÔòΪ &true;¡£</entry> + </row> + <row> + <entry>$a !== $b</entry> + <entry>²»È«µÈ</entry> + <entry>Èç¹û $a ²»È«µÈÓÚ $b ÔòΪ &true;¡£</entry> + </row> + </tbody> + </tgroup> + </table> + <para> + <literal>+</literal> ÔËËã·û°ÑÓұߵÄÊý×鸽¼Óµ½×ó±ßµÄÊý×éºóÃæ£¬µ«ÊÇÖØ¸´µÄ¼üÖµ²»»á±»¸²¸Ç¡£ + </para> <para> <informalexample> <programlisting role="php"> <![CDATA[ +<?php $a = array("a" => "apple", "b" => "banana"); -$b = array("a" =>"pear", "b" => "strawberry", "c" => "cherry"); +$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry"); -$c = $a + $b; +$c = $a + $b; // Union of $a and $b +echo "Union of \$a and \$b: \n"; +var_dump($c); +$c = $b + $a; // Union of $b and $a +echo "Union of \$b and \$a: \n"; var_dump($c); +?> ]]> </programlisting> </informalexample> Ö´Ðк󣬴˽ű¾»áÏÔʾ£º <screen role="php"> <![CDATA[ +Union of $a and $b: array(3) { ["a"]=> string(5) "apple" @@ -653,12 +969,82 @@ ["c"]=> string(6) "cherry" } - +Union of $b and $a: +array(3) { + ["a"]=> + string(4) "pear" + ["b"]=> + string(10) "strawberry" + ["c"]=> + string(6) "cherry" +} ]]> </screen> </para> <para> - Çë²ÎÔÄÊÖ²áÖС°<link linkend="language.types.array">Êý×éÀàÐÍ</link>¡±ºÍ¡°<link linkend="ref.array">Êý×麯Êý</link>¡±µÄÓйØÕ½ڡ£ + Êý×éÖеĵ¥ÔªÈç¹û¾ßÓÐÏàͬµÄ¼üÃûºÍÖµÔò±È½ÏʱÏàµÈ¡£ + </para> + <para> + <example> + <title>±È½ÏÊý×é</title> + <programlisting role="php"> +<![CDATA[ +<?php +$a = array("apple", "banana"); +$b = array(1 => "banana", "0" => "apple"); + +var_dump($a == $b); // bool(true) +var_dump($a === $b); // bool(false) +?> +]]> + </programlisting> + </example> + </para> + <para> + ²Î¼û<link linkend="language.types.array">Êý×éÀàÐÍ</link>ºÍ<link + linkend="ref.array">Êý×麯Êý</link>Õ½ڡ£ + </para> + </sect1> + + <sect1 id="language.operators.type"> + <title>ÀàÐÍÔËËã·û</title> + <para> + PHP ÓÐÒ»¸öÀàÐÍÔËËã·û£º<literal>instanceof</literal>¡£<literal>instanceof</literal> + ÓÃÀ´²â¶¨Ò»¸ö¸ø¶¨µÄ¶ÔÏóÊÇ·ñÀ´×ÔÖ¸¶¨µÄ<link + linkend="language.oop">¶ÔÏóÀà</link>¡£ + </para> + <simpara> + <literal>instanceof</literal> ÔËËã·ûÊÇ PHP 5 Òý½øµÄ¡£ÔÚ´Ë֮ǰÓà + <function>is_a</function>£¬µ«ÊÇ + <function>is_a</function> ÒѾ¹ýʱÁË£¬×îºÃÓà <literal>instanceof</literal>¡£ + </simpara> + <informalexample> + <programlisting> +<![CDATA[ +<?php +class A { } +class B { } + +$thing = new A; + +if ($thing instanceof A) { + echo 'A'; +} +if ($thing instanceof B) { + echo 'B'; +} +?> +]]> + </programlisting> + <simpara> + ÓÉÓÚ <varname>$thing</varname> ÊÇÀàÐÍ A µÄÒ»¸ö <type>object</type>£¬¶ø²»ÊÇ + B µÄ£¬Ö»ÓкÍÀàÐÍ A Ïà·ûºÏµÄ³ÌÐò¿é±»ÔËÐУº + </simpara> + <screen>A</screen> + </informalexample> + <para> + ²Î¼û <function>get_class</function> ºÍ + <function>is_a</function>¡£ </para> </sect1> </chapter>
http://cvs.php.net/diff.php/phpdoc-zh/language/operators.xml?r1=1.10&r2=1.11&ty=u Index: phpdoc-zh/language/operators.xml diff -u phpdoc-zh/language/operators.xml:1.10 phpdoc-zh/language/operators.xml:1.11 --- phpdoc-zh/language/operators.xml:1.10 Sat Aug 16 23:32:29 2003 +++ phpdoc-zh/language/operators.xml Tue Aug 16 11:25:54 2005 @@ -1,20 +1,33 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.10 $ --> +<!-- $Revision: 1.11 $ --> <!-- $Author: dallas $ --> -<!-- EN-Revision: 1.53 Maintainer: dallas Status: ready --> +<!-- EN-Revision: 1.94 Maintainer: dallas Status: ready --> <chapter id="language.operators"> <title>ÔËËã·û</title> <simpara> + ÔËËã·ûÊÇ¿ÉÒÔͨ¹ý¸ø³öµÄÒ»»ò¶à¸öÖµ£¨Óñà³ÌÐл°À´Ëµ£¬±í´ïʽ£©À´²úÉúÁíÒ»¸öÖµ£¨Òò¶øÕû¸ö½á¹¹³ÉΪһ¸ö±í´ïʽ£©µÄ¶«Î÷¡£ËùÒÔ¿ÉÒÔÈÏΪº¯Êý»òÈκλ᷵»ØÒ»¸öÖµ£¨ÀýÈç + print£©µÄ½á¹¹ÊÇÔËËã·û£¬¶øÄÇЩûÓзµ»ØÖµµÄ£¨ÀýÈç echo£©ÊDZðµÄ¶«Î÷¡£ </simpara> + <para> + ÓÐÈýÖÖÀàÐ͵ÄÔËËã·û¡£µÚÒ»ÖÖÊÇÒ»ÔªÔËËã·û£¬Ö»ÔËËãÒ»¸öÖµ£¬ÀýÈç + !£¨È¡·´ÔËËã·û£©»ò ++£¨¼ÓÒ»ÔËËã·û£©¡£µÚ¶þÖÖÊÇÓÐÏÞ¶þÔªÔËËã·û£¬PHP + Ö§³ÖµÄ´ó¶àÊýÔËËã·û¶¼ÊÇÕâÖÖ¡£Áбí¼ûϽÚ<link + linkend="language.operators.precedence">ÔËËã·ûÓÅÏȼ¶</link>¡£ + </para> + <para> + µÚÈýÖÖÊÇÈýÔªÔËËã·û£º?:¡£ËüÓ¦ÓÃÀ´¸ù¾ÝÒ»¸ö±í´ïʽÔÚÁíÁ½¸ö±í´ïʽÖÐÑ¡ÔñÒ»¸ö£¬¶ø²»ÊÇÓÃÀ´Ñ¡ÔñÔÚÁ½¸öÓï¾ä»òÕß³ÌÐò·ÏßÖÐÑ¡Ôñ¡£°ÑÕû¸öÈýÔª±í´ïʽ·ÅÔÚÀ©ºÅÀïÊǸöºÜºÃµÄÖ÷Òâ¡£ + </para> <sect1 id="language.operators.precedence"> <title>ÔËËã·ûÓÅÏȼ¶</title> <para> ÔËËã·ûÓÅÏȼ¶Ö¸¶¨ÁËÁ½¸ö±í´ïʽ°ó¶¨µÃÓжࡰ½ôÃÜ¡±¡£ÀýÈ磬±í´ïʽ <literal>1 + 5 * - 3</literal> µÄ½á¹ûÊÇ <literal>16</literal> ¶ø²»ÊÇ <literal>18</literal> ÊÇÒòΪ³ËºÅ£¨¡°*¡±£©µÄÓÅÏȼ¶±È¼ÓºÅ£¨¡°+¡±£©¸ß¡£±ØÒªÊ±¿ÉÒÔÓÃÀ¨ºÅÀ´Ç¿ÖƸıäÓÅÏȼ¶¡£ÀýÈ磺<literal>(1 + 5) * 3</literal> µÄֵΪ <literal>18</literal>¡£ + 3</literal> µÄ½á¹ûÊÇ <literal>16</literal> ¶ø²»ÊÇ <literal>18</literal> + ÊÇÒòΪ³ËºÅ£¨¡°*¡±£©µÄÓÅÏȼ¶±È¼ÓºÅ£¨¡°+¡±£©¸ß¡£±ØÒªÊ±¿ÉÒÔÓÃÀ¨ºÅÀ´Ç¿ÖƸıäÓÅÏȼ¶¡£ÀýÈ磺<literal>(1 + + 5) * 3</literal> µÄֵΪ <literal>18</literal>¡£Èç¹ûÔËËã·ûÓÅÏȼ¶Ïàͬ£¬ÔòʹÓôÓ×óµ½ÓÒµÄ×óÁªË³Ðò¡£ </para> <para> - ϱí´ÓµÍµ½¸ßÁгöÁËÔËËã·ûµÄÓÅÏȼ¶¡£ + ϱí´Ó¸ßµ½µÍÁгöÁËÔËËã·ûµÄÓÅÏȼ¶¡£Í¬Ò»ÐÐÖеÄÔËËã·û¾ßÓÐÏàͬÓÅÏȼ¶£¬´ËʱËüÃǵĽáºÏ·½Ïò¾ö¶¨Çóֵ˳Ðò¡£ <table> <title>ÔËËã·ûÓÅÏȼ¶</title> <tgroup cols="2"> @@ -22,95 +35,169 @@ <row> <entry>½áºÏ·½Ïò</entry> <entry>ÔËËã·û</entry> + <entry>¸½¼ÓÐÅÏ¢</entry> </row> </thead> <tbody> <row> - <entry>×ó</entry> - <entry>,</entry> + <entry>·Ç½áºÏ</entry> + <entry>new</entry> + <entry><link linkend="language.oop5.basic.new">new</link></entry> </row> <row> - <entry>×ó</entry> - <entry>or</entry> + <entry>ÓÒ</entry> + <entry>[</entry> + <entry><function>array</function></entry> + </row> + <row> + <entry>·Ç½áºÏ</entry> + <entry>++ --</entry> + <entry> + <link linkend="language.operators.increment">µÝÔö£¯µÝ¼õÔËËã·û</link> + </entry> + </row> + <row> + <entry>·Ç½áºÏ</entry> + <entry>! ~ - (int) (float) (string) (array) (object) @</entry> + <entry> + <link linkend="language.types">ÀàÐÍ</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>xor</entry> + <entry>* / %</entry> + <entry> + <link linkend="language.operators.arithmetic">ËãÊýÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>and</entry> + <entry>+ - .</entry> + <entry> + <link linkend="language.operators.arithmetic">ËãÊýÔËËã·û</link>&listendand; + <link linkend="language.operators.string">×Ö·û´®ÔËËã·û</link></entry> </row> <row> - <entry>ÓÒ</entry> - <entry>print</entry> + <entry>×ó</entry> + <entry><< >></entry> + <entry> + <link linkend="language.operators.bitwise">λÔËËã·û</link> + </entry> </row> <row> - <entry>ÓÒ</entry> + <entry>·Ç½áºÏ</entry> + <entry>< <= > >=</entry> <entry> - = += -= *= /= .= %= &= |= ^= ~= <<= >>= + <link linkend="language.operators.comparison">±È½ÏÔËËã·û</link> </entry> </row> <row> - <entry>×ó</entry> - <entry>? :</entry> + <entry>·Ç½áºÏ</entry> + <entry>== != === !==</entry> + <entry> + <link linkend="language.operators.comparison">±È½ÏÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>||</entry> + <entry>&</entry> + <entry> + <link linkend="language.operators.bitwise">λÔËËã·û</link>&listendand; + <link linkend="language.references">ÒýÓÃ</link></entry> </row> <row> <entry>×ó</entry> - <entry>&&</entry> + <entry>^</entry> + <entry> + <link linkend="language.operators.bitwise">λÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> <entry>|</entry> + <entry> + <link linkend="language.operators.bitwise">λÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>^</entry> + <entry>&&</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>&</entry> + <entry>||</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> - <entry>ÎÞ</entry> - <entry>== != === !==</entry> + <entry>×ó</entry> + <entry>? :</entry> + <entry> + <link linkend="language.operators.comparison.ternary">ÈýÔªÔËËã·û</link> + </entry> </row> <row> - <entry>ÎÞ</entry> - <entry>< <= > >=</entry> + <entry>ÓÒ</entry> + <entry> + = += -= *= /= .= %= &= |= ^= <<= >>= + </entry> + <entry> + <link linkend="language.operators.assignment">¸³ÖµÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry><< >></entry> + <entry>and</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>+ - .</entry> + <entry>xor</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> <entry>×ó</entry> - <entry>* / %</entry> - </row> - <row> - <entry>ÓÒ</entry> - <entry>! ~ ++ -- (int) (float) (string) (array) (object) @</entry> - </row> - <row> - <entry>ÓÒ</entry> - <entry>[</entry> + <entry>or</entry> + <entry> + <link linkend="language.operators.logical">Âß¼ÔËËã·û</link> + </entry> </row> <row> - <entry>ÎÞ</entry> - <entry>new</entry> + <entry>×ó</entry> + <entry>,</entry> + <entry>¶à´¦Óõ½</entry> </row> </tbody> </tgroup> </table> </para> + <para> + ×óÁª±íʾ±í´ïʽ´Ó×óÏòÓÒÇóÖµ£¬ÓÒÁªÏà·´¡£ + <example> + <title>½áºÏ·½Ïò</title> + <programlisting role="php"> +<![CDATA[ +<?php +$a = 3 * 3 % 5; // (3 * 3) % 5 = 4 +$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2 + +$a = 1; +$b = 2; +$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5 +?> +]]> + </programlisting> + </example> + ʹÓÃÀ¨ºÅ¿ÉÒÔÔöÇ¿´úÂëµÄ¿É¶ÁÐÔ¡£ + </para> <note> <para> ¾¡¹Ü <literal>!</literal> ±È <literal>=</literal> µÄÓÅÏȼ¶¸ß£¬PHP @@ -137,6 +224,11 @@ </thead> <tbody> <row> + <entry>-$a</entry> + <entry>È¡·´</entry> + <entry>$a µÄ¸ºÖµ¡£</entry> + </row> + <row> <entry>$a + $b</entry> <entry>¼Ó·¨</entry> <entry>$a ºÍ $b µÄºÍ¡£</entry> @@ -167,8 +259,14 @@ <simpara> ³ýºÅ£¨¡°/¡±£©×ÜÊÇ·µ»Ø¸¡µãÊý£¬¼´Ê¹Á½¸öÔËËãÊýÊÇÕûÊý£¨»òÓÉ×Ö·û´®×ª»»³ÉµÄÕûÊý£©Ò²ÊÇÕâÑù¡£ </simpara> + <note> + <simpara> + ȡģ <literal>$a % $b</literal> ÔÚ <literal>$a</literal> + Ϊ¸ºÖµÊ±µÄ½á¹ûÒ²ÊǸºÖµ¡£ + </simpara> + </note> <simpara> - Çë²éÔÄÊֲᡰ<link linkend="ref.math">Êýѧº¯Êý</link>¡±ÓйØÕ½ڡ£ + ²Î¼ûÊÖ²áÖеÄ<link linkend="ref.math">Êýѧº¯Êý¿â</link>¡£ </simpara> <!-- @@ -193,27 +291,38 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php + $a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4. + +?> ]]> </programlisting> </informalexample> </para> <para> - ÔÚ»ù±¾¸³ÖµÔËËã·ûÖ®Í⣬»¹ÓÐÊʺÏÓÚËùÓжþÔªËãÊõºÍ×Ö·û´®ÔËËã·ûµÄ¡°×éºÍÔËËã·û¡±£¬Õâ¿ÉÒÔÈÃÄãÔÚÒ»¸ö±í´ïʽÖÐʹÓÃËüµÄÖµ²¢°Ñ±í´ïʽµÄ½á¹û¸³¸øËü£¬ÀýÈ磺 + ÔÚ»ù±¾¸³ÖµÔËËã·ûÖ®Í⣬»¹ÓÐÊʺÏÓÚËùÓÐ<link + linkend="language.operators">¶þÔªËãÊõ</link>ºÍ×Ö·û´®ÔËËã·ûµÄ¡°×éºÍÔËËã·û¡±£¬Õâ¿ÉÒÔÈÃÄãÔÚÒ»¸ö±í´ïʽÖÐʹÓÃËüµÄÖµ²¢°Ñ±í´ïʽµÄ½á¹û¸³¸øËü£¬ÀýÈ磺 <informalexample> <programlisting role="php"> <![CDATA[ +<?php + $a = 3; $a += 5; // sets $a to 8, as if we had said: $a = $a + 5; $b = "Hello "; $b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!"; + +?> ]]> </programlisting> </informalexample> </para> <para> - ×¢Ò⸳ֵÔËË㽫ԱäÁ¿µÄÖµ¿½±´µ½Ð±äÁ¿ÖУ¨´«Öµ¸³Öµ£©£¬ËùÒԸıäÆäÖÐÒ»¸ö²¢²»Ó°ÏìÁíÒ»¸ö¡£ÕâÒ²ÊʺÏÓÚÄãÔÚÔÚ½ôÃÜÑ»·Öп½±´Ò»Ð©ÖµÀýÈç´óÊýÖµ¡£PHP 4 Ö§³ÖÒýÓø³Öµ£¬Óà <computeroutput>$var = - &$othervar;</computeroutput> Óï·¨£¬µ«ÔÚ PHP 3 Öв»¿ÉÄÜÕâÑù×ö¡£¡°ÒýÓø³Öµ¡±Òâζ×ÅÁ½¸ö±äÁ¿¶¼Ö¸Ïòͬһ¸öÊý¾Ý£¬Ã»ÓÐÈκÎÊý¾ÝµÄ¿½±´¡£ÓйØÒýÓõĸü¶àÐÅÏ¢¼û<link + ×¢Ò⸳ֵÔËË㽫ԱäÁ¿µÄÖµ¿½±´µ½Ð±äÁ¿ÖУ¨´«Öµ¸³Öµ£©£¬ËùÒԸıäÆäÖÐÒ»¸ö²¢²»Ó°ÏìÁíÒ»¸ö¡£ÕâÒ²ÊʺÏÓÚÄãÔÚÔÚ½ôÃÜÑ»·Öп½±´Ò»Ð©ÖµÀýÈç´óÊýÖµ¡£×Ô + PHP 4 ÆðÖ§³ÖÒýÓø³Öµ£¬Óà <computeroutput>$var = + &$othervar;</computeroutput> Óï·¨£¬µ«ÔÚ PHP 3 + Öв»¿ÉÄÜÕâÑù×ö¡£¡°ÒýÓø³Öµ¡±Òâζ×ÅÁ½¸ö±äÁ¿¶¼Ö¸Ïòͬһ¸öÊý¾Ý£¬Ã»ÓÐÈκÎÊý¾ÝµÄ¿½±´¡£ÓйØÒýÓõĸü¶àÐÅÏ¢¼û<link linkend="language.references">ÒýÓõÄ˵Ã÷</link>¡£ </para> </sect1> @@ -221,19 +330,20 @@ <sect1 id="language.operators.bitwise"> <title>λÔËËã·û</title> <simpara> - λÔËËã·ûÔÊÐí¶ÔÕûÐÍÊýÖÐÖ¸¶¨µÄλ½øÐÐÖÃλ¡£Èç¹û×óÓÒ²ÎÊý¶¼ÊÇ×Ö·û´®£¬ÔòλÔËËã·û½«²Ù×÷Õâ¸ö×Ö·û´®ÖеÄ×Ö·û¡£ + λÔËËã·ûÔÊÐí¶ÔÕûÐÍÊýÖÐÖ¸¶¨µÄλ½øÐÐÖÃλ¡£Èç¹û×óÓÒ²ÎÊý¶¼ÊÇ×Ö·û´®£¬ÔòλÔËËã·û½«²Ù×÷×Ö·ûµÄ + ASCII Öµ¡£ </simpara> <para> <informalexample> <programlisting role="php"> <![CDATA[ <?php - echo 12 ^ 9; // Outputs '5' +echo 12 ^ 9; // Outputs '5' - echo "12" ^ "9"; // Outputs the Backspace character (ascii 8) +echo "12" ^ "9"; // Outputs the Backspace character (ascii 8) // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8 - echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0 +echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0 // 'a' ^ 'e' = #4 ?> ]]> @@ -285,6 +395,11 @@ </tbody> </tgroup> </table> + <warning> + <para> + ÔÚ 32 λϵͳÉϲ»ÒªÓÒÒÆ³¬¹ý 32 λ¡£²»ÒªÔÚ½á¹û¿ÉÄܳ¬¹ý 32 λµÄÇé¿öÏÂ×óÒÆ¡£ + </para> + </warning> </sect1> <sect1 id="language.operators.comparison"> @@ -312,7 +427,7 @@ <row> <entry>$a === $b</entry> <entry>È«µÈ</entry> - <entry>&true;£¬Èç¹û $a µÈÓÚ $b£¬²¢ÇÒËüÃǵÄÀàÐÍÒ²Ïàͬ¡£(PHP 4 only)</entry> + <entry>&true;£¬Èç¹û $a µÈÓÚ $b£¬²¢ÇÒËüÃǵÄÀàÐÍÒ²Ïàͬ¡££¨PHP 4 Òý½ø£©</entry> </row> <row> <entry>$a != $b</entry> @@ -353,38 +468,176 @@ </tgroup> </table> <para> - ÁíÍâÒ»¸öÌõ¼þÔËËã·ûÊÇ¡°?:¡±£¨»òÈýÔª£©ÔËËã·û£¬ËüºÍ C ÒÔ¼°ºÜ¶àÆäËüÓïÑԵIJÙ×÷Ò»Ñù¡£ + Èç¹û±È½ÏÒ»¸öÕûÊýºÍ×Ö·û´®£¬Ôò×Ö·û´®»á±»<link + linkend="language.types.string.conversion">ת»»ÎªÕûÊý</link>¡£Èç¹û±È½ÏÁ½¸öÊý×Ö×Ö·û´®£¬Ôò×÷ΪÕûÊý±È½Ï¡£´Ë¹æÔòÒ²ÊÊÓÃÓÚ + <link linkend="control-structures.switch">switch</link> Óï¾ä¡£ <informalexample> <programlisting role="php"> <![CDATA[ <?php -// Example usage for: Ternary Operator -$action = (empty($_POST['action'])) ? 'default' : $_POST['action']; +var_dump(0 == "a"); // 0 == 0 -> true +var_dump("1" == "01"); // 1 == 1 -> true -// The above is identical to this if/else statement -if (empty($_POST['action'])) { - $action = 'default'; -} else { - $action = $_POST['action']; +switch ("a") { +case 0: + echo "0"; + break; +case "a": // never reached because "a" is already matched with 0 + echo "a"; + break; } ?> ]]> </programlisting> </informalexample> - ¶ÔÓÚ±í´ïʽ <literal>(expr1) ? (expr2) : (expr3)</literal>£¬Èç¹û <replaceable>expr1</replaceable> µÄֵΪ &true;£¬Ôò´Ë±í´ïʽµÄֵΪ <replaceable>expr2</replaceable>£¬Èç¹û <replaceable>expr1</replaceable> µÄֵΪ &false;£¬Ôò´Ë±í´ïʽµÄֵΪ <replaceable>expr3</replaceable>¡£ </para> + + <para> + Èç¹ûÔËËãÊýµÄÀàÐͲ»Í¬£¬Ôò¸ù¾Ýϱí±È½Ï£¨°´Ë³Ðò£©¡£ + </para> + <table> + <title>±È½Ï²»Í¬ÀàÐÍ</title> + <tgroup cols="3"> + <thead> + <row> + <entry>ÔËËãÊý 1 ÀàÐÍ</entry> + <entry>ÔËËãÊý 1 ÀàÐÍ</entry> + <entry>½á¹û</entry> + </row> + </thead> + <tbody> + <row> + <entry><type>null</type> »ò <type>string</type></entry> + <entry><type>string</type></entry> + <entry>½« &null; ת»»Îª ""£¬½øÐÐÊý×Ö»ò´Ê»ã±È½Ï</entry> + </row> + <row> + <entry><type>bool</type> »ò <type>null</type></entry> + <entry>ÈÎºÎÆäËüÀàÐÍ</entry> + <entry>ת»»Îª <type>bool</type>£¬&false; < &true;</entry> + </row> + <row> + <entry><type>object</type></entry> + <entry><type>object</type></entry> + <entry> + ÄÚÖÃÀà¿ÉÒÔ¶¨Òå×Ô¼ºµÄ±È½Ï£¬²»Í¬À಻ÄܱȽϣ¬ÏàͬÀàºÍÊý×éͬÑù·½Ê½±È½ÏÊôÐÔ£¨PHP + 4 ÖУ©£¬PHP 5 ÓÐÆä×Ô¼ºµÄ<link + linkend="language.oop5.object-comparison">˵Ã÷</link> + </entry> + </row> + <row> + <entry><type>string</type>£¬<type>resource</type> »ò <type>number</type></entry> + <entry><type>string</type>£¬<type>resource</type> »ò <type>number</type></entry> + <entry>½«×Ö·û´®ºÍ×ÊԴת»»³ÉÊý×Ö£¬°´ÆÕͨÊýѧ±È½Ï</entry> + </row> + <row> + <entry><type>array</type></entry> + <entry><type>array</type></entry> + <entry> + ¾ßÓнÏÉÙ³ÉÔ±µÄÊý×é½ÏС£¬Èç¹ûÔËËãÊý 1 Öеļü²»´æÔÚÓÚÔËËãÊý + 2 ÖÐÔòÊý×éÎÞ·¨±È½Ï£¬·ñÔò°¤¸öÖµ±È½Ï£¨¼ûÏÂÀý£© + </entry> + </row> + <row> + <entry><type>array</type></entry> + <entry>ÈÎºÎÆäËüÀàÐÍ</entry> + <entry><type>array</type> ×ÜÊǸü´ó</entry> + </row> + <row> + <entry><type>object</type></entry> + <entry>ÈÎºÎÆäËüÀàÐÍ</entry> + <entry><type>object</type> ×ÜÊǸü´ó</entry> + </row> + </tbody> + </tgroup> + </table> + <para> + <example> + <title>±ê×¼Êý×é±È½Ï´úÂë</title> + <programlisting role="php"> +<![CDATA[ +<?php +// Êý×éÊÇÓñê×¼±È½ÏÔËËã·ûÕâÑù±È½ÏµÄ +function standard_array_compare($op1, $op2) +{ + if (count($op1) < count($op2)) { + return -1; // $op1 < $op2 + } elseif (count($op1) > count($op2)) { + return 1; // $op1 > $op2 + } + foreach ($op1 as $key => $val) { + if (!array_key_exists($key, $op2)) { + return null; // uncomparable + } elseif ($val < $op2[$key]) { + return -1; + } elseif ($val > $op2[$key]) { + return 1; + } + } + return 0; // $op1 == $op2 +} +?> +]]> + </programlisting> + </example> + </para> + <para> - Çë²ÎÔĺ¯Êý <function>strcasecmp</function>¡¢<function>strcmp</function> ¼°¡°<link linkend="language.types">ÀàÐÍ</link>¡±µÄÓйØÕ½ڡ£ + ²Î¼û <function>strcasecmp</function>£¬<function>strcmp</function>£¬<link + linkend="language.operators.array">Êý×éÔËËã·û</link>ºÍ<link linkend="language.types">ÀàÐÍ</link>Ò»Õ¡£ </para> + + <sect2 id="language.operators.comparison.ternary"> + <title>ÈýÔªÔËËã·û</title> + <para> + ÁíÒ»¸öÌõ¼þÔËËã·ûÊÇ¡°?:¡±£¨»òÈýÔª£©ÔËËã·û ¡£ + <example> + <title>¸³Ä¬ÈÏÖµ</title> + <programlisting role="php"> + <![CDATA[ + <?php + // Example usage for: Ternary Operator + $action = (empty($_POST['action'])) ? 'default' : $_POST['action']; + + // The above is identical to this if/else statement + if (empty($_POST['action'])) { + $action = 'default'; + } else { + $action = $_POST['action']; + } + + ?> + ]]> + </programlisting> + </example> + ±í´ïʽ <literal>(expr1) ? (expr2) : (expr3)</literal> ÔÚ + <replaceable>expr1</replaceable> ÇóֵΪ &true; + ʱµÄֵΪ <replaceable>expr2</replaceable>£¬ÔÚ + <replaceable>expr1</replaceable> ÇóֵΪ &false; ʱµÄֵΪ + <replaceable>expr3</replaceable>¡£ + </para> + <note> + <simpara> + ×¢ÒâÈýÔªÔËËã·ûÊǸöÓï¾ä£¬Òò´ËÆäÇóÖµ²»ÊDZäÁ¿£¬¶øÊÇÓï¾äµÄ½á¹û¡£Èç¹ûÏëͨ¹ýÒýÓ÷µ»ØÒ»¸ö±äÁ¿Õâµã¾ÍºÜÖØÒª¡£ÔÚÒ»¸öͨ¹ýÒýÓ÷µ»ØµÄº¯ÊýÖÐÓï¾ä + <literal>return $var == 42 ? $a : $b;</literal> ½«²»Æð×÷Óã¬ÒÔºóµÄ PHP + °æ±¾»áΪ´Ë·¢³öÒ»Ìõ¾¯¸æ¡£ + </simpara> + </note> + </sect2> + </sect1> <sect1 id="language.operators.errorcontrol"> <title>´íÎó¿ØÖÆÔËËã·û</title> <simpara> - PHP Ö§³ÖÒ»¸ö´íÎó¿ØÖÆÔËËã·û£º@¡£µ±½«Æä·ÅÖÃÔÚÒ»¸ö PHP ±í´ïʽ֮ǰ£¬¸Ã±í´ïʽ¿ÉÄܲúÉúµÄÈκδíÎóÐÅÏ¢¶¼±»ºöÂÔµô¡£ + PHP Ö§³ÖÒ»¸ö´íÎó¿ØÖÆÔËËã·û£º@¡£µ±½«Æä·ÅÖÃÔÚÒ»¸ö PHP + ±í´ïʽ֮ǰ£¬¸Ã±í´ïʽ¿ÉÄܲúÉúµÄÈκδíÎóÐÅÏ¢¶¼±»ºöÂÔµô¡£ </simpara> <simpara> - Èç¹û¼¤»îÁË <link linkend="ini.track-errors">track_errors</link> ÌØÐÔ£¬±í´ïʽËù²úÉúµÄÈκδíÎóÐÅÏ¢¶¼±»´æ·ÅÔÚ±äÁ¿ <link linkend="reserved.variables.phperrormsg">$php_errormsg</link> ÖС£´Ë±äÁ¿ÔÚÿ´Î³ö´íʱ¶¼»á±»¸²¸Ç£¬ËùÒÔÈç¹ûÏëÓÃËüµÄ»°¾ÍÒª¾¡Ôç¼ì²é¡£ + Èç¹û¼¤»îÁË <link linkend="ini.track-errors"><option>track_errors</option></link> + ÌØÐÔ£¬±í´ïʽËù²úÉúµÄÈκδíÎóÐÅÏ¢¶¼±»´æ·ÅÔÚ±äÁ¿ + <link linkend="reserved.variables.phperrormsg">$php_errormsg</link> + ÖС£´Ë±äÁ¿ÔÚÿ´Î³ö´íʱ¶¼»á±»¸²¸Ç£¬ËùÒÔÈç¹ûÏëÓÃËüµÄ»°¾ÍÒª¾¡Ôç¼ì²é¡£ </simpara> <para> <informalexample> @@ -406,17 +659,16 @@ </para> <note> <simpara> - @ ÔËËã·ûÖ»¶Ô<link linkend="language.expressions">±í´ïʽ</link>ÓÐЧ¡£¶ÔÐÂÊÖÀ´ËµÒ»¸ö¼òµ¥µÄ¹æÔò¾ÍÊÇ£ºÈç¹ûÄãÄÜ´Óij´¦µÃµ½Öµ£¬Äã¾ÍÄÜÔÚËüÇ°Ãæ¼ÓÉÏ @ ÔËËã·û¡£ÀýÈ磬Äã¿ÉÒÔ°ÑËü·ÅÔÚ±äÁ¿£¬º¯ÊýºÍ <function>include</function> µ÷Ó㬳£Á¿£¬µÈµÈ֮ǰ¡£²»ÄܰÑËü·ÅÔÚº¯Êý»òÀàµÄ¶¨Òå֮ǰ£¬Ò²²»ÄÜÓÃÓÚÌõ¼þ½á¹¹ÀýÈç <literal>if</literal> ºÍ <literal>foreach</literal> µÈ¡£ + @ ÔËËã·ûÖ»¶Ô<link linkend="language.expressions">±í´ïʽ</link>ÓÐЧ¡£¶ÔÐÂÊÖÀ´ËµÒ»¸ö¼òµ¥µÄ¹æÔò¾ÍÊÇ£ºÈç¹ûÄÜ´Óij´¦µÃµ½Öµ£¬¾ÍÄÜÔÚËüÇ°Ãæ¼ÓÉÏ + @ ÔËËã·û¡£ÀýÈ磬¿ÉÒÔ°ÑËü·ÅÔÚ±äÁ¿£¬º¯ÊýºÍ <function>include</function> + µ÷Ó㬳£Á¿£¬µÈµÈ֮ǰ¡£²»ÄܰÑËü·ÅÔÚº¯Êý»òÀàµÄ¶¨Òå֮ǰ£¬Ò²²»ÄÜÓÃÓÚÌõ¼þ½á¹¹ÀýÈç + <literal>if</literal> ºÍ <literal>foreach</literal> µÈ¡£ </simpara> </note> <simpara> - ²Î¼û <function>error_reporting</function> ¼°ÊÖ²áÖС°<link linkend="ref.errorfunc">´íÎó´¦Àí¼°ÈÕÖ¾º¯Êý</link>¡±µÄÓйØÕ½ڡ£ + ²Î¼û <function>error_reporting</function> + ¼°ÊÖ²áÖÐ<link linkend="ref.errorfunc">´íÎó´¦Àí¼°ÈÕÖ¾º¯Êý</link>µÄÓйØÕ½ڡ£ </simpara> - <note> - <para> - ´íÎó¿ØÖÆÇ°×º¡°@¡±²»»áÆÁ±Î½âÎö´íÎóµÄÐÅÏ¢¡£ - </para> - </note> <warning> <para> ĿǰµÄ¡°@¡±´íÎó¿ØÖÆÔËËã·ûǰ׺ÉõÖÁʹµ¼Ö½ű¾ÖÕÖ¹µÄÑÏÖØ´íÎóµÄ´íÎ󱨸æÒ²Ê§Ð§¡£ÕâÒâζ×ÅÈç¹ûÄãÔÚij¸ö²»´æÔÚ»òÀàÐÍ´íÎóµÄº¯Êýµ÷ÓÃǰÓÃÁË¡°@¡±À´ÒÖÖÆ´íÎóÐÅÏ¢£¬Äǽű¾»áûÓÐÈκμ£ÏóÏÔʾÔÒò¶øËÀÔÚÄÇÀï¡£ @@ -450,12 +702,18 @@ </sect1> <sect1 id="language.operators.increment"> - <title>¼ÓÒ»£¯¼õÒ»ÔËËã·û</title> + <title>µÝÔö£¯µÝ¼õÔËËã·û</title> <para> - PHP Ö§³Ö C ·ç¸ñµÄǰ£¯ºó¼ÓÒ»Óë¼õÒ»ÔËËã·û¡£ + PHP Ö§³Ö C ·ç¸ñµÄǰ£¯ºóµÝÔöÓëµÝ¼õÔËËã·û¡£ </para> + <note> + <simpara> + µÝÔö£¯µÝ¼õÔËËã·û²»Ó°Ïì²¼¶ûÖµ¡£µÝ¼õ + &null; ֵҲûÓÐЧ¹û£¬µ«ÊǵÝÔö &null; µÄ½á¹ûÊÇ <literal>1</literal>¡£ + </simpara> + </note> <table> - <title>¼ÓÒ»£¯¼õÒ»ÔËËã·û</title> + <title>µÝÔö£¯µÝ¼õÔËËã·û</title> <tgroup cols="3"> <thead> <row> @@ -519,32 +777,37 @@ </informalexample> </para> <para> - ÔÚ´¦Àí×Ö·û±äÁ¿µÄËãÊýÔËËãʱ£¬PHP ÑØÏ®ÁË Perl µÄϰ¹ß£¬¶ø·Ç C µÄ¡£ÀýÈ磬ÔÚ Perl ÖÐ 'Z'+1 ½«µÃµ½ 'AA'£¬¶øÔÚ C ÖУ¬'Z'+1 ½«µÃµ½ '[' { ord('Z') == 90, ord('[') == 91 )¡£ + ÔÚ´¦Àí×Ö·û±äÁ¿µÄËãÊýÔËËãʱ£¬PHP ÑØÏ®ÁË Perl µÄϰ¹ß£¬¶ø·Ç C µÄ¡£ÀýÈ磬ÔÚ + Perl ÖÐ 'Z'+1 ½«µÃµ½ 'AA'£¬¶øÔÚ C ÖУ¬'Z'+1 ½«µÃµ½ + '['£¨ord('Z') == 90£¬ord('[') == 91£©¡£×¢Òâ×Ö·û±äÁ¿Ö»ÄܵÝÔö£¬²»Äܵݼõ¡£ <example> <title>Éæ¼°×Ö·û±äÁ¿µÄËãÊýÔËËã</title> <programlisting role="php"> <![CDATA[ <?php $i = 'W'; -for($n=0; $n<6; $n++) - echo ++$i . "\n"; - -/* - Produces the output similar to the following: - +for ($n=0; $n<6; $n++) { + echo ++$i . "\n"; +} +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ X Y Z AA AB AC - -*/ -?> ]]> - </programlisting> + </screen> </example> </para> + <para> + µÝÔö»òµÝ¼õ²¼¶ûֵûÓÐЧ¹û¡£ + </para> </sect1> <sect1 id="language.operators.logical"> @@ -610,41 +873,94 @@ <informalexample> <programlisting role="php"> <![CDATA[ +<?php $a = "Hello "; $b = $a . "World!"; // now $b contains "Hello World!" $a = "Hello "; $a .= "World!"; // now $a contains "Hello World!" +?> ]]> </programlisting> </informalexample> </para> <para> - Çë²ÎÔÄÊÖ²áÖС°<link linkend="language.types.string">×Ö·û´®ÀàÐÍ</link>¡±ºÍ¡°<link linkend="ref.strings">×Ö·û´®º¯Êý</link>¡±µÄÓйØÕ½ڡ£ + ²Î¼û<link linkend="language.types.string">×Ö·û´®ÀàÐÍ</link>ºÍ<link linkend="ref.strings">×Ö·û´®º¯Êý</link>Õ½ڡ£ </para> </sect1> <sect1 id="language.operators.array"> <title>Êý×éÔËËã·û</title> - <simpara> - PHP ½öÓеÄÒ»¸öÊý×éÔËËã·ûÊÇ <literal>+</literal> ÔËËã·û¡£Ëü°ÑÓұߵÄÊý×鸽¼Óµ½×ó±ßµÄÊý×éºó£¬µ«ÊÇÖØ¸´µÄ¼üÖµ²»»á±»¸²¸Ç¡£ - </simpara> + <table> + <title>Êý×éÔËËã·û</title> + <tgroup cols="3"> + <thead> + <row> + <entry>Àý×Ó</entry> + <entry>Ãû³Æ</entry> + <entry>½á¹û</entry> + </row> + </thead> + <tbody> + <row> + <entry>$a + $b</entry> + <entry>ÁªºÏ</entry> + <entry>$a ºÍ $b µÄÁªºÏ¡£</entry> + </row> + <row> + <entry>$a == $b</entry> + <entry>ÏàµÈ</entry> + <entry>Èç¹û $a ºÍ $b ¾ßÓÐÏàͬµÄ¼ü£¯Öµ¶ÔÔòΪ &true;¡£</entry> + </row> + <row> + <entry>$a === $b</entry> + <entry>È«µÈ</entry> + <entry>Èç¹û $a ºÍ $b ¾ßÓÐÏàͬµÄ¼ü£¯Öµ¶Ô²¢ÇÒ˳ÐòºÍÀàÐͶ¼ÏàͬÔòΪ &true;¡£</entry> + </row> + <row> + <entry>$a != $b</entry> + <entry>²»µÈ</entry> + <entry>Èç¹û $a ²»µÈÓÚ $b ÔòΪ &true;¡£</entry> + </row> + <row> + <entry>$a <> $b</entry> + <entry>²»µÈ</entry> + <entry>Èç¹û $a ²»µÈÓÚ $b ÔòΪ &true;¡£</entry> + </row> + <row> + <entry>$a !== $b</entry> + <entry>²»È«µÈ</entry> + <entry>Èç¹û $a ²»È«µÈÓÚ $b ÔòΪ &true;¡£</entry> + </row> + </tbody> + </tgroup> + </table> + <para> + <literal>+</literal> ÔËËã·û°ÑÓұߵÄÊý×鸽¼Óµ½×ó±ßµÄÊý×éºóÃæ£¬µ«ÊÇÖØ¸´µÄ¼üÖµ²»»á±»¸²¸Ç¡£ + </para> <para> <informalexample> <programlisting role="php"> <![CDATA[ +<?php $a = array("a" => "apple", "b" => "banana"); -$b = array("a" =>"pear", "b" => "strawberry", "c" => "cherry"); +$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry"); -$c = $a + $b; +$c = $a + $b; // Union of $a and $b +echo "Union of \$a and \$b: \n"; +var_dump($c); +$c = $b + $a; // Union of $b and $a +echo "Union of \$b and \$a: \n"; var_dump($c); +?> ]]> </programlisting> </informalexample> Ö´Ðк󣬴˽ű¾»áÏÔʾ£º <screen role="php"> <![CDATA[ +Union of $a and $b: array(3) { ["a"]=> string(5) "apple" @@ -653,12 +969,82 @@ ["c"]=> string(6) "cherry" } - +Union of $b and $a: +array(3) { + ["a"]=> + string(4) "pear" + ["b"]=> + string(10) "strawberry" + ["c"]=> + string(6) "cherry" +} ]]> </screen> </para> <para> - Çë²ÎÔÄÊÖ²áÖС°<link linkend="language.types.array">Êý×éÀàÐÍ</link>¡±ºÍ¡°<link linkend="ref.array">Êý×麯Êý</link>¡±µÄÓйØÕ½ڡ£ + Êý×éÖеĵ¥ÔªÈç¹û¾ßÓÐÏàͬµÄ¼üÃûºÍÖµÔò±È½ÏʱÏàµÈ¡£ + </para> + <para> + <example> + <title>±È½ÏÊý×é</title> + <programlisting role="php"> +<![CDATA[ +<?php +$a = array("apple", "banana"); +$b = array(1 => "banana", "0" => "apple"); + +var_dump($a == $b); // bool(true) +var_dump($a === $b); // bool(false) +?> +]]> + </programlisting> + </example> + </para> + <para> + ²Î¼û<link linkend="language.types.array">Êý×éÀàÐÍ</link>ºÍ<link + linkend="ref.array">Êý×麯Êý</link>Õ½ڡ£ + </para> + </sect1> + + <sect1 id="language.operators.type"> + <title>ÀàÐÍÔËËã·û</title> + <para> + PHP ÓÐÒ»¸öÀàÐÍÔËËã·û£º<literal>instanceof</literal>¡£<literal>instanceof</literal> + ÓÃÀ´²â¶¨Ò»¸ö¸ø¶¨µÄ¶ÔÏóÊÇ·ñÀ´×ÔÖ¸¶¨µÄ<link + linkend="language.oop">¶ÔÏóÀà</link>¡£ + </para> + <simpara> + <literal>instanceof</literal> ÔËËã·ûÊÇ PHP 5 Òý½øµÄ¡£ÔÚ´Ë֮ǰÓà + <function>is_a</function>£¬µ«ÊÇ + <function>is_a</function> ÒѾ¹ýʱÁË£¬×îºÃÓà <literal>instanceof</literal>¡£ + </simpara> + <informalexample> + <programlisting> +<![CDATA[ +<?php +class A { } +class B { } + +$thing = new A; + +if ($thing instanceof A) { + echo 'A'; +} +if ($thing instanceof B) { + echo 'B'; +} +?> +]]> + </programlisting> + <simpara> + ÓÉÓÚ <varname>$thing</varname> ÊÇÀàÐÍ A µÄÒ»¸ö <type>object</type>£¬¶ø²»ÊÇ + B µÄ£¬Ö»ÓкÍÀàÐÍ A Ïà·ûºÏµÄ³ÌÐò¿é±»ÔËÐУº + </simpara> + <screen>A</screen> + </informalexample> + <para> + ²Î¼û <function>get_class</function> ºÍ + <function>is_a</function>¡£ </para> </sect1> </chapter>