cvs: phpdoc-zh /language operators.xml
| From: | Gang Chen | Date: | Sat, 20 Dec 2008 23:01:01 +0000 |
| Subject: | cvs: phpdoc-zh /language operators.xml | ||
| Groups: | php.doc.zh | ||
| Request: | Send a blank email to doc-zh+get-3127@lists.php.net to get a copy of this message | ||
eniac2008 Sat Dec 20 23:01:01 2008 UTC
Modified files:
/phpdoc-zh/language operators.xml
Log:
Sync with EN-Revision 1.128
http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/operators.xml?r1=1.20&r2=1.21&diff_format=u Index: phpdoc-zh/language/operators.xml diff -u phpdoc-zh/language/operators.xml:1.20 phpdoc-zh/language/operators.xml:1.21 --- phpdoc-zh/language/operators.xml:1.20 Sat Jun 23 13:32:42 2007 +++ phpdoc-zh/language/operators.xml Sat Dec 20 23:01:01 2008 @@ -1,7 +1,8 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.20 $ --> -<!-- $Author: bjori $ --> -<!-- EN-Revision: 1.101 Maintainer: dallas Status: ready --> +<!-- $Revision: 1.21 $ --> +<!-- $Author: eniac2008 $ --> +<!-- EN-Revision: 1.128 Maintainer: Altair Status: ready --> +<!-- CREDITS: dallas --> <chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook"> <title>ÔËËã·û</title> <simpara> @@ -41,8 +42,8 @@ <tbody> <row> <entry>·Ç½áºÏ</entry> - <entry>new</entry> - <entry><link linkend="language.oop5.basic.new">new</link></entry> + <entry>clone new</entry> + <entry><link linkend="language.oop5.cloning">clone</link> ºÍ <link linkend="language.oop5.basic.new">new</link></entry> </row> <row> <entry>×ó</entry> @@ -58,23 +59,37 @@ </row> <row> <entry>·Ç½áºÏ</entry> - <entry>! ~ - (int) (float) (string) (array) (object) @</entry> + <entry>~ - (int) (float) (string) (array) (object) (bool) @</entry> <entry> <link linkend="language.types">ÀàÐÍ</link> </entry> </row> <row> + <entry>·Ç½áºÏ</entry> + <entry>instanceof</entry> + <entry> + <link linkend="language.types">ÀàÐÍ</link> + </entry> + </row> + <row> + <entry>ÓÒ½áºÏ</entry> + <entry>!</entry> + <entry> + <link linkend="language.operators.logical">Âß¼²Ù×÷·û</link> + </entry> + </row> + <row> <entry>×ó</entry> <entry>* / %</entry> <entry> - <link linkend="language.operators.arithmetic">ËãÊýÔËËã·û</link> + <link linkend="language.operators.arithmetic">ËãÊõÔËËã·û</link> </entry> </row> <row> <entry>×ó</entry> <entry>+ - .</entry> <entry> - <link linkend="language.operators.arithmetic">ËãÊýÔËËã·û</link>ºÍ<link + <link linkend="language.operators.arithmetic">ËãÊõÔËËã·û</link>ºÍ<link linkend="language.operators.string">×Ö·û´®ÔËËã·û</link></entry> </row> <row> @@ -86,7 +101,7 @@ </row> <row> <entry>·Ç½áºÏ</entry> - <entry>< <= > >=</entry> + <entry>< <= > >= <></entry> <entry> <link linkend="language.operators.comparison">±È½ÏÔËËã·û</link> </entry> @@ -200,9 +215,9 @@ </para> <note> <para> - ¾¡¹Ü <literal>!</literal> ±È <literal>=</literal> µÄÓÅÏȼ¶¸ß£¬PHP + ¾¡¹Ü <literal>=</literal> ±ÈÆäËü´ó¶àÊýµÄÔËËã·ûµÄÓÅÏȼ¶µÍ£¬PHP ÈÔ¾ÉÔÊÐíÀàËÆÈçϵıí´ïʽ£º<literal>if (!$a = foo())</literal>£¬ÔÚ´ËÀýÖÐ - <literal>foo()</literal> µÄÊä³ö±»¸³¸øÁË <varname>$a</varname>¡£ + <literal>foo()</literal> µÄ·µ»ØÖµ±»¸³¸øÁË <varname>$a</varname>¡£ </para> </note> </sect1> @@ -257,7 +272,10 @@ </tgroup> </table> <simpara> - ³ýºÅ£¨¡°/¡±£©×ÜÊÇ·µ»Ø¸¡µãÊý£¬¼´Ê¹Á½¸öÔËËãÊýÊÇÕûÊý£¨»òÓÉ×Ö·û´®×ª»»³ÉµÄÕûÊý£©Ò²ÊÇÕâÑù¡£ + ³ý·¨ÔËËã·û×ÜÊÇ·µ»Ø·ûµãÊý¡£Ö»ÓÐÔÚÏÂÁÐÇé¿öÀýÍ⣺Á½¸ö²Ù×÷Êý¶¼ÊÇÕûÊý£¨»ò×Ö·û´®×ª»»³ÉµÄÕûÊý£©²¢ÇÒÕýºÃÄÜÕû³ý£¬ÕâʱËü·µ»ØÒ»¸öÕûÊý¡£ + </simpara> + <simpara> + ȡģÔËËã·ûµÄ²Ù×÷ÊýÔÚÔËËã֮ǰ¶¼»áת»»³ÉÕûÊý£¨³ýȥСÊý²¿·Ö£©¡£ </simpara> <note> <simpara> @@ -269,16 +287,6 @@ ²Î¼ûÊÖ²áÖеÄ<link linkend="ref.math">Êýѧº¯Êý</link>¡£ </simpara> - <!-- - <simpara> - The division operator ("/") returns an integer value (the ½á¹û - of an integer division) if the two operands are integers (or - strings that get converted to integers) and the quotient is an - integer. If either operand is a floating-point value, or the - operation ½á¹ûs in a non-integer value, a floating-point value - is returned. - </simpara> - --> </sect1> <sect1 xml:id="language.operators.assignment"> @@ -319,11 +327,11 @@ </informalexample> </para> <para> - ×¢Ò⸳ֵÔËË㽫ԱäÁ¿µÄÖµ¿½±´µ½Ð±äÁ¿ÖУ¨´«Öµ¸³Öµ£©£¬ËùÒԸıäÆäÖÐÒ»¸ö²¢²»Ó°ÏìÁíÒ»¸ö¡£ÕâÒ²ÊʺÏÓÚÔÚºÜÃܼ¯µÄÑ»·Öп½±´Ò»Ð©ÖµÀýÈç´óÊý×é¡£×Ô - PHP 4 ÆðÖ§³ÖÒýÓø³Öµ£¬Óà <computeroutput>$var = - &$othervar;</computeroutput> Óï·¨£¬µ«ÔÚ PHP 3 - Öв»¿ÉÄÜÕâÑù×ö¡£ÒýÓø³ÖµÒâζ×ÅÁ½¸ö±äÁ¿¶¼Ö¸Ïòͬһ¸öÊý¾Ý£¬Ã»ÓÐÈκÎÊý¾ÝµÄ¿½±´¡£ÓйØÒýÓõĸü¶àÐÅÏ¢¼û<link - linkend="language.references">ÒýÓõÄ˵Ã÷</link>¡£ + ×¢Ò⸳ֵÔËË㽫ԱäÁ¿µÄÖµ¿½±´µ½Ð±äÁ¿ÖУ¨´«Öµ¸³Öµ£©£¬ËùÒԸıäÆäÖÐÒ»¸ö²¢²»Ó°ÏìÁíÒ»¸ö¡£ÕâÒ²ÊʺÏÓÚÔÚºÜÃܼ¯µÄÑ»·Öп½±´Ò»Ð©ÖµÀýÈç´óÊý×é¡£Ò²¿ÉÒÔʹÓÃÒýÓø³Öµ£¬Óà <computeroutput>$var = + &$othervar;</computeroutput> Óï·¨¡£ÒýÓø³ÖµÒâζ×ÅÁ½¸ö±äÁ¿¶¼Ö¸Ïòͬһ¸öÊý¾Ý£¬Ã»ÓÐÈκÎÊý¾ÝµÄ¿½±´¡£ÓйØÒýÓõĸü¶àÐÅÏ¢¼û<link + linkend="language.references">ÒýÓõÄ˵Ã÷</link>¡£ÔÚ + PHP 5ÖУ¬¶ÔÏó×ÜÊÇͨ¹ýÒýÓø³ÖµµÄ£¬³ý·ÇÃ÷ȷʹÓÃÐ嵀 <link + linkend="language.oop5.cloning">clone</link>¹Ø¼ü×Ö¡£ </para> </sect1> @@ -345,6 +353,12 @@ echo "hallo" ^ "hello"; // Êä³ö ascii Öµ #0 #4 #0 #0 #0 // 'a' ^ 'e' = #4 + +echo 2 ^ "3"; // Êä³ö 1 + // 2 ^ ((int)"3") == 1 + +echo "2" ^ 3; // Êä³ö 1 + // ((int)"2") ^ 3 == 1 ?> ]]> </programlisting> @@ -477,7 +491,7 @@ <?php var_dump(0 == "a"); // 0 == 0 -> true var_dump("1" == "01"); // 1 == 1 -> true - +var_dump("1" == "1e0"); // 1 == 1 -> true switch ("a") { case 0: echo "0"; @@ -623,7 +637,36 @@ °æ±¾»áΪ´Ë·¢³öÒ»Ìõ¾¯¸æ¡£ </simpara> </note> - </sect2> + <note> + <para> + ½¨Òé±ÜÃ⽫ÈýÔªÔËËã·û¶Ñ»ýÔÚÒ»ÆðʹÓᣵ±ÔÚÒ»ÌõÓï¾äÖÐʹÓöà¸öÈýÔªÔËËã·ûʱ»áÔì³É + PHP ÔËËã½á¹û²»ÇåÎú£º + Is is recommended that you avoid "stacking" ternary expressions. PHP's + behaviour when using more than one ternary operator within a single + statement is non-obvious: + <example> + <title>²»ÇåÎúµÄÈýÔªÔËËã·ûÐÐΪ</title> + <programlisting role="php"> +<![CDATA[ +<?php +// Õ§¿´ÆðÀ´ÏÂÃæµÄÊä³öÊÇ 'true' +echo (true?'true':false?'t':'f'); + +// È»¶ø£¬ÉÏÃæÓï¾äµÄʵ¼ÊÊä³öÊÇ't'£¬ÒòΪÈýÔªÔËËã·ûÊÇ´Ó×óÍùÓÒ¼ÆËãµÄ + +// ÏÂÃæÊÇÓëÉÏÃæµÈ¼ÛµÄÓï¾ä£¬µ«¸üÇåÎú +echo ((true ? 'true' : 'false') ? 't' : 'f'); + +// here, you can see that the first expression is evaluated to 'true', which +// in turn evaluates to (bool)true, thus returning the true branch of the +// second ternary expression. +?> +]]> + </programlisting> + </example> + </para> + </note> + </sect2> </sect1> @@ -636,7 +679,7 @@ <simpara> Èç¹û¼¤»îÁË <link linkend="ini.track-errors"><option>track_errors</option></link> ÌØÐÔ£¬±í´ïʽËù²úÉúµÄÈκδíÎóÐÅÏ¢¶¼±»´æ·ÅÔÚ±äÁ¿ - <link linkend="reserved.variables.phperrormsg">$php_errormsg</link> + <varname>$php_errormsg</varname> ÖС£´Ë±äÁ¿ÔÚÿ´Î³ö´íʱ¶¼»á±»¸²¸Ç£¬ËùÒÔÈç¹ûÏëÓÃËüµÄ»°¾ÍÒª¾¡Ôç¼ì²é¡£ </simpara> <para> @@ -862,6 +905,40 @@ <simpara> ¡°Ó롱ºÍ¡°»ò¡±ÓÐÁ½ÖÖ²»Í¬ÐÎʽÔËËã·ûµÄÔÒòÊÇËüÃÇÔËËãµÄÓÅÏȼ¶²»Í¬£¨¼û<link linkend="language.operators.precedence">ÔËËã·ûÓÅÏȼ¶</link>£©¡£ </simpara> + <example> + <title>Âß¼ÔËËã·ûʾÀý</title> + <programlisting role="php"> +<![CDATA[ +<?php + +// ÏÂÃæµÄ foo() ²»»á±»µ÷Óã¬ÒòΪËüÃDZ»ÔËËã·û¡°¶Ì·¡±ÁË¡£ +$a = (false && foo()); +$b = (true || foo()); +$c = (false and foo()); +$d = (true or foo()); + +// "||" µÄÓÅÏȼ¶±È "or" ¸ß +$e = false || true; // $e ±»¸³ÖµÎª (false || true)£¬½á¹ûΪ true +$f = false or true; // $f ±»¸³ÖµÎª false [Altair×¢£º"=" µÄÓÅÏȼ¶±È "or" ¸ß] +var_dump($e, $f); + +// "&&" µÄÓÅÏȼ¶±È "and"¡¡¸ß +$g = true && false; // $g ±»¸³ÖµÎª (true && false)£¬½á¹ûΪ false +$h = true and false; // $h ±»¸³ÖµÎª true [Altair×¢£º"=" µÄÓÅÏȼ¶±È "and" ¸ß] +var_dump($g, $h); +?> +]]> + </programlisting> + &example.outputs.similar; + <screen> +<![CDATA[ +bool(true) +bool(false) +bool(false) +bool(true) +]]> + </screen> + </example> </sect1> <sect1 xml:id="language.operators.string"> @@ -938,7 +1015,7 @@ </tgroup> </table> <para> - <literal>+</literal> ÔËËã·û°ÑÓұߵÄÊý×鸽¼Óµ½×ó±ßµÄÊý×éºóÃæ£¬µ«ÊÇÖØ¸´µÄ¼üÖµ²»»á±»¸²¸Ç¡£ + <literal>+</literal> ÔËËã·û°ÑÓұߵÄÊý×éÔªËØ£¨³ýÈ¥¼üÖµÓë×ó±ßµÄÊý×éÔªËØÏàͬµÄÄÇÐ©ÔªËØ£©¸½¼Óµ½×ó±ßµÄÊý×éºóÃæ£¬µ«ÊÇÖØ¸´µÄ¼üÖµ²»»á±»¸²¸Ç¡£ </para> <para> <informalexample> @@ -1011,40 +1088,190 @@ <sect1 xml:id="language.operators.type"> <title>ÀàÐÍÔËËã·û</title> <para> - PHP Ö»ÓÐÒ»¸öÀàÐÍÔËËã·û£º<literal>instanceof</literal> - ÓÃÀ´²â¶¨Ò»¸ö¸ø¶¨µÄ¶ÔÏó£¬ËüµÄ¸¸¶ÔÏó»òËüÃÇËùʵÏÖµÄ<link - linkend="language.oop5.interfaces">½Ó¿Ú</link>ÊÇ·ñÀ´×ÔÖ¸¶¨µÄ<link - linkend="language.oop">¶ÔÏóÀà</link>¡£ + <literal>instanceof</literal> ÓÃÓÚÈ·¶¨Ò»¸ö PHP ±äÁ¿ÊÇ·ñÊôÓÚijһÀà + <link linkend="language.oop5.basic.class">class</link> µÄʵÀý£º + <example> + <title>instanceof ʹÓÃʾÀý</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyClass +{ +} + +class NotMyClass +{ +} +$a = new MyClass; + +var_dump($a instanceof MyClass); +var_dump($a instanceof NotMyClass); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(false) +]]> + </screen> + </example> </para> - <simpara> - <literal>instanceof</literal> ÔËËã·ûÊÇ PHP 5 Òý½øµÄ¡£ÔÚ´Ë֮ǰÓà - <function>is_a</function>£¬µ«ÊÇ - <function>is_a</function> ÒѾ¹ýʱÁË£¬×îºÃÓà <literal>instanceof</literal>¡£ - </simpara> - <informalexample> - <programlisting role="php"> + <para> + <literal>instanceof</literal>¡¡Ò²¿ÉÓÃÀ´È·¶¨Ò»¸ö±äÁ¿ÊDz»ÊǼ̳Ð×Ôijһ¸¸ÀàµÄ×ÓÀàµÄʵÀý£º + can also be used to determine whether a variable + is an instantiated object of a class that inherits from a parent class: + <example> + <title>instanceof Óë¼Ì³ÐÀàʾÀý</title> + <programlisting role="php"> <![CDATA[ <?php -class A { } -class B { } +class ParentClass +{ +} + +class MyClass extends ParentClass +{ +} -$thing = new A; +$a = new MyClass; -if ($thing instanceof A) { - echo 'A'; +var_dump($a instanceof MyClass); +var_dump($a instanceof ParentClass); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(true) +]]> + </screen> + </example> + </para> + <para> + ¼ì²éÒ»¸ö¶ÔÏó <emphasis>²»ÊÇ</emphasis> ij¸öÀàµÄʵÀý£¬¿ÉÒÔʹÓà <link + linkend="language.operators.logical">Âß¼ÔËËã·û <literal>not</literal></link>¡£ + <example> + <title>ʹÓÃinstanceof¼ì²é¶ÔÏó <emphasis>²»ÊÇ</emphasis> ijһÀàµÄʵÀý</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyClass +{ +} + +$a = new MyClass; +var_dump(!($a instanceof stdClass)); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +]]> + </screen> + </example> + </para> + <para> + ×îºó£¬<literal>instanceof</literal>Ò²¿ÉÓÃÓÚÈ·¶¨Ò»¸ö±äÁ¿ÊDz»ÊÇʵÏÖÁËij¸ö<link + linkend="language.oop5.interfaces">½Ó¿Ú</link>µÄ¶ÔÏóµÄʵÀý: + <example> + <title>Using instanceof for class</title> + <programlisting role="php"> +<![CDATA[ +<?php +interface MyInterface +{ +} + +class MyClass implements MyInterface +{ +} + +$a = new MyClass; + +var_dump($a instanceof MyClass); +var_dump($a instanceof MyInterface); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(true) +]]> + </screen> + </example> + </para> + <para> + ËäÈ» <literal>instanceof</literal> ͨ³£Ö±½ÓÓëÀàÃûÒ»ÆðʹÓ㬵«Ò²¿ÉÒÔʹÓöÔÏó»ò×Ö·û´®±äÁ¿£º + <example> + <title>Using instanceof with other variables</title> + <programlisting role="php"> +<![CDATA[ +<?php +interface MyInterface +{ } -if ($thing instanceof B) { - echo 'B'; + +class MyClass implements MyInterface +{ } + +$a = new MyClass; +$b = new MyClass; +$c = 'MyClass'; +$d = 'NotMyClass'; + +var_dump($a instanceof $b); // $b is an object of class MyClass +var_dump($a instanceof $c); // $c is a string 'MyClass' +var_dump($a instanceof $d); // $d is a string 'NotMyClass' ?> ]]> </programlisting> - <simpara> - ÓÉÓÚ <varname>$thing</varname> ÊÇÀàÐÍ A µÄÒ»¸ö <type>object</type>£¬¶ø²»ÊÇ - B µÄ£¬Ö»ÓкÍÀàÐÍ A Ïà·ûºÏµÄ³ÌÐò¿é±»ÔËÐУº - </simpara> - <screen>A</screen> - </informalexample> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(true) +bool(false) +]]> + </screen> + </example> + </para> + <para> + È»¶ø instanceof µÄʹÓû¹ÓÐһЩÏÝÚ屨ÐëÁ˽⡣ÔÚ + PHP 5.1.0֮ǰ£¬Èç¹ûÒª¼ì²éµÄÀàÃû³Æ²»´æÔÚ£¬<literal>instanceof</literal> »áµ÷Óà <link + linkend="language.oop5.autoload">__autoload()</link>¡£ÁíÍ⣬Èç¹û¸ÃÀàûÓб»×°ÔØÔò»á²úÉúÒ»¸öÖÂÃü´íÎ󡣿ÉÒÔͨ¹ýʹÓÃ<literal>¶¯Ì¬ÀàÒýÓÃ(dynamic + class reference)</literal>»òÓÃÒ»¸ö°üº¬ÀàÃûµÄ×Ö·û´®±äÁ¿À´±Ü¿ªÕâÖÖÎÊÌ⣺ + <example> + <title>±ÜÃâ PHP 5.0 ÖÐ instanceof ÒýÆðµÄÀàÃû²éÕÒºÍÖÂÃü´íÎóÎÊÌâ</title> + <programlisting role="php"> +<![CDATA[ +<?php +$d = 'NotMyClass'; +var_dump($a instanceof $d); // no fatal error here +?> + ]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(false) +]]> + </screen> + </example> + </para> + <simpara> + <literal>instanceof</literal> ÔËËã·ûÊÇ PHP 5 Òý½øµÄ¡£ÔÚ´Ë֮ǰÓà + <function>is_a</function>£¬µ«ÊÇ + <function>is_a</function> ÒѾ¹ýʱÁË£¬×îºÃÓà <literal>instanceof</literal>¡£ + </simpara> <para> ²Î¼û <function>get_class</function> ºÍ <function>is_a</function>¡£
http://cvs.php.net/viewvc.cgi/phpdoc-zh/language/operators.xml?r1=1.20&r2=1.21&diff_format=u Index: phpdoc-zh/language/operators.xml diff -u phpdoc-zh/language/operators.xml:1.20 phpdoc-zh/language/operators.xml:1.21 --- phpdoc-zh/language/operators.xml:1.20 Sat Jun 23 13:32:42 2007 +++ phpdoc-zh/language/operators.xml Sat Dec 20 23:01:01 2008 @@ -1,7 +1,8 @@ <?xml version="1.0" encoding="gb2312"?> -<!-- $Revision: 1.20 $ --> -<!-- $Author: bjori $ --> -<!-- EN-Revision: 1.101 Maintainer: dallas Status: ready --> +<!-- $Revision: 1.21 $ --> +<!-- $Author: eniac2008 $ --> +<!-- EN-Revision: 1.128 Maintainer: Altair Status: ready --> +<!-- CREDITS: dallas --> <chapter xml:id="language.operators" xmlns="http://docbook.org/ns/docbook"> <title>ÔËËã·û</title> <simpara> @@ -41,8 +42,8 @@ <tbody> <row> <entry>·Ç½áºÏ</entry> - <entry>new</entry> - <entry><link linkend="language.oop5.basic.new">new</link></entry> + <entry>clone new</entry> + <entry><link linkend="language.oop5.cloning">clone</link> ºÍ <link linkend="language.oop5.basic.new">new</link></entry> </row> <row> <entry>×ó</entry> @@ -58,23 +59,37 @@ </row> <row> <entry>·Ç½áºÏ</entry> - <entry>! ~ - (int) (float) (string) (array) (object) @</entry> + <entry>~ - (int) (float) (string) (array) (object) (bool) @</entry> <entry> <link linkend="language.types">ÀàÐÍ</link> </entry> </row> <row> + <entry>·Ç½áºÏ</entry> + <entry>instanceof</entry> + <entry> + <link linkend="language.types">ÀàÐÍ</link> + </entry> + </row> + <row> + <entry>ÓÒ½áºÏ</entry> + <entry>!</entry> + <entry> + <link linkend="language.operators.logical">Âß¼²Ù×÷·û</link> + </entry> + </row> + <row> <entry>×ó</entry> <entry>* / %</entry> <entry> - <link linkend="language.operators.arithmetic">ËãÊýÔËËã·û</link> + <link linkend="language.operators.arithmetic">ËãÊõÔËËã·û</link> </entry> </row> <row> <entry>×ó</entry> <entry>+ - .</entry> <entry> - <link linkend="language.operators.arithmetic">ËãÊýÔËËã·û</link>ºÍ<link + <link linkend="language.operators.arithmetic">ËãÊõÔËËã·û</link>ºÍ<link linkend="language.operators.string">×Ö·û´®ÔËËã·û</link></entry> </row> <row> @@ -86,7 +101,7 @@ </row> <row> <entry>·Ç½áºÏ</entry> - <entry>< <= > >=</entry> + <entry>< <= > >= <></entry> <entry> <link linkend="language.operators.comparison">±È½ÏÔËËã·û</link> </entry> @@ -200,9 +215,9 @@ </para> <note> <para> - ¾¡¹Ü <literal>!</literal> ±È <literal>=</literal> µÄÓÅÏȼ¶¸ß£¬PHP + ¾¡¹Ü <literal>=</literal> ±ÈÆäËü´ó¶àÊýµÄÔËËã·ûµÄÓÅÏȼ¶µÍ£¬PHP ÈÔ¾ÉÔÊÐíÀàËÆÈçϵıí´ïʽ£º<literal>if (!$a = foo())</literal>£¬ÔÚ´ËÀýÖÐ - <literal>foo()</literal> µÄÊä³ö±»¸³¸øÁË <varname>$a</varname>¡£ + <literal>foo()</literal> µÄ·µ»ØÖµ±»¸³¸øÁË <varname>$a</varname>¡£ </para> </note> </sect1> @@ -257,7 +272,10 @@ </tgroup> </table> <simpara> - ³ýºÅ£¨¡°/¡±£©×ÜÊÇ·µ»Ø¸¡µãÊý£¬¼´Ê¹Á½¸öÔËËãÊýÊÇÕûÊý£¨»òÓÉ×Ö·û´®×ª»»³ÉµÄÕûÊý£©Ò²ÊÇÕâÑù¡£ + ³ý·¨ÔËËã·û×ÜÊÇ·µ»Ø·ûµãÊý¡£Ö»ÓÐÔÚÏÂÁÐÇé¿öÀýÍ⣺Á½¸ö²Ù×÷Êý¶¼ÊÇÕûÊý£¨»ò×Ö·û´®×ª»»³ÉµÄÕûÊý£©²¢ÇÒÕýºÃÄÜÕû³ý£¬ÕâʱËü·µ»ØÒ»¸öÕûÊý¡£ + </simpara> + <simpara> + ȡģÔËËã·ûµÄ²Ù×÷ÊýÔÚÔËËã֮ǰ¶¼»áת»»³ÉÕûÊý£¨³ýȥСÊý²¿·Ö£©¡£ </simpara> <note> <simpara> @@ -269,16 +287,6 @@ ²Î¼ûÊÖ²áÖеÄ<link linkend="ref.math">Êýѧº¯Êý</link>¡£ </simpara> - <!-- - <simpara> - The division operator ("/") returns an integer value (the ½á¹û - of an integer division) if the two operands are integers (or - strings that get converted to integers) and the quotient is an - integer. If either operand is a floating-point value, or the - operation ½á¹ûs in a non-integer value, a floating-point value - is returned. - </simpara> - --> </sect1> <sect1 xml:id="language.operators.assignment"> @@ -319,11 +327,11 @@ </informalexample> </para> <para> - ×¢Ò⸳ֵÔËË㽫ԱäÁ¿µÄÖµ¿½±´µ½Ð±äÁ¿ÖУ¨´«Öµ¸³Öµ£©£¬ËùÒԸıäÆäÖÐÒ»¸ö²¢²»Ó°ÏìÁíÒ»¸ö¡£ÕâÒ²ÊʺÏÓÚÔÚºÜÃܼ¯µÄÑ»·Öп½±´Ò»Ð©ÖµÀýÈç´óÊý×é¡£×Ô - PHP 4 ÆðÖ§³ÖÒýÓø³Öµ£¬Óà <computeroutput>$var = - &$othervar;</computeroutput> Óï·¨£¬µ«ÔÚ PHP 3 - Öв»¿ÉÄÜÕâÑù×ö¡£ÒýÓø³ÖµÒâζ×ÅÁ½¸ö±äÁ¿¶¼Ö¸Ïòͬһ¸öÊý¾Ý£¬Ã»ÓÐÈκÎÊý¾ÝµÄ¿½±´¡£ÓйØÒýÓõĸü¶àÐÅÏ¢¼û<link - linkend="language.references">ÒýÓõÄ˵Ã÷</link>¡£ + ×¢Ò⸳ֵÔËË㽫ԱäÁ¿µÄÖµ¿½±´µ½Ð±äÁ¿ÖУ¨´«Öµ¸³Öµ£©£¬ËùÒԸıäÆäÖÐÒ»¸ö²¢²»Ó°ÏìÁíÒ»¸ö¡£ÕâÒ²ÊʺÏÓÚÔÚºÜÃܼ¯µÄÑ»·Öп½±´Ò»Ð©ÖµÀýÈç´óÊý×é¡£Ò²¿ÉÒÔʹÓÃÒýÓø³Öµ£¬Óà <computeroutput>$var = + &$othervar;</computeroutput> Óï·¨¡£ÒýÓø³ÖµÒâζ×ÅÁ½¸ö±äÁ¿¶¼Ö¸Ïòͬһ¸öÊý¾Ý£¬Ã»ÓÐÈκÎÊý¾ÝµÄ¿½±´¡£ÓйØÒýÓõĸü¶àÐÅÏ¢¼û<link + linkend="language.references">ÒýÓõÄ˵Ã÷</link>¡£ÔÚ + PHP 5ÖУ¬¶ÔÏó×ÜÊÇͨ¹ýÒýÓø³ÖµµÄ£¬³ý·ÇÃ÷ȷʹÓÃÐ嵀 <link + linkend="language.oop5.cloning">clone</link>¹Ø¼ü×Ö¡£ </para> </sect1> @@ -345,6 +353,12 @@ echo "hallo" ^ "hello"; // Êä³ö ascii Öµ #0 #4 #0 #0 #0 // 'a' ^ 'e' = #4 + +echo 2 ^ "3"; // Êä³ö 1 + // 2 ^ ((int)"3") == 1 + +echo "2" ^ 3; // Êä³ö 1 + // ((int)"2") ^ 3 == 1 ?> ]]> </programlisting> @@ -477,7 +491,7 @@ <?php var_dump(0 == "a"); // 0 == 0 -> true var_dump("1" == "01"); // 1 == 1 -> true - +var_dump("1" == "1e0"); // 1 == 1 -> true switch ("a") { case 0: echo "0"; @@ -623,7 +637,36 @@ °æ±¾»áΪ´Ë·¢³öÒ»Ìõ¾¯¸æ¡£ </simpara> </note> - </sect2> + <note> + <para> + ½¨Òé±ÜÃ⽫ÈýÔªÔËËã·û¶Ñ»ýÔÚÒ»ÆðʹÓᣵ±ÔÚÒ»ÌõÓï¾äÖÐʹÓöà¸öÈýÔªÔËËã·ûʱ»áÔì³É + PHP ÔËËã½á¹û²»ÇåÎú£º + Is is recommended that you avoid "stacking" ternary expressions. PHP's + behaviour when using more than one ternary operator within a single + statement is non-obvious: + <example> + <title>²»ÇåÎúµÄÈýÔªÔËËã·ûÐÐΪ</title> + <programlisting role="php"> +<![CDATA[ +<?php +// Õ§¿´ÆðÀ´ÏÂÃæµÄÊä³öÊÇ 'true' +echo (true?'true':false?'t':'f'); + +// È»¶ø£¬ÉÏÃæÓï¾äµÄʵ¼ÊÊä³öÊÇ't'£¬ÒòΪÈýÔªÔËËã·ûÊÇ´Ó×óÍùÓÒ¼ÆËãµÄ + +// ÏÂÃæÊÇÓëÉÏÃæµÈ¼ÛµÄÓï¾ä£¬µ«¸üÇåÎú +echo ((true ? 'true' : 'false') ? 't' : 'f'); + +// here, you can see that the first expression is evaluated to 'true', which +// in turn evaluates to (bool)true, thus returning the true branch of the +// second ternary expression. +?> +]]> + </programlisting> + </example> + </para> + </note> + </sect2> </sect1> @@ -636,7 +679,7 @@ <simpara> Èç¹û¼¤»îÁË <link linkend="ini.track-errors"><option>track_errors</option></link> ÌØÐÔ£¬±í´ïʽËù²úÉúµÄÈκδíÎóÐÅÏ¢¶¼±»´æ·ÅÔÚ±äÁ¿ - <link linkend="reserved.variables.phperrormsg">$php_errormsg</link> + <varname>$php_errormsg</varname> ÖС£´Ë±äÁ¿ÔÚÿ´Î³ö´íʱ¶¼»á±»¸²¸Ç£¬ËùÒÔÈç¹ûÏëÓÃËüµÄ»°¾ÍÒª¾¡Ôç¼ì²é¡£ </simpara> <para> @@ -862,6 +905,40 @@ <simpara> ¡°Ó롱ºÍ¡°»ò¡±ÓÐÁ½ÖÖ²»Í¬ÐÎʽÔËËã·ûµÄÔÒòÊÇËüÃÇÔËËãµÄÓÅÏȼ¶²»Í¬£¨¼û<link linkend="language.operators.precedence">ÔËËã·ûÓÅÏȼ¶</link>£©¡£ </simpara> + <example> + <title>Âß¼ÔËËã·ûʾÀý</title> + <programlisting role="php"> +<![CDATA[ +<?php + +// ÏÂÃæµÄ foo() ²»»á±»µ÷Óã¬ÒòΪËüÃDZ»ÔËËã·û¡°¶Ì·¡±ÁË¡£ +$a = (false && foo()); +$b = (true || foo()); +$c = (false and foo()); +$d = (true or foo()); + +// "||" µÄÓÅÏȼ¶±È "or" ¸ß +$e = false || true; // $e ±»¸³ÖµÎª (false || true)£¬½á¹ûΪ true +$f = false or true; // $f ±»¸³ÖµÎª false [Altair×¢£º"=" µÄÓÅÏȼ¶±È "or" ¸ß] +var_dump($e, $f); + +// "&&" µÄÓÅÏȼ¶±È "and"¡¡¸ß +$g = true && false; // $g ±»¸³ÖµÎª (true && false)£¬½á¹ûΪ false +$h = true and false; // $h ±»¸³ÖµÎª true [Altair×¢£º"=" µÄÓÅÏȼ¶±È "and" ¸ß] +var_dump($g, $h); +?> +]]> + </programlisting> + &example.outputs.similar; + <screen> +<![CDATA[ +bool(true) +bool(false) +bool(false) +bool(true) +]]> + </screen> + </example> </sect1> <sect1 xml:id="language.operators.string"> @@ -938,7 +1015,7 @@ </tgroup> </table> <para> - <literal>+</literal> ÔËËã·û°ÑÓұߵÄÊý×鸽¼Óµ½×ó±ßµÄÊý×éºóÃæ£¬µ«ÊÇÖØ¸´µÄ¼üÖµ²»»á±»¸²¸Ç¡£ + <literal>+</literal> ÔËËã·û°ÑÓұߵÄÊý×éÔªËØ£¨³ýÈ¥¼üÖµÓë×ó±ßµÄÊý×éÔªËØÏàͬµÄÄÇÐ©ÔªËØ£©¸½¼Óµ½×ó±ßµÄÊý×éºóÃæ£¬µ«ÊÇÖØ¸´µÄ¼üÖµ²»»á±»¸²¸Ç¡£ </para> <para> <informalexample> @@ -1011,40 +1088,190 @@ <sect1 xml:id="language.operators.type"> <title>ÀàÐÍÔËËã·û</title> <para> - PHP Ö»ÓÐÒ»¸öÀàÐÍÔËËã·û£º<literal>instanceof</literal> - ÓÃÀ´²â¶¨Ò»¸ö¸ø¶¨µÄ¶ÔÏó£¬ËüµÄ¸¸¶ÔÏó»òËüÃÇËùʵÏÖµÄ<link - linkend="language.oop5.interfaces">½Ó¿Ú</link>ÊÇ·ñÀ´×ÔÖ¸¶¨µÄ<link - linkend="language.oop">¶ÔÏóÀà</link>¡£ + <literal>instanceof</literal> ÓÃÓÚÈ·¶¨Ò»¸ö PHP ±äÁ¿ÊÇ·ñÊôÓÚijһÀà + <link linkend="language.oop5.basic.class">class</link> µÄʵÀý£º + <example> + <title>instanceof ʹÓÃʾÀý</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyClass +{ +} + +class NotMyClass +{ +} +$a = new MyClass; + +var_dump($a instanceof MyClass); +var_dump($a instanceof NotMyClass); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(false) +]]> + </screen> + </example> </para> - <simpara> - <literal>instanceof</literal> ÔËËã·ûÊÇ PHP 5 Òý½øµÄ¡£ÔÚ´Ë֮ǰÓà - <function>is_a</function>£¬µ«ÊÇ - <function>is_a</function> ÒѾ¹ýʱÁË£¬×îºÃÓà <literal>instanceof</literal>¡£ - </simpara> - <informalexample> - <programlisting role="php"> + <para> + <literal>instanceof</literal>¡¡Ò²¿ÉÓÃÀ´È·¶¨Ò»¸ö±äÁ¿ÊDz»ÊǼ̳Ð×Ôijһ¸¸ÀàµÄ×ÓÀàµÄʵÀý£º + can also be used to determine whether a variable + is an instantiated object of a class that inherits from a parent class: + <example> + <title>instanceof Óë¼Ì³ÐÀàʾÀý</title> + <programlisting role="php"> <![CDATA[ <?php -class A { } -class B { } +class ParentClass +{ +} + +class MyClass extends ParentClass +{ +} -$thing = new A; +$a = new MyClass; -if ($thing instanceof A) { - echo 'A'; +var_dump($a instanceof MyClass); +var_dump($a instanceof ParentClass); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(true) +]]> + </screen> + </example> + </para> + <para> + ¼ì²éÒ»¸ö¶ÔÏó <emphasis>²»ÊÇ</emphasis> ij¸öÀàµÄʵÀý£¬¿ÉÒÔʹÓà <link + linkend="language.operators.logical">Âß¼ÔËËã·û <literal>not</literal></link>¡£ + <example> + <title>ʹÓÃinstanceof¼ì²é¶ÔÏó <emphasis>²»ÊÇ</emphasis> ijһÀàµÄʵÀý</title> + <programlisting role="php"> +<![CDATA[ +<?php +class MyClass +{ +} + +$a = new MyClass; +var_dump(!($a instanceof stdClass)); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +]]> + </screen> + </example> + </para> + <para> + ×îºó£¬<literal>instanceof</literal>Ò²¿ÉÓÃÓÚÈ·¶¨Ò»¸ö±äÁ¿ÊDz»ÊÇʵÏÖÁËij¸ö<link + linkend="language.oop5.interfaces">½Ó¿Ú</link>µÄ¶ÔÏóµÄʵÀý: + <example> + <title>Using instanceof for class</title> + <programlisting role="php"> +<![CDATA[ +<?php +interface MyInterface +{ +} + +class MyClass implements MyInterface +{ +} + +$a = new MyClass; + +var_dump($a instanceof MyClass); +var_dump($a instanceof MyInterface); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(true) +]]> + </screen> + </example> + </para> + <para> + ËäÈ» <literal>instanceof</literal> ͨ³£Ö±½ÓÓëÀàÃûÒ»ÆðʹÓ㬵«Ò²¿ÉÒÔʹÓöÔÏó»ò×Ö·û´®±äÁ¿£º + <example> + <title>Using instanceof with other variables</title> + <programlisting role="php"> +<![CDATA[ +<?php +interface MyInterface +{ } -if ($thing instanceof B) { - echo 'B'; + +class MyClass implements MyInterface +{ } + +$a = new MyClass; +$b = new MyClass; +$c = 'MyClass'; +$d = 'NotMyClass'; + +var_dump($a instanceof $b); // $b is an object of class MyClass +var_dump($a instanceof $c); // $c is a string 'MyClass' +var_dump($a instanceof $d); // $d is a string 'NotMyClass' ?> ]]> </programlisting> - <simpara> - ÓÉÓÚ <varname>$thing</varname> ÊÇÀàÐÍ A µÄÒ»¸ö <type>object</type>£¬¶ø²»ÊÇ - B µÄ£¬Ö»ÓкÍÀàÐÍ A Ïà·ûºÏµÄ³ÌÐò¿é±»ÔËÐУº - </simpara> - <screen>A</screen> - </informalexample> + &example.outputs; + <screen> +<![CDATA[ +bool(true) +bool(true) +bool(false) +]]> + </screen> + </example> + </para> + <para> + È»¶ø instanceof µÄʹÓû¹ÓÐһЩÏÝÚ屨ÐëÁ˽⡣ÔÚ + PHP 5.1.0֮ǰ£¬Èç¹ûÒª¼ì²éµÄÀàÃû³Æ²»´æÔÚ£¬<literal>instanceof</literal> »áµ÷Óà <link + linkend="language.oop5.autoload">__autoload()</link>¡£ÁíÍ⣬Èç¹û¸ÃÀàûÓб»×°ÔØÔò»á²úÉúÒ»¸öÖÂÃü´íÎ󡣿ÉÒÔͨ¹ýʹÓÃ<literal>¶¯Ì¬ÀàÒýÓÃ(dynamic + class reference)</literal>»òÓÃÒ»¸ö°üº¬ÀàÃûµÄ×Ö·û´®±äÁ¿À´±Ü¿ªÕâÖÖÎÊÌ⣺ + <example> + <title>±ÜÃâ PHP 5.0 ÖÐ instanceof ÒýÆðµÄÀàÃû²éÕÒºÍÖÂÃü´íÎóÎÊÌâ</title> + <programlisting role="php"> +<![CDATA[ +<?php +$d = 'NotMyClass'; +var_dump($a instanceof $d); // no fatal error here +?> + ]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +bool(false) +]]> + </screen> + </example> + </para> + <simpara> + <literal>instanceof</literal> ÔËËã·ûÊÇ PHP 5 Òý½øµÄ¡£ÔÚ´Ë֮ǰÓà + <function>is_a</function>£¬µ«ÊÇ + <function>is_a</function> ÒѾ¹ýʱÁË£¬×îºÃÓà <literal>instanceof</literal>¡£ + </simpara> <para> ²Î¼û <function>get_class</function> ºÍ <function>is_a</function>¡£