cvs: phpdoc /cs translation.xml /cs/language control-structures.xml expressions.xml references.xml
| From: | Lukas Jelinek | Date: | Mon, 25 Mar 2002 23:07:15 +0000 |
| Subject: | cvs: phpdoc /cs translation.xml /cs/language control-structures.xml expressions.xml references.xml | ||
| Groups: | php.doc | ||
| Request: | Send a blank email to phpdoc+get-969343489@lists.php.net to get a copy of this message | ||
luk Mon Mar 25 18:07:15 2002 EDT
Added files:
/phpdoc/cs/language control-structures.xml expressions.xml
Modified files:
/phpdoc/cs translation.xml
/phpdoc/cs/language references.xml
Log:
A few small fixes, new translations.
Index: phpdoc/cs/translation.xml diff -u phpdoc/cs/translation.xml:1.4 phpdoc/cs/translation.xml:1.5 --- phpdoc/cs/translation.xml:1.4 Sun Mar 17 17:16:09 2002 +++ phpdoc/cs/translation.xml Mon Mar 25 18:07:15 2002 @@ -19,7 +19,6 @@ <file name="appendices/resources.xml" person="luk" type="translation" /> <file name="functions/mysql.xml" person="costra" type="translation" /> <file name="language/control-structures.xml" person="luk" type="translation" /> - <file name="language/expressions.xml" person="luk" type="translation" /> <file name="language/operators.xml" person="luk" type="translation" /> <file name="language/oop.xml" person="luk" type="translation" /> <file name="language/types.xml" person="luk" type="translation" /> Index: phpdoc/cs/language/references.xml diff -u phpdoc/cs/language/references.xml:1.2 phpdoc/cs/language/references.xml:1.3 --- phpdoc/cs/language/references.xml:1.2 Sun Mar 17 17:16:09 2002 +++ phpdoc/cs/language/references.xml Mon Mar 25 18:07:15 2002 @@ -7,8 +7,8 @@ <sect1 id="language.references.whatare"> <title>Co jsou reference</title> <simpara> - Reference (odkazy) jsou prostøedek, ja v PHP pøistupovat k tému¾ obsahu - promìnné pod rùznými jmény. Nejsou to ukazatele (pointery jako v C, jsou + Reference (odkazy) jsou prostøedek, jak v PHP pøistupovat k tému¾ obsahu + promìnné pod rùznými jmény. Nejsou to ukazatele (pointery) jako v C, jsou to aliasy v tabulce symbolù. Uvìdomte si, ¾e v PHP je rozdíl mezi názvem promìnné a jejím obsahem, tak¾e stejný obsah mù¾e mít rùzné názvy. Nejbli¾¹í analogií jsou názvy souborù a soubory v UNIXu - názvy Index: phpdoc/cs/language/control-structures.xml +++ phpdoc/cs/language/control-structures.xml <?xml version="1.0" encoding="iso-8859-2"?> <!-- EN-Revision: 1.55 Maintainer: luk Status: mixed --> <chapter id="control-structures"> <title>Øídicí struktury</title> <simpara> Jakýkoli PHP skript je slo¾en ze série konstruktù. Konstrukt mù¾e být pøiøazení, volání funkce, cyklus, podmínka, stejnì jako konstrukt, který nic nedìlá (prázdný konstrukt). Konstrukt obvykle konèí støedníkem. Navíc lze konstrukty seskupit do skupiny (bloku) uzavøené slo¾enými závorkami. Tento blok je sám o sobì konstruktem. V této kapitole jsou popsány rùzné typy konstruktù. </simpara> <sect1 id="control-structures.if"> <title><literal>if</literal></title> <para> Konstrukt <literal>if</literal> je jedním z nejdùle¾itìj¹ích prvkù v mnoha jazycích, vèetnì PHP. Umo¾òuje podmínìné provádìní kusu kódu. Struktura <literal>if</literal> v PHP je podobná struktuøe v C: <informalexample> <programlisting> <![CDATA[ if (expr) statement ]]> </programlisting> </informalexample> </para> <simpara> Jak je popsáno v <link linkend="language.expressions">sekci o výrazech</link>, výraz <replaceable>expr</replaceable> je ohodnoce svou boolovskou hodnotou. Poku je <replaceable>expr</replaceable> ohodnocen jako &true;, PHP provede <replaceable>statement</replaceable>; je-li ohodnocen jako &false;, neprovede se nic. Více informací o to, jak se výrazy ohodnocují jako &false; najdete v èásti <link linkend="language.types.boolean.casting">'Konverze na typ boolean'</link>. </simpara> <para> Následující pøíklad by vypsal <computeroutput>a je vìt¹í ne¾ b</computeroutput>, pokud <replaceable>$a</replaceable> je vìt¹í ne¾ <replaceable>$b</replaceable>: <informalexample> <programlisting role="php"> <![CDATA[ if ($a > $b) print "a je vìt¹í ne¾ b"; ]]> </programlisting> </informalexample> </para> <para> Èasto byste chtìli, aby se podmínìnì provádìl více ne¾ jeden konstrukt. Není samozøejmì nutné ka¾dý konstrukt zabalit do struktury <literal>if</literal>. Místo toho mù¾ete seskupit více konstruktù do bloku. Napøíklad tento kód by zobrazil <computeroutput>a je vìt¹í ne¾ b</computeroutput>, pokud <replaceable>$a</replaceable> je vìt¹í ne¾ <replaceable>$b</replaceable> a pøiøadil by hodnotu <replaceable>$a</replaceable> do <replaceable>$b</replaceable>: <informalexample> <programlisting role="php"> <![CDATA[ if ($a > $b) { print "a je vìt¹í ne¾ b"; $b = $a; } ]]> </programlisting> </informalexample> </para> <simpara> Konstrukty <literal>if</literal> mohou být libovolnì vnoøovány do jiných konstruktù <literal>if</literal>, co¾ poskytuje plnou flexibilitu podmínìného provádìní rùzných èástí programu. </simpara> </sect1> <sect1 id="control-structures.else"> <title><literal>else</literal></title> <para> Èasto také mù¾ete chtít nìco provádìt, pokud je jistá podmínka splnìna, a nìco jiného, kdy¾ splnìna není. To umo¾òuje konstrukt <literal>else</literal>. <literal>else</literal> roz¹iøuje konstrukt <literal>if</literal> o provádìní kódu v pøípadì, ¾e výraz v konstruktu <literal>if</literal> je ohodnocen jako &false;. Napøíklad následující kód vypí¹e <computeroutput>a je vìt¹í ne¾ b</computeroutput>, pokud <replaceable>$a</replaceable> je vìt¹í ne¾ <replaceable>$b</replaceable>, a <computeroutput>a NENÍ vìt¹í ne¾ b</computeroutput> v ostatních pøípadech: <informalexample> <programlisting role="php"> <![CDATA[ if ($a > $b) { print "a je vìt¹í ne¾ b"; } else { print "a NENÍ vìt¹í ne¾ b"; } ]]> </programlisting> </informalexample> Konstrukt v <literal>else</literal> se provádí, pouze pokud je výraz v <literal>if</literal> ohodnocen jako &false;, a pokud by zde byly nìjaké výrazy <literal>elseif</literal> - pouze pokud by byly také ohodnoceny jako &false; (viz <link linkend="control-structures.elseif">elseif</link>). </para> </sect1> <sect1 id="control-structures.elseif"> <title><literal>elseif</literal></title> <para> Jak název napovídá, <literal>elseif</literal>, je kombinací <literal>if</literal> a <literal>else</literal>. Stejnì jako <literal>else</literal>, roz¹iøuje konstrukt <literal>if</literal> k provádìní odli¹ných konstruktù v pøípadì, ¾e jevýraz pùvodního konstruktu <literal>if</literal> ohodnocen jako &false;. Tedy, narozdíl od <literal>else</literal>, se provádí pouze tehdy, je-li výraz v podmínce <literal>elseif</literal> ohodnocen jako &true;. Napøíklad následující kód vypí¹e <computeroutput>a je vìt¹í ne¾ b</computeroutput>, <computeroutput>a se rovná b</computeroutput> nebo <computeroutput>a je men¹í ne¾ b</computeroutput>: <informalexample> <programlisting role="php"> <![CDATA[ if ($a > $b) { print "a je vìt¹í ne¾ b"; } elseif ($a == $b) { print "a se rovná b"; } else { print "a je men¹í ne¾ b"; } ]]> </programlisting> </informalexample> </para> <simpara> V rámci jednoho konstruktu <literal>if</literal> mù¾e být více konstruktù <literal>elseif</literal>. Provádí se první konstrukt <literal>elseif</literal> (pokud vùbec nìjaký), jeho¾ výraz je ohodnocen &true;. V PHP mù¾ete napsat i 'else if' (dvìma slovy), chování bude naprosto toto¾né jako u 'elseif' (jedním slovem). Syntaktický význam je mírnì odli¹ný (znáte-li C, je to stejné), av¹ak ve výsledku dostaneme pøesnì toto¾né chování. </simpara> <simpara> Konstrukt <literal>elseif</literal> se provádí, pouze jsou-li pøíslu¹ný (bezprostøednì pøedcházející) výraz konstruktu <literal>if</literal> a výrazy v¹ech pøíslu¹ných pøedcházejících konstruktù <literal>elseif</literal> ohodnoceny jako &false;, a konkrétní výraz v <literal>elseif</literal> ohodnocen jako &true;. </simpara> </sect1> <sect1 id="control-structures.alternative-syntax"> <title>Alternativní syntaxe øídicích struktur</title> <para> PHP nabízí alternativní syntaxi pro nìkteré z øídicích struktur, jmenovitì <literal>if</literal>, <literal>while</literal>, <literal>for</literal>, <literal>foreach</literal>, a <literal>switch</literal>. V ka¾dém z tìchto pøípadù je základním formátem alternativní syntaxe zámìna otvírací závorky za dvojteèku (:) a uzavírací závorky za <literal>endif;</literal>, <literal>endwhile;</literal>, <literal>endfor;</literal>, <literal>endforeach;</literal>, resp. <literal>endswitch;</literal>. <informalexample> <programlisting role="php"> <![CDATA[ <?php if ($a == 5): ?> A se rovná 5 <?php endif; ?> ]]> </programlisting> </informalexample> </para> <simpara> In the above example, the HTML block "A = 5" is nested within an <literal>if</literal> statement written in the alternative syntax. The HTML block would be displayed only if $a is equal to 5. </simpara> <para> The alternative syntax applies to <literal>else</literal> and <literal>elseif</literal> as well. The following is an <literal>if</literal> structure with <literal>elseif</literal> and <literal>else</literal> in the alternative format: <informalexample> <programlisting role="php"> <![CDATA[ if ($a == 5): print "a equals 5"; print "..."; elseif ($a == 6): print "a equals 6"; print "!!!"; else: print "a is neither 5 nor 6"; endif; ]]> </programlisting> </informalexample> </para> <para> See also <link linkend="control-structures.while">while</link>, <link linkend="control-structures.for">for</link>, and <link linkend="control-structures.if">if</link> for further examples. </para> </sect1> <sect1 id="control-structures.while"> <title><literal>while</literal></title> <para> <literal>while</literal> loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a <literal>while</literal> statement is: <informalexample> <programlisting> <![CDATA[ while (expr) statement ]]> </programlisting> </informalexample> </para> <simpara> The meaning of a <literal>while</literal> statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the <literal>while</literal> expression evaluates to &true;. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the <literal>while</literal> expression evaluates to &false; from the very beginning, the nested statement(s) won't even be run once. </simpara> <para> Like with the <literal>if</literal> statement, you can group multiple statements within the same <literal>while</literal> loop by surrounding a group of statements with curly braces, or by using the alternate syntax: <informalexample> <programlisting> <![CDATA[ while (expr): statement ... endwhile; ]]> </programlisting> </informalexample> </para> <para> The following examples are identical, and both print numbers from 1 to 10: <informalexample> <programlisting role="php"> <![CDATA[ /* example 1 */ $i = 1; while ($i <= 10) { print $i++; /* the printed value would be $i before the increment (post-increment) */ } /* example 2 */ $i = 1; while ($i <= 10): print $i; $i++; endwhile; ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.do.while"> <title><literal>do..while</literal></title> <simpara> <literal>do..while</literal> loops are very similar to <literal>while</literal> loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular <literal>while</literal> loops is that the first iteration of a <literal>do..while</literal> loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it's may not necessarily run with a regular <literal>while</literal> loop (the truth expression is checked at the beginning of each iteration, if it evaluates to &false; right from the beginning, the loop execution would end immediately). </simpara> <para> There is just one syntax for <literal>do..while</literal> loops: <informalexample> <programlisting role="php"> <![CDATA[ $i = 0; do { print $i; } while ($i>0); ]]> </programlisting> </informalexample> </para> <simpara> The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to &false; ($i is not bigger than 0) and the loop execution ends. </simpara> <para> Advanced C users may be familiar with a different usage of the <literal>do..while</literal> loop, to allow stopping execution in the middle of code blocks, by encapsulating them with <literal>do..while</literal>(0), and using the <link linkend="control-structures.break"><literal>break</literal></link> statement. The following code fragment demonstrates this: <informalexample> <programlisting role="php"> <![CDATA[ do { if ($i < 5) { print "i is not big enough"; break; } $i *= $factor; if ($i < $minimum_limit) { break; } print "i is ok"; ...process i... } while(0); ]]> </programlisting> </informalexample> </para> <simpara> Don't worry if you don't understand this right away or at all. You can code scripts and even powerful scripts without using this `feature'. </simpara> </sect1> <sect1 id="control-structures.for"> <title><literal>for</literal></title> <para> <literal>for</literal> loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a <literal>for</literal> loop is: <informalexample> <programlisting> <![CDATA[ for (expr1; expr2; expr3) statement ]]> </programlisting> </informalexample> </para> <simpara> The first expression (<replaceable>expr1</replaceable>) is evaluated (executed) once unconditionally at the beginning of the loop. </simpara> <simpara> In the beginning of each iteration, <replaceable>expr2</replaceable> is evaluated. If it evaluates to &true;, the loop continues and the nested statement(s) are executed. If it evaluates to &false;, the execution of the loop ends. </simpara> <simpara> At the end of each iteration, <replaceable>expr3</replaceable> is evaluated (executed). </simpara> <simpara> Each of the expressions can be empty. <replaceable>expr2</replaceable> being empty means the loop should be run indefinitely (PHP implicitly considers it as &true;, like C). This may not be as useless as you might think, since often you'd want to end the loop using a conditional <link linkend="control-structures.break"><literal>break</literal></link> statement instead of using the <literal>for</literal> truth expression. </simpara> <para> Consider the following examples. All of them display numbers from 1 to 10: <informalexample> <programlisting role="php"> <![CDATA[ /* example 1 */ for ($i = 1; $i <= 10; $i++) { print $i; } /* example 2 */ for ($i = 1;;$i++) { if ($i > 10) { break; } print $i; } /* example 3 */ $i = 1; for (;;) { if ($i > 10) { break; } print $i; $i++; } /* example 4 */ for ($i = 1; $i <= 10; print $i, $i++); ]]> </programlisting> </informalexample> </para> <simpara> Of course, the first example appears to be the nicest one (or perhaps the fourth), but you may find that being able to use empty expressions in <literal>for</literal> loops comes in handy in many occasions. </simpara> <para> PHP also supports the alternate "colon syntax" for <literal>for</literal> loops. <informalexample> <programlisting> <![CDATA[ for (expr1; expr2; expr3): statement; ...; endfor; ]]> </programlisting> </informalexample> </para> <para> Other languages have a <literal>foreach</literal> statement to traverse an array or hash. PHP 3 has no such construct; PHP 4 does (see <link linkend="control-structures.foreach">foreach</link>). In PHP 3, you can combine <link linkend="control-structures.while">while</link> with the <function>list</function> and <function>each</function> functions to achieve the same effect. See the documentation for these functions for an example. </para> </sect1> <sect1 id="control-structures.foreach"> <title><literal>foreach</literal></title> <para> PHP 4 (not PHP 3) includes a <literal>foreach</literal> construct, much like Perl and some other languages. This simply gives an easy way to iterate over arrays. There are two syntaxes; the second is a minor but useful extension of the first: <informalexample> <programlisting> <![CDATA[ foreach(array_expression as $value) statement foreach(array_expression as $key => $value) statement ]]> </programlisting> </informalexample> </para> <simpara> The first form loops over the array given by <literal>array_expression</literal>. On each loop, the value of the current element is assigned to <literal>$value</literal> and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element). </simpara> <simpara> The second form does the same thing, except that the current element's key will be assigned to the variable <literal>$key</literal> on each loop. </simpara> <para> <note> <para> When <literal>foreach</literal> first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call <function>reset</function> before a <literal>foreach</literal> loop. </para> </note> </para> <para> <note> <para> Also note that <literal>foreach</literal> operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the <function>each</function> construct and changes to the array element returned are not reflected in the original array. </para> </note> </para> <note> <para> <literal>foreach</literal> does not support the ability to suppress error messages using '@'. </para> </note> <para> You may have noticed that the following are functionally identical: <informalexample> <programlisting role="php"> <![CDATA[ reset ($arr); while (list(, $value) = each ($arr)) { echo "Value: $value<br>\n"; } foreach ($arr as $value) { echo "Value: $value<br>\n"; } ]]> </programlisting> </informalexample> The following are also functionally identical: <informalexample> <programlisting role="php"> <![CDATA[ reset ($arr); while (list($key, $value) = each ($arr)) { echo "Key: $key; Value: $value<br>\n"; } foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br>\n"; } ]]> </programlisting> </informalexample> </para> <para> Some more examples to demonstrate usages: <informalexample> <programlisting role="php"> <![CDATA[ /* foreach example 1: value only */ $a = array (1, 2, 3, 17); foreach ($a as $v) { print "Current value of \$a: $v.\n"; } /* foreach example 2: value (with key printed for illustration) */ $a = array (1, 2, 3, 17); $i = 0; /* for illustrative purposes only */ foreach($a as $v) { print "\$a[$i] => $v.\n"; $i++; } /* foreach example 3: key and value */ $a = array ( "one" => 1, "two" => 2, "three" => 3, "seventeen" => 17 ); foreach($a as $k => $v) { print "\$a[$k] => $v.\n"; } /* foreach example 4: multi-dimensional arrays */ $a[0][0] = "a"; $a[0][1] = "b"; $a[1][0] = "y"; $a[1][1] = "z"; foreach($a as $v1) { foreach ($v1 as $v2) { print "$v2\n"; } } /* foreach example 5: dynamic arrays */ foreach(array(1, 2, 3, 4, 5) as $v) { print "$v\n"; } ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.break"> <title><literal>break</literal></title> <simpara> <literal>break</literal> ends execution of the current <literal>for</literal>, <literal>foreach</literal> <literal>while</literal>, <literal>do..while</literal> or <literal>switch</literal> structure. </simpara> <simpara> <literal>break</literal> accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. </simpara> <para> <informalexample> <programlisting role="php"> <![CDATA[ $arr = array ('one', 'two', 'three', 'four', 'stop', 'five'); while (list (, $val) = each ($arr)) { if ($val == 'stop') { break; /* You could also write 'break 1;' here. */ } echo "$val<br>\n"; } /* Using the optional argument. */ $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5<br>\n"; break 1; /* Exit only the switch. */ case 10: echo "At 10; quitting<br>\n"; break 2; /* Exit the switch and the while. */ default: break; } } ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.continue"> <title><literal>continue</literal></title> <simpara> <literal>continue</literal> is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration. </simpara> <simpara> <literal>continue</literal> accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. </simpara> <para> <informalexample> <programlisting role="php"> <![CDATA[ while (list ($key, $value) = each ($arr)) { if (!($key % 2)) { // skip odd members continue; } do_something_odd ($value); } $i = 0; while ($i++ < 5) { echo "Outer<br>\n"; while (1) { echo " Middle<br>\n"; while (1) { echo " Inner<br>\n"; continue 3; } echo "This never gets output.<br>\n"; } echo "Neither does this.<br>\n"; } ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.switch"> <title><literal>switch</literal></title> <simpara> The <literal>switch</literal> statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the <literal>switch</literal> statement is for. </simpara> <para> The following two examples are two different ways to write the same thing, one using a series of <literal>if</literal> statements, and the other using the <literal>switch</literal> statement: <informalexample> <programlisting role="php"> <![CDATA[ if ($i == 0) { print "i equals 0"; } if ($i == 1) { print "i equals 1"; } if ($i == 2) { print "i equals 2"; } switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; } ]]> </programlisting> </informalexample> </para> <para> It is important to understand how the <literal>switch</literal> statement is executed in order to avoid mistakes. The <literal>switch</literal> statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a <literal>case</literal> statement is found with a value that matches the value of the <literal>switch</literal> expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the <literal>switch</literal> block, or the first time it sees a <literal>break</literal> statement. If you don't write a <literal>break</literal> statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example: <informalexample> <programlisting role="php"> <![CDATA[ switch ($i) { case 0: print "i equals 0"; case 1: print "i equals 1"; case 2: print "i equals 2"; } ]]> </programlisting> </informalexample> </para> <simpara> Here, if $i equals to 0, PHP would execute all of the print statements! If $i equals to 1, PHP would execute the last two print statements, and only if $i equals to 2, you'd get the 'expected' behavior and only 'i equals 2' would be displayed. So, it's important not to forget <literal>break</literal> statements (even though you may want to avoid supplying them on purpose under certain circumstances). </simpara> <simpara> In a <literal>switch</literal> statement, the condition is evaluated only once and the result is compared to each <literal>case</literal> statement. In an <literal>elseif</literal> statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a <literal>switch</literal> may be faster. </simpara> <para> The statement list for a case can also be empty, which simply passes control into the statement list for the next case. <informalexample> <programlisting role="php"> <![CDATA[ switch ($i) { case 0: case 1: case 2: print "i is less than 3 but not negative"; break; case 3: print "i is 3"; } ]]> </programlisting> </informalexample> </para> <para> A special case is the default case. This case matches anything that wasn't matched by the other cases, and should be the last <literal>case</literal> statement. For example: <informalexample> <programlisting role="php"> <![CDATA[ switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; default: print "i is not equal to 0, 1 or 2"; } ]]> </programlisting> </informalexample> </para> <para> The <literal>case</literal> expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type. </para> <para> The alternative syntax for control structures is supported with switches. For more information, see <link linkend="control-structures.alternative-syntax">Alternative syntax for control structures</link> . <informalexample> <programlisting role="php"> <![CDATA[ switch ($i): case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; default: print "i is not equal to 0, 1 or 2"; endswitch; ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.declare"> <title><literal>declare</literal></title> <para> The <literal>declare</literal> construct is used to set execution directives for a block of code. The syntax of <literal>declare</literal> is similar to the syntax of other flow control constructs: <informalexample> <programlisting> <![CDATA[ declare (directive) statement ]]> </programlisting> </informalexample> </para> <para> The <literal>directive</literal> section allows the behavior of the <literal>declare</literal> block to be set. Currently only one directive is recognized: the <literal>ticks</literal> directive. (See below for more information on the <link linkend="control-structures.declare.ticks">ticks</link> directive) </para> <para> The <literal>statement</literal> part of the <literal>declare</literal> block will be executed - how it is executed and what side-effects occur during execution may depend on the directive set in the <literal>directive</literal> block. </para> <sect2 id="control-structures.declare.ticks"> <title>Ticks</title> <para>A tick is an event that occurs for every <replaceable>N</replaceable> low-level statements executed by the parser within the <literal>declare</literal> block. The value for <replaceable>N</replaceable> is specified using <literal>ticks=<replaceable>N</replaceable></literal> within the <literal>declare</literal> blocks's <literal>directive</literal> section. </para> <para> The event(s) that occurs on each tick is specified using the <function>register_tick_function</function>. See the example below for more details. Note that more than one event can occur for each tick. </para> <para> <example> <title>Profile a section of PHP code</title> <programlisting role="php"> <![CDATA[ <pre> <?php // A function that records the time when it is called function profile ($dump = FALSE) { static $profile; // Return the times stored in profile, then erase it if ($dump) { $temp = $profile; unset ($profile); return ($temp); } $profile[] = microtime (); } // Set up a tick handler register_tick_function("profile"); // Initialize the function before the declare block profile (); // Run a block of code, throw a tick every 2nd statement declare (ticks=2) { for ($x = 1; $x < 50; ++$x) { echo similar_text (md5($x), md5($x*$x)), "<br>"; } } // Display the data stored in the profiler print_r (profile (TRUE)); ?> </pre> ]]> </programlisting> </example> The example profiles the PHP code within the 'declare' block, recording the time at which every second low-level statement in the block was executed. This information can then be used to find the slow areas within particular segments of code. This process can be performed using other methods: using ticks is more convenient and easier to implement. </para> <simpara> Ticks are well suited for debugging, implementing simple multitasking, backgrounded I/O and many other tasks. </simpara> <simpara> See also <function>register_tick_function</function> and <function>unregister_tick_function</function>. </simpara> </sect2> </sect1> <sect1 id="function.return"> <title>return</title> <simpara> If called from within a function, the <function>return</function> statement immediately ends execution of the current function, and returns its argument as the value of the function call. <function>return</function> will also end the execution of an <function>eval</function> statement or script file. </simpara> <simpara> If called from the global scope, then execution of the current script file is ended. If the current script file was <function>include</function>ed or <function>require</function>ed, then control is passed back to the calling file. Furthermore, if the current script file was <function>include</function>ed, then the value given to <function>return</function> will be returned as the value of the <function>include</function> call. If <function>return</function> is called from within the main script file, then script execution ends. If the current script file was named by the <link linkend="ini.auto-prepend-file">auto_prepend_file</link> or <link linkend="ini.auto-append-file">auto_append_file</link> configuration options in <link linkend="configuration.file">the configuration file</link>, then that script file's execution is ended. </simpara> <simpara>For more information, see <link linkend="functions.returning-values">Returning values</link>. </simpara> <note> <simpara> Note that since <function>return</function> is a language construct and not a function, the parentheses surrounding its arguments are <emphasis>not</emphasis> required--in fact, it is more common to leave them out than to use them, although it doesn't matter one way or the other. </simpara> </note> </sect1> <sect1 id="function.require"> <title><function>require</function></title> <simpara> The <function>require</function> statement includes and evaluates the specific file. </simpara> <simpara> <function>require</function> includes and evaluates a specific file. Detailed information on how this inclusion works is described in the documentation for <function>include</function>. </simpara> <simpara> <function>require</function> and <function>include</function> are identical in every way except how they handle failure. <function>include</function> produces a <link linkend="internal.e-warning">Warning</link> while <function>require</function> results in a <link linkend="internal.e-error"> Fatal Error</link>. In other words, don't hesitate to use <function>require</function> if you want a missing file to halt processing of the page. <function>include</function> does not behave this way, the script will continue regardless. Be sure to have an appropriate <link linkend="ini.include-path">include_path</link> setting as well. </simpara> <para> <example> <title>Basic <function>require</function> examples</title> <programlisting role="php"> <![CDATA[ <?php require 'prepend.php'; require $somefile; require ('somefile.txt'); ?> ]]> </programlisting> </example> </para> <simpara> See the <function>include</function> documentation for more examples. </simpara> <note> <simpara> Prior to PHP 4.0.2, the following applies: <function>require</function> will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect <function>require</function>. However, if the line on which the <function>require</function> occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of <function>require</function>. Although the code contained in the target file is still subject to the loop, the <function>require</function> itself happens only once. </simpara> </note> <simpara> See also <function>include</function>, <function>require_once</function>, <function>include_once</function>, <function>eval</function>, <function>file</function>, <function>readfile</function>, <function>virtual</function> and <link linkend="ini.include-path">include_path</link>. </simpara> </sect1> <sect1 id="function.include"> <title><function>include</function></title> <simpara> The <function>include</function> statement includes and evaluates the specified file. </simpara> <simpara> The documentation below also applies to <function>require</function>. The two constructs are identical in every way except how they handle failure. <function>include</function> produces a <link linkend="internal.e-warning">Warning</link> while <function>require</function> results in a <link linkend="internal.e-error">Fatal Error</link>. In other words, use <function>require</function> if you want a missing file to halt processing of the page. <function>include</function> does not behave this way, the script will continue regardless. Be sure to have an appropriate <link linkend="ini.include-path">include_path</link> setting as well. </simpara> <simpara> When a file is included, the code it contains inherits the <link linkend="language.variables.scope">variable scope</link> of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. </simpara> <para> <example> <title>Basic <function>include</function> example</title> <programlisting role="php"> <![CDATA[ vars.php <?php $color = 'green'; $fruit = 'apple'; ?> test.php <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?> ]]> </programlisting> </example> </para> <simpara> If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. </simpara> <para> <example> <title>Including within functions</title> <programlisting role="php"> <![CDATA[ <?php function foo() { global $color; include 'vars.php'; echo "A $color $fruit"; } /* vars.php is in the scope of foo() so * * $fruit is NOT available outside of this * * scope. $color is because we declared it * * as global. */ foo(); // A green apple echo "A $color $fruit"; // A green ?> ]]> </programlisting> </example> </para> <simpara> When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within <link linkend="language.basic-syntax.phpmode">valid PHP start and end tags</link>. </simpara> <simpara> If "<link linkend="ini.allow-url-fopen">URL fopen wrappers</link>" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using an URL (via HTTP) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using an URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script. </simpara> <para> <example> <title><function>include</function> through HTTP</title> <programlisting role="php"> <![CDATA[ <?php /* This example assumes that www.example.com is configured to parse .php * * files and not .txt files. Also, 'Works' here means that the variables * * $foo and $bar are available within the included file. */ // Won't work; file.txt wasn't handled by www.example.com as PHP include 'http://www.example.com/file.txt?foo=1&bar=2'; // Won't work; looks for a file named 'file.php?foo=1&bar=2' on the // local filesystem. include 'file.php?foo=1&bar=2'; // Works. include 'http://www.example.com/file.php?foo=1&bar=2'; $foo = 1; $bar = 2; include 'file.txt'; // Works. include 'file.php'; // Works. ?> ]]> </programlisting> </example> See also <link linkend="features.remote-files">Remote files</link>, <function>fopen</function> and <function>file</function> for related information. </para> <para> Because <function>include</function> and <function>require</function> are special language constructs, you must enclose them within a statement block if it's inside a conditional block. </para> <para> <example> <title>include() and conditional blocks</title> <programlisting role="php"> <![CDATA[ <?php // This is WRONG and will not work as desired. if ($condition) include $file; else include $other; // This is CORRECT. if ($condition) { include $file; } else { include $other; } ?> ]]> </programlisting> </example> </para> <simpara> Handling Returns: It is possible to execute a <function>return</function> statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function. </simpara> <note> <simpara> In PHP 3, the return may not appear inside a block unless it's a function block, in which case the <function>return</function> applies to that function and not the whole file. </simpara> </note> <para> <example> <title><function>include</function> and the <function>return</function> statement</title> <programlisting role="php"> <![CDATA[ return.php <?php $var = 'PHP'; return $var; ?> noreturn.php <?php $var = 'PHP'; ?> testreturns.php <?php $foo = include 'return.php'; echo $foo; // prints 'PHP' $bar = include 'noreturn.php'; echo $bar; // prints 1 ?> ]]> </programlisting> </example> </para> <simpara> <literal>$bar</literal> is the value <literal>1</literal> because the include was successful. Notice the difference between the above examples. The first uses <function>return</function> within the included file while the other does not. A few other ways to "include" files into variables are with <function>fopen</function>, <function>file</function> or by using <function>include</function> along with <link linkend="ref.outcontrol">Output Control Functions</link>. </simpara> <simpara> See also <function>require</function>, <function>require_once</function>, <function>include_once</function>, <function>readfile</function>, <function>virtual</function>, and <link linkend="ini.include-path">include_path</link>. </simpara> </sect1> <sect1 id="function.require-once"> <title><function>require_once</function></title> <para> The <function>require_once</function> statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the <function>require</function> statement, with the only difference being that if the code from a file has already been included, it will not be included again. See the documentation for <function>require</function> for more information on how this statement works. </para> <para> <function>require_once</function> should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc. </para> <para> For examples on using <function>require_once</function> and <function>include_once</function>, look at the <ulink url="&url.php.pear;">PEAR</ulink> code included in the latest PHP source code distributions. </para> <note> <para> <function>require_once</function> was added in PHP 4.0.1pl2 </para> </note> <para> See also: <function>require</function>, <function>include</function>, <function>include_once</function>, <function>get_required_files</function>, <function>get_included_files</function>, <function>readfile</function>, and <function>virtual</function>. </para> </sect1> <sect1 id="function.include-once"> <title><function>include_once</function></title> <para> The <function>include_once</function> statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the <function>include</function> statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once. </para> <para> <function>include_once</function> should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc. </para> <para> For more examples on using <function>require_once</function> and <function>include_once</function>, look at the <ulink url="&url.php.pear;">PEAR</ulink> code included in the latest PHP source code distributions. </para> <note> <para> <function>include_once</function> was added in PHP 4.0.1pl2 </para> </note> <para> See also <function>include</function>, <function>require</function>, <function>require_once</function>, <function>get_required_files</function>, <function>get_included_files</function>, <function>readfile</function>, and <function>virtual</function>. </para> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/cs/language/expressions.xml +++ phpdoc/cs/language/expressions.xml <?xml version="1.0" encoding="iso-8859-2"?> <!-- EN-Revision: 1.19 Maintainer: luk Status: ready --> <chapter id="language.expressions"> <title>Výrazy</title> <simpara> Výrazy jsou nejdùle¾itìj¹ími stavebními kameny PHP. V PHP je témìø v¹e, co napí¹ete, výraz. Nejjednodu¹¹í, a pøece nejpøesnìj¹í definicí výrazu je "v¹echno, co má hodnotu". </simpara> <simpara> Nejzákladnìj¹ími formami výrazù jsou konstanty a promìnné. Kdy¾ napí¹ete "$a = 5", pøiøazujete '5' do $a. '5' má, pochopitelnì, hodnotu 5, nebo jinými slovy, '5' je výraz s hodnotou 5 (v tomto pøípadì je '5' celoèíselnou konstantou). </simpara> <simpara> Po tomto pøiøazení budete oèekávat, ¾e hodnota $a bude 5, tak¾e kdybyste napsali $b = $a, oèekávali byste toté¾, jako pøi napsání $b = 5. Jinými slovy, $a je tedy výraz s hodnotou 5. Pokud v¹e pracuje správnì, pøesnì to se také stane. </simpara> <para> O nìco slo¾itìj¹ím pøíkladem výrazù jsou funkce. Uva¾ujme napø. tuto funkci: <informalexample> <programlisting role="php"> <![CDATA[ function foo () { return 5; } ]]> </programlisting> </informalexample></para> <simpara> Za pøedpokladu, ¾e jste dobøe seznámeni s konceptem funkcí (pokud ne, nahlédnìte do kapitoly o funkcích), byste pøedpokládali, ¾e napsání <literal>$c = foo()</literal> je v zásadì toté¾ jako <literal>$c = 5</literal>, a máte pravdu. Funkce jsou výrazy s hodnotou jejich návratové hodnoty. Funkce foo() vrací 5, tudí¾ hodnota výrazu 'foo()' je 5. Obvykle funkce nevracejí konstantní hodnotu, nýbr¾ nìco vypoèítávají. </simpara> <simpara> Hodnoty v PHP samozøejmì nemusejí být pouze celá èísla, a velmi èasto také nejsou. PHP podporuje tøi typy skalárních hodnot: celoèíselné, reálné (pohyblivá øádová èárka) a øetìzce (skalární hodnoty jsou hodnoty, které nejde "rozbít" na men¹í èásti, narozdíl napø. od polí). PHP podporuje také dva kompozitní (neskalární) typy: pole a objekty. Ka¾dý z tìchto typù hodnot mù¾e být pøiøazen do promìnné nebo vracen z funkce. </simpara> <simpara> U¾ivatelé PHP/FI 2 by nemìli pocítit zmìnu. Ale PHP jde ve výrazech mnohem dále, stejnì jako mnoho jiných programovacích jazykù. PHP je výrazovì orientovaný jazyk, ve smyslu, ¾e témìø v¹e je výraz. Uva¾ujme pøíklad, kterým jsme se ji¾ zabývali, '$a = 5'. Ihned vidíme, ¾e jsou zde zahrnuty dvì hodnoty, celoèíselná konstanta '5' a hodnota $a, která je aktualizována na 5. Ale je pravda, ¾e je tu je¹tì jedna hodnota, je to hodnota samotného pøiøazení. Pøiøazení jako takové ohodnocuje pøiøazovanou hodnotu, v tomto pøípadì 5. V praxi to znamená, ¾e '$a = 5', bez ohledu na to co dìlá, je výraz s hodnotou 5. Proto je '$b = ($a = 5)' toté¾ jako '$a = 5; $b = 5;' (støedník oznaèuje konec výrazu). Proto¾e pøiøezení jsou parsována zprava doleva, mù¾ete také napsat '$b = $a = 5'. </simpara> <simpara> Jiným dobrým pøíkladem orientace na výrazy je pre- a post-inkrementace a dekrementace. U¾ivatelé PHP/FI 2 a mnoha jiných jazykù znají notaci promìnná++ a promìnná--. To jsou inkrementaèní a dekrementaèní operátory. V PHP/FI 2 nemìlo '$a++' ¾ádnou hodnotu (není to výraz), a proto ne¹lo pøiøadit nebo jinak pou¾ít. PHP roz¹iøuje schopnosti pøemìnou tìchto konstrukcí ve výrazy, jako v C. V PHP, stejnì jako v C, existují dva typy inkrementace - pre-inkrementace a post-inkrementace. Oba ve své podstatì inkrementují promìnnou a efekt na tuto promìnnou je toto¾ný. Rozdíl je v hodnotì inkrementaèního výrazu. Pre-inkrementace, zapsaná jako '++$var', ohodnocuje výraz inkrementovanou hodnotou (PHP inkrementuje promìnnou døíve, ne¾ pøeète její hodnotu, proto "pre-inkrementace"). Post-inkrementace, zapsaná jako '$var++', ohodnocuje výraz pùvodní hodnotou promìnné $var, pøed inkrementací (PHP inkrementuje promìnnou po pøeètení její hodnoty, proto "post-inkrement"). </simpara> <simpara> Velmi èastým typem výrazù jsou výrazy porovnávací. Tyto výrazy se ohodnocují 0 a 1 ve významu &false;, resp. &true;. PHP podporuje > (vìt¹í ne¾), >= (vìt¹í nebo rovno), == (rovná se), != (nerovná se), < (men¹í ne¾) a <= (men¹í nebo rovno). Tyto výrazy se nejèastìji pou¾ívají v podmínkách, jako je konstrukt <literal>if</literal>. </simpara> <simpara> Posledním pøíkladem výrazù, kterým se budeme zabývat, je kombinací pøiøazení a operátorù. Ji¾ víte, ¾e kdy¾ chcete inkrementovat $a o jednièku, jednodu¹e napí¹ete '$a++' nebo '++$a'. Ale co kdy¾ chcete hodnotu zvý¹it o jiné èíslo, napø. o 3? Mohli byste napsat '$a++' víckrát za sebou, ale to samozøejmì není efektivní ani pohodlné. Mnohem praktiètìj¹í je napsat '$a = $a + 3'. Výraz '$a + 3' ohodnocuje hodnotu $a plus 3 a je pøiøazen zpìt do $a, co¾ dává $a inkrementované o 3. V PHP, stejnì jako v øadì jiných jazykù (jako je C), to mù¾ete napsat krat¹ím zpùsobem, který se èasem stane jasnìj¹í i rychlej¹í k pochopení. Pøiètení 3 k aktuální hodnotì $a lze zapsat jako '$a += 3'. Pøesnì to znamená "vezmi hodnotu $a, pøièti k ní 3 a pøiøaï zpìt do $a". Kromì krat¹ího a pøehlednìj¹ího zápisu je výhodou také rychlej¹í provedení. Hodnota '$a += 3', jako hodnota regulérního pøiøazení, je pøiøazovaná hodnota. Uvìdomte si, ¾e to NENÍ 3, nýbr¾ $a plus 3 (co¾ je hodnota výrazu pøiøazovaného do $a). Takto lze pou¾ít jakýkoli binární operátor, napøíklad '$a -= 5' (odeèti 5 od hodnoty $a), '$b *= 7' (vynásob hodnotu $b èíslem 7) apod. </simpara> <para> Je tu je¹tì jeden výraz, který se mù¾e zdát zvlá¹tní, pokud jste ho je¹tì nevidìli v jiných jazycích: ternární podmínìný operátor: <informalexample><programlisting> <![CDATA[ $prvni ? $druhy : $treti ]]> </programlisting></informalexample> Pokud hodnota prvního podvýrazu je &true; (nenulová), je ohodnocen druhý podvýraz a je výsledkem celého podmínìného výrazu. Jinak je ohodnocen tøetí podvýraz a je pak hodotou celého výrazu. </para> <para> Následující pøíklad by mìl pomoci lépe pochopit pre- a post-inkrementaci i výrazy obecnì: <informalexample> <programlisting role="php"> <![CDATA[ function double($i) { return $i*2; } $b = $a = 5; /* pøiøaï hodnotu pìt do promìnných $a a $b */ $c = $a++; /* proveï post-inkrement, pøiøaï pùvodní hodnotu $a (5) do $c */ $e = $d = ++$b; /* proveï pre-inkrement, pøiøaï inkrementovanou hodnotu $b (6) do $d a $e */ /* nyní jsou hodnoty promìnných $d a $e rovny 6 */ $f = double($d++); /* pøiøaï dvakrát hodnotu $d <emphasis>pøed</emphasis> inkrementací, 2*6 = 12, do $f */ $g = double(++$e); /* pøiøaï dvakrát hodnotu $e <emphasis>po</emphasis> inkrementaci, 2*7 = 14 do $g */ $h = $g += 10; /* nejdøíve je $g inkrementováno o 10 má pak hodnotu 24. Hodnota pøiøazení (24) se pøiøadí do $h a $h tím získává také hodnotu 24. */ ]]> </programlisting> </informalexample> </para> <simpara> Na zaèátku kapitoly bylo øeèeno, ¾e si popí¹eme rùzné typy konstruktù, a jak bylo slíbeno, výrazy mohou být konstrukty. V tomto pøípadì mají konstrukty formát 'expr' ';', co¾ znamená "výraz následovaný støedníkem. V konstruktu '$b=$a=5;', je $a=5 platný výraz, ale samo o sobì to není konstrukt.'$b=$a=5;' je i platný konstrukt. </simpara> <simpara> <emphasis> Pozn. pøekladatele: Pøedchozím odstavci (obèas i jinde) pou¾ívám termín "konstrukt" pro anglické slovo "statement". Tento pøeklad není pøíli¹ korektní, ale v èeské programátorské mluvì neexistuje vhodný termín. Kdyby nìkdo vìdìl o lep¹ím, napi¹te mi, prosím, na luk@php.net. </emphasis> </simpara> <simpara> Poslední vìcí, která si zaslou¾í zmínku, je pravdivostní hodnota výrazù. V mnoha pøípadech, hlavnì podmínkách a cyklech, vás nezajímá konkrétní hodnota výrazu, nýbr¾ pouze to, jestli je &true; nebo &false;. <!-- (PHP doesn't have a dedicated boolean type) : WRONG, php does. --> Konstanty &true; a &false; (malá/velká písmena nehrají roli) pøedstavují dvì mo¾né boolovské (pravdivostní) hodnoty. V pøípadì potøeby je výraz automaticky pøeveden na typ boolean. Detailnìj¹í informace o zpùsobu konverze - viz <link linkend="language.types.typecasting">sekce o typové konverzi</link>. </simpara> <simpara> PHP poskytuje plnou a silnou implementaci výrazù a úplnì je zdokumentovat pøesahuje rozsah tohoto manuálu. Vý¹e uvedené pøíklady by vám mìli naznaèit, co jsou vùbec výrazy a jak konstruovat u¾iteèné výrazy. Ve zbývající èásti manuálu budeme psát <replaceable>expr</replaceable> jako¾to jakýkoli platný PHP výraz. </simpara> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: -->
Index: phpdoc/cs/translation.xml diff -u phpdoc/cs/translation.xml:1.4 phpdoc/cs/translation.xml:1.5 --- phpdoc/cs/translation.xml:1.4 Sun Mar 17 17:16:09 2002 +++ phpdoc/cs/translation.xml Mon Mar 25 18:07:15 2002 @@ -19,7 +19,6 @@ <file name="appendices/resources.xml" person="luk" type="translation" /> <file name="functions/mysql.xml" person="costra" type="translation" /> <file name="language/control-structures.xml" person="luk" type="translation" /> - <file name="language/expressions.xml" person="luk" type="translation" /> <file name="language/operators.xml" person="luk" type="translation" /> <file name="language/oop.xml" person="luk" type="translation" /> <file name="language/types.xml" person="luk" type="translation" /> Index: phpdoc/cs/language/references.xml diff -u phpdoc/cs/language/references.xml:1.2 phpdoc/cs/language/references.xml:1.3 --- phpdoc/cs/language/references.xml:1.2 Sun Mar 17 17:16:09 2002 +++ phpdoc/cs/language/references.xml Mon Mar 25 18:07:15 2002 @@ -7,8 +7,8 @@ <sect1 id="language.references.whatare"> <title>Co jsou reference</title> <simpara> - Reference (odkazy) jsou prostøedek, ja v PHP pøistupovat k tému¾ obsahu - promìnné pod rùznými jmény. Nejsou to ukazatele (pointery jako v C, jsou + Reference (odkazy) jsou prostøedek, jak v PHP pøistupovat k tému¾ obsahu + promìnné pod rùznými jmény. Nejsou to ukazatele (pointery) jako v C, jsou to aliasy v tabulce symbolù. Uvìdomte si, ¾e v PHP je rozdíl mezi názvem promìnné a jejím obsahem, tak¾e stejný obsah mù¾e mít rùzné názvy. Nejbli¾¹í analogií jsou názvy souborù a soubory v UNIXu - názvy Index: phpdoc/cs/language/control-structures.xml +++ phpdoc/cs/language/control-structures.xml <?xml version="1.0" encoding="iso-8859-2"?> <!-- EN-Revision: 1.55 Maintainer: luk Status: mixed --> <chapter id="control-structures"> <title>Øídicí struktury</title> <simpara> Jakýkoli PHP skript je slo¾en ze série konstruktù. Konstrukt mù¾e být pøiøazení, volání funkce, cyklus, podmínka, stejnì jako konstrukt, který nic nedìlá (prázdný konstrukt). Konstrukt obvykle konèí støedníkem. Navíc lze konstrukty seskupit do skupiny (bloku) uzavøené slo¾enými závorkami. Tento blok je sám o sobì konstruktem. V této kapitole jsou popsány rùzné typy konstruktù. </simpara> <sect1 id="control-structures.if"> <title><literal>if</literal></title> <para> Konstrukt <literal>if</literal> je jedním z nejdùle¾itìj¹ích prvkù v mnoha jazycích, vèetnì PHP. Umo¾òuje podmínìné provádìní kusu kódu. Struktura <literal>if</literal> v PHP je podobná struktuøe v C: <informalexample> <programlisting> <![CDATA[ if (expr) statement ]]> </programlisting> </informalexample> </para> <simpara> Jak je popsáno v <link linkend="language.expressions">sekci o výrazech</link>, výraz <replaceable>expr</replaceable> je ohodnoce svou boolovskou hodnotou. Poku je <replaceable>expr</replaceable> ohodnocen jako &true;, PHP provede <replaceable>statement</replaceable>; je-li ohodnocen jako &false;, neprovede se nic. Více informací o to, jak se výrazy ohodnocují jako &false; najdete v èásti <link linkend="language.types.boolean.casting">'Konverze na typ boolean'</link>. </simpara> <para> Následující pøíklad by vypsal <computeroutput>a je vìt¹í ne¾ b</computeroutput>, pokud <replaceable>$a</replaceable> je vìt¹í ne¾ <replaceable>$b</replaceable>: <informalexample> <programlisting role="php"> <![CDATA[ if ($a > $b) print "a je vìt¹í ne¾ b"; ]]> </programlisting> </informalexample> </para> <para> Èasto byste chtìli, aby se podmínìnì provádìl více ne¾ jeden konstrukt. Není samozøejmì nutné ka¾dý konstrukt zabalit do struktury <literal>if</literal>. Místo toho mù¾ete seskupit více konstruktù do bloku. Napøíklad tento kód by zobrazil <computeroutput>a je vìt¹í ne¾ b</computeroutput>, pokud <replaceable>$a</replaceable> je vìt¹í ne¾ <replaceable>$b</replaceable> a pøiøadil by hodnotu <replaceable>$a</replaceable> do <replaceable>$b</replaceable>: <informalexample> <programlisting role="php"> <![CDATA[ if ($a > $b) { print "a je vìt¹í ne¾ b"; $b = $a; } ]]> </programlisting> </informalexample> </para> <simpara> Konstrukty <literal>if</literal> mohou být libovolnì vnoøovány do jiných konstruktù <literal>if</literal>, co¾ poskytuje plnou flexibilitu podmínìného provádìní rùzných èástí programu. </simpara> </sect1> <sect1 id="control-structures.else"> <title><literal>else</literal></title> <para> Èasto také mù¾ete chtít nìco provádìt, pokud je jistá podmínka splnìna, a nìco jiného, kdy¾ splnìna není. To umo¾òuje konstrukt <literal>else</literal>. <literal>else</literal> roz¹iøuje konstrukt <literal>if</literal> o provádìní kódu v pøípadì, ¾e výraz v konstruktu <literal>if</literal> je ohodnocen jako &false;. Napøíklad následující kód vypí¹e <computeroutput>a je vìt¹í ne¾ b</computeroutput>, pokud <replaceable>$a</replaceable> je vìt¹í ne¾ <replaceable>$b</replaceable>, a <computeroutput>a NENÍ vìt¹í ne¾ b</computeroutput> v ostatních pøípadech: <informalexample> <programlisting role="php"> <![CDATA[ if ($a > $b) { print "a je vìt¹í ne¾ b"; } else { print "a NENÍ vìt¹í ne¾ b"; } ]]> </programlisting> </informalexample> Konstrukt v <literal>else</literal> se provádí, pouze pokud je výraz v <literal>if</literal> ohodnocen jako &false;, a pokud by zde byly nìjaké výrazy <literal>elseif</literal> - pouze pokud by byly také ohodnoceny jako &false; (viz <link linkend="control-structures.elseif">elseif</link>). </para> </sect1> <sect1 id="control-structures.elseif"> <title><literal>elseif</literal></title> <para> Jak název napovídá, <literal>elseif</literal>, je kombinací <literal>if</literal> a <literal>else</literal>. Stejnì jako <literal>else</literal>, roz¹iøuje konstrukt <literal>if</literal> k provádìní odli¹ných konstruktù v pøípadì, ¾e jevýraz pùvodního konstruktu <literal>if</literal> ohodnocen jako &false;. Tedy, narozdíl od <literal>else</literal>, se provádí pouze tehdy, je-li výraz v podmínce <literal>elseif</literal> ohodnocen jako &true;. Napøíklad následující kód vypí¹e <computeroutput>a je vìt¹í ne¾ b</computeroutput>, <computeroutput>a se rovná b</computeroutput> nebo <computeroutput>a je men¹í ne¾ b</computeroutput>: <informalexample> <programlisting role="php"> <![CDATA[ if ($a > $b) { print "a je vìt¹í ne¾ b"; } elseif ($a == $b) { print "a se rovná b"; } else { print "a je men¹í ne¾ b"; } ]]> </programlisting> </informalexample> </para> <simpara> V rámci jednoho konstruktu <literal>if</literal> mù¾e být více konstruktù <literal>elseif</literal>. Provádí se první konstrukt <literal>elseif</literal> (pokud vùbec nìjaký), jeho¾ výraz je ohodnocen &true;. V PHP mù¾ete napsat i 'else if' (dvìma slovy), chování bude naprosto toto¾né jako u 'elseif' (jedním slovem). Syntaktický význam je mírnì odli¹ný (znáte-li C, je to stejné), av¹ak ve výsledku dostaneme pøesnì toto¾né chování. </simpara> <simpara> Konstrukt <literal>elseif</literal> se provádí, pouze jsou-li pøíslu¹ný (bezprostøednì pøedcházející) výraz konstruktu <literal>if</literal> a výrazy v¹ech pøíslu¹ných pøedcházejících konstruktù <literal>elseif</literal> ohodnoceny jako &false;, a konkrétní výraz v <literal>elseif</literal> ohodnocen jako &true;. </simpara> </sect1> <sect1 id="control-structures.alternative-syntax"> <title>Alternativní syntaxe øídicích struktur</title> <para> PHP nabízí alternativní syntaxi pro nìkteré z øídicích struktur, jmenovitì <literal>if</literal>, <literal>while</literal>, <literal>for</literal>, <literal>foreach</literal>, a <literal>switch</literal>. V ka¾dém z tìchto pøípadù je základním formátem alternativní syntaxe zámìna otvírací závorky za dvojteèku (:) a uzavírací závorky za <literal>endif;</literal>, <literal>endwhile;</literal>, <literal>endfor;</literal>, <literal>endforeach;</literal>, resp. <literal>endswitch;</literal>. <informalexample> <programlisting role="php"> <![CDATA[ <?php if ($a == 5): ?> A se rovná 5 <?php endif; ?> ]]> </programlisting> </informalexample> </para> <simpara> In the above example, the HTML block "A = 5" is nested within an <literal>if</literal> statement written in the alternative syntax. The HTML block would be displayed only if $a is equal to 5. </simpara> <para> The alternative syntax applies to <literal>else</literal> and <literal>elseif</literal> as well. The following is an <literal>if</literal> structure with <literal>elseif</literal> and <literal>else</literal> in the alternative format: <informalexample> <programlisting role="php"> <![CDATA[ if ($a == 5): print "a equals 5"; print "..."; elseif ($a == 6): print "a equals 6"; print "!!!"; else: print "a is neither 5 nor 6"; endif; ]]> </programlisting> </informalexample> </para> <para> See also <link linkend="control-structures.while">while</link>, <link linkend="control-structures.for">for</link>, and <link linkend="control-structures.if">if</link> for further examples. </para> </sect1> <sect1 id="control-structures.while"> <title><literal>while</literal></title> <para> <literal>while</literal> loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a <literal>while</literal> statement is: <informalexample> <programlisting> <![CDATA[ while (expr) statement ]]> </programlisting> </informalexample> </para> <simpara> The meaning of a <literal>while</literal> statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the <literal>while</literal> expression evaluates to &true;. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the <literal>while</literal> expression evaluates to &false; from the very beginning, the nested statement(s) won't even be run once. </simpara> <para> Like with the <literal>if</literal> statement, you can group multiple statements within the same <literal>while</literal> loop by surrounding a group of statements with curly braces, or by using the alternate syntax: <informalexample> <programlisting> <![CDATA[ while (expr): statement ... endwhile; ]]> </programlisting> </informalexample> </para> <para> The following examples are identical, and both print numbers from 1 to 10: <informalexample> <programlisting role="php"> <![CDATA[ /* example 1 */ $i = 1; while ($i <= 10) { print $i++; /* the printed value would be $i before the increment (post-increment) */ } /* example 2 */ $i = 1; while ($i <= 10): print $i; $i++; endwhile; ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.do.while"> <title><literal>do..while</literal></title> <simpara> <literal>do..while</literal> loops are very similar to <literal>while</literal> loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular <literal>while</literal> loops is that the first iteration of a <literal>do..while</literal> loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it's may not necessarily run with a regular <literal>while</literal> loop (the truth expression is checked at the beginning of each iteration, if it evaluates to &false; right from the beginning, the loop execution would end immediately). </simpara> <para> There is just one syntax for <literal>do..while</literal> loops: <informalexample> <programlisting role="php"> <![CDATA[ $i = 0; do { print $i; } while ($i>0); ]]> </programlisting> </informalexample> </para> <simpara> The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to &false; ($i is not bigger than 0) and the loop execution ends. </simpara> <para> Advanced C users may be familiar with a different usage of the <literal>do..while</literal> loop, to allow stopping execution in the middle of code blocks, by encapsulating them with <literal>do..while</literal>(0), and using the <link linkend="control-structures.break"><literal>break</literal></link> statement. The following code fragment demonstrates this: <informalexample> <programlisting role="php"> <![CDATA[ do { if ($i < 5) { print "i is not big enough"; break; } $i *= $factor; if ($i < $minimum_limit) { break; } print "i is ok"; ...process i... } while(0); ]]> </programlisting> </informalexample> </para> <simpara> Don't worry if you don't understand this right away or at all. You can code scripts and even powerful scripts without using this `feature'. </simpara> </sect1> <sect1 id="control-structures.for"> <title><literal>for</literal></title> <para> <literal>for</literal> loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a <literal>for</literal> loop is: <informalexample> <programlisting> <![CDATA[ for (expr1; expr2; expr3) statement ]]> </programlisting> </informalexample> </para> <simpara> The first expression (<replaceable>expr1</replaceable>) is evaluated (executed) once unconditionally at the beginning of the loop. </simpara> <simpara> In the beginning of each iteration, <replaceable>expr2</replaceable> is evaluated. If it evaluates to &true;, the loop continues and the nested statement(s) are executed. If it evaluates to &false;, the execution of the loop ends. </simpara> <simpara> At the end of each iteration, <replaceable>expr3</replaceable> is evaluated (executed). </simpara> <simpara> Each of the expressions can be empty. <replaceable>expr2</replaceable> being empty means the loop should be run indefinitely (PHP implicitly considers it as &true;, like C). This may not be as useless as you might think, since often you'd want to end the loop using a conditional <link linkend="control-structures.break"><literal>break</literal></link> statement instead of using the <literal>for</literal> truth expression. </simpara> <para> Consider the following examples. All of them display numbers from 1 to 10: <informalexample> <programlisting role="php"> <![CDATA[ /* example 1 */ for ($i = 1; $i <= 10; $i++) { print $i; } /* example 2 */ for ($i = 1;;$i++) { if ($i > 10) { break; } print $i; } /* example 3 */ $i = 1; for (;;) { if ($i > 10) { break; } print $i; $i++; } /* example 4 */ for ($i = 1; $i <= 10; print $i, $i++); ]]> </programlisting> </informalexample> </para> <simpara> Of course, the first example appears to be the nicest one (or perhaps the fourth), but you may find that being able to use empty expressions in <literal>for</literal> loops comes in handy in many occasions. </simpara> <para> PHP also supports the alternate "colon syntax" for <literal>for</literal> loops. <informalexample> <programlisting> <![CDATA[ for (expr1; expr2; expr3): statement; ...; endfor; ]]> </programlisting> </informalexample> </para> <para> Other languages have a <literal>foreach</literal> statement to traverse an array or hash. PHP 3 has no such construct; PHP 4 does (see <link linkend="control-structures.foreach">foreach</link>). In PHP 3, you can combine <link linkend="control-structures.while">while</link> with the <function>list</function> and <function>each</function> functions to achieve the same effect. See the documentation for these functions for an example. </para> </sect1> <sect1 id="control-structures.foreach"> <title><literal>foreach</literal></title> <para> PHP 4 (not PHP 3) includes a <literal>foreach</literal> construct, much like Perl and some other languages. This simply gives an easy way to iterate over arrays. There are two syntaxes; the second is a minor but useful extension of the first: <informalexample> <programlisting> <![CDATA[ foreach(array_expression as $value) statement foreach(array_expression as $key => $value) statement ]]> </programlisting> </informalexample> </para> <simpara> The first form loops over the array given by <literal>array_expression</literal>. On each loop, the value of the current element is assigned to <literal>$value</literal> and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element). </simpara> <simpara> The second form does the same thing, except that the current element's key will be assigned to the variable <literal>$key</literal> on each loop. </simpara> <para> <note> <para> When <literal>foreach</literal> first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call <function>reset</function> before a <literal>foreach</literal> loop. </para> </note> </para> <para> <note> <para> Also note that <literal>foreach</literal> operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the <function>each</function> construct and changes to the array element returned are not reflected in the original array. </para> </note> </para> <note> <para> <literal>foreach</literal> does not support the ability to suppress error messages using '@'. </para> </note> <para> You may have noticed that the following are functionally identical: <informalexample> <programlisting role="php"> <![CDATA[ reset ($arr); while (list(, $value) = each ($arr)) { echo "Value: $value<br>\n"; } foreach ($arr as $value) { echo "Value: $value<br>\n"; } ]]> </programlisting> </informalexample> The following are also functionally identical: <informalexample> <programlisting role="php"> <![CDATA[ reset ($arr); while (list($key, $value) = each ($arr)) { echo "Key: $key; Value: $value<br>\n"; } foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br>\n"; } ]]> </programlisting> </informalexample> </para> <para> Some more examples to demonstrate usages: <informalexample> <programlisting role="php"> <![CDATA[ /* foreach example 1: value only */ $a = array (1, 2, 3, 17); foreach ($a as $v) { print "Current value of \$a: $v.\n"; } /* foreach example 2: value (with key printed for illustration) */ $a = array (1, 2, 3, 17); $i = 0; /* for illustrative purposes only */ foreach($a as $v) { print "\$a[$i] => $v.\n"; $i++; } /* foreach example 3: key and value */ $a = array ( "one" => 1, "two" => 2, "three" => 3, "seventeen" => 17 ); foreach($a as $k => $v) { print "\$a[$k] => $v.\n"; } /* foreach example 4: multi-dimensional arrays */ $a[0][0] = "a"; $a[0][1] = "b"; $a[1][0] = "y"; $a[1][1] = "z"; foreach($a as $v1) { foreach ($v1 as $v2) { print "$v2\n"; } } /* foreach example 5: dynamic arrays */ foreach(array(1, 2, 3, 4, 5) as $v) { print "$v\n"; } ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.break"> <title><literal>break</literal></title> <simpara> <literal>break</literal> ends execution of the current <literal>for</literal>, <literal>foreach</literal> <literal>while</literal>, <literal>do..while</literal> or <literal>switch</literal> structure. </simpara> <simpara> <literal>break</literal> accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. </simpara> <para> <informalexample> <programlisting role="php"> <![CDATA[ $arr = array ('one', 'two', 'three', 'four', 'stop', 'five'); while (list (, $val) = each ($arr)) { if ($val == 'stop') { break; /* You could also write 'break 1;' here. */ } echo "$val<br>\n"; } /* Using the optional argument. */ $i = 0; while (++$i) { switch ($i) { case 5: echo "At 5<br>\n"; break 1; /* Exit only the switch. */ case 10: echo "At 10; quitting<br>\n"; break 2; /* Exit the switch and the while. */ default: break; } } ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.continue"> <title><literal>continue</literal></title> <simpara> <literal>continue</literal> is used within looping structures to skip the rest of the current loop iteration and continue execution at the beginning of the next iteration. </simpara> <simpara> <literal>continue</literal> accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. </simpara> <para> <informalexample> <programlisting role="php"> <![CDATA[ while (list ($key, $value) = each ($arr)) { if (!($key % 2)) { // skip odd members continue; } do_something_odd ($value); } $i = 0; while ($i++ < 5) { echo "Outer<br>\n"; while (1) { echo " Middle<br>\n"; while (1) { echo " Inner<br>\n"; continue 3; } echo "This never gets output.<br>\n"; } echo "Neither does this.<br>\n"; } ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.switch"> <title><literal>switch</literal></title> <simpara> The <literal>switch</literal> statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the <literal>switch</literal> statement is for. </simpara> <para> The following two examples are two different ways to write the same thing, one using a series of <literal>if</literal> statements, and the other using the <literal>switch</literal> statement: <informalexample> <programlisting role="php"> <![CDATA[ if ($i == 0) { print "i equals 0"; } if ($i == 1) { print "i equals 1"; } if ($i == 2) { print "i equals 2"; } switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; } ]]> </programlisting> </informalexample> </para> <para> It is important to understand how the <literal>switch</literal> statement is executed in order to avoid mistakes. The <literal>switch</literal> statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a <literal>case</literal> statement is found with a value that matches the value of the <literal>switch</literal> expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the <literal>switch</literal> block, or the first time it sees a <literal>break</literal> statement. If you don't write a <literal>break</literal> statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example: <informalexample> <programlisting role="php"> <![CDATA[ switch ($i) { case 0: print "i equals 0"; case 1: print "i equals 1"; case 2: print "i equals 2"; } ]]> </programlisting> </informalexample> </para> <simpara> Here, if $i equals to 0, PHP would execute all of the print statements! If $i equals to 1, PHP would execute the last two print statements, and only if $i equals to 2, you'd get the 'expected' behavior and only 'i equals 2' would be displayed. So, it's important not to forget <literal>break</literal> statements (even though you may want to avoid supplying them on purpose under certain circumstances). </simpara> <simpara> In a <literal>switch</literal> statement, the condition is evaluated only once and the result is compared to each <literal>case</literal> statement. In an <literal>elseif</literal> statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a <literal>switch</literal> may be faster. </simpara> <para> The statement list for a case can also be empty, which simply passes control into the statement list for the next case. <informalexample> <programlisting role="php"> <![CDATA[ switch ($i) { case 0: case 1: case 2: print "i is less than 3 but not negative"; break; case 3: print "i is 3"; } ]]> </programlisting> </informalexample> </para> <para> A special case is the default case. This case matches anything that wasn't matched by the other cases, and should be the last <literal>case</literal> statement. For example: <informalexample> <programlisting role="php"> <![CDATA[ switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; default: print "i is not equal to 0, 1 or 2"; } ]]> </programlisting> </informalexample> </para> <para> The <literal>case</literal> expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type. </para> <para> The alternative syntax for control structures is supported with switches. For more information, see <link linkend="control-structures.alternative-syntax">Alternative syntax for control structures</link> . <informalexample> <programlisting role="php"> <![CDATA[ switch ($i): case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; default: print "i is not equal to 0, 1 or 2"; endswitch; ]]> </programlisting> </informalexample> </para> </sect1> <sect1 id="control-structures.declare"> <title><literal>declare</literal></title> <para> The <literal>declare</literal> construct is used to set execution directives for a block of code. The syntax of <literal>declare</literal> is similar to the syntax of other flow control constructs: <informalexample> <programlisting> <![CDATA[ declare (directive) statement ]]> </programlisting> </informalexample> </para> <para> The <literal>directive</literal> section allows the behavior of the <literal>declare</literal> block to be set. Currently only one directive is recognized: the <literal>ticks</literal> directive. (See below for more information on the <link linkend="control-structures.declare.ticks">ticks</link> directive) </para> <para> The <literal>statement</literal> part of the <literal>declare</literal> block will be executed - how it is executed and what side-effects occur during execution may depend on the directive set in the <literal>directive</literal> block. </para> <sect2 id="control-structures.declare.ticks"> <title>Ticks</title> <para>A tick is an event that occurs for every <replaceable>N</replaceable> low-level statements executed by the parser within the <literal>declare</literal> block. The value for <replaceable>N</replaceable> is specified using <literal>ticks=<replaceable>N</replaceable></literal> within the <literal>declare</literal> blocks's <literal>directive</literal> section. </para> <para> The event(s) that occurs on each tick is specified using the <function>register_tick_function</function>. See the example below for more details. Note that more than one event can occur for each tick. </para> <para> <example> <title>Profile a section of PHP code</title> <programlisting role="php"> <![CDATA[ <pre> <?php // A function that records the time when it is called function profile ($dump = FALSE) { static $profile; // Return the times stored in profile, then erase it if ($dump) { $temp = $profile; unset ($profile); return ($temp); } $profile[] = microtime (); } // Set up a tick handler register_tick_function("profile"); // Initialize the function before the declare block profile (); // Run a block of code, throw a tick every 2nd statement declare (ticks=2) { for ($x = 1; $x < 50; ++$x) { echo similar_text (md5($x), md5($x*$x)), "<br>"; } } // Display the data stored in the profiler print_r (profile (TRUE)); ?> </pre> ]]> </programlisting> </example> The example profiles the PHP code within the 'declare' block, recording the time at which every second low-level statement in the block was executed. This information can then be used to find the slow areas within particular segments of code. This process can be performed using other methods: using ticks is more convenient and easier to implement. </para> <simpara> Ticks are well suited for debugging, implementing simple multitasking, backgrounded I/O and many other tasks. </simpara> <simpara> See also <function>register_tick_function</function> and <function>unregister_tick_function</function>. </simpara> </sect2> </sect1> <sect1 id="function.return"> <title>return</title> <simpara> If called from within a function, the <function>return</function> statement immediately ends execution of the current function, and returns its argument as the value of the function call. <function>return</function> will also end the execution of an <function>eval</function> statement or script file. </simpara> <simpara> If called from the global scope, then execution of the current script file is ended. If the current script file was <function>include</function>ed or <function>require</function>ed, then control is passed back to the calling file. Furthermore, if the current script file was <function>include</function>ed, then the value given to <function>return</function> will be returned as the value of the <function>include</function> call. If <function>return</function> is called from within the main script file, then script execution ends. If the current script file was named by the <link linkend="ini.auto-prepend-file">auto_prepend_file</link> or <link linkend="ini.auto-append-file">auto_append_file</link> configuration options in <link linkend="configuration.file">the configuration file</link>, then that script file's execution is ended. </simpara> <simpara>For more information, see <link linkend="functions.returning-values">Returning values</link>. </simpara> <note> <simpara> Note that since <function>return</function> is a language construct and not a function, the parentheses surrounding its arguments are <emphasis>not</emphasis> required--in fact, it is more common to leave them out than to use them, although it doesn't matter one way or the other. </simpara> </note> </sect1> <sect1 id="function.require"> <title><function>require</function></title> <simpara> The <function>require</function> statement includes and evaluates the specific file. </simpara> <simpara> <function>require</function> includes and evaluates a specific file. Detailed information on how this inclusion works is described in the documentation for <function>include</function>. </simpara> <simpara> <function>require</function> and <function>include</function> are identical in every way except how they handle failure. <function>include</function> produces a <link linkend="internal.e-warning">Warning</link> while <function>require</function> results in a <link linkend="internal.e-error"> Fatal Error</link>. In other words, don't hesitate to use <function>require</function> if you want a missing file to halt processing of the page. <function>include</function> does not behave this way, the script will continue regardless. Be sure to have an appropriate <link linkend="ini.include-path">include_path</link> setting as well. </simpara> <para> <example> <title>Basic <function>require</function> examples</title> <programlisting role="php"> <![CDATA[ <?php require 'prepend.php'; require $somefile; require ('somefile.txt'); ?> ]]> </programlisting> </example> </para> <simpara> See the <function>include</function> documentation for more examples. </simpara> <note> <simpara> Prior to PHP 4.0.2, the following applies: <function>require</function> will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect <function>require</function>. However, if the line on which the <function>require</function> occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of <function>require</function>. Although the code contained in the target file is still subject to the loop, the <function>require</function> itself happens only once. </simpara> </note> <simpara> See also <function>include</function>, <function>require_once</function>, <function>include_once</function>, <function>eval</function>, <function>file</function>, <function>readfile</function>, <function>virtual</function> and <link linkend="ini.include-path">include_path</link>. </simpara> </sect1> <sect1 id="function.include"> <title><function>include</function></title> <simpara> The <function>include</function> statement includes and evaluates the specified file. </simpara> <simpara> The documentation below also applies to <function>require</function>. The two constructs are identical in every way except how they handle failure. <function>include</function> produces a <link linkend="internal.e-warning">Warning</link> while <function>require</function> results in a <link linkend="internal.e-error">Fatal Error</link>. In other words, use <function>require</function> if you want a missing file to halt processing of the page. <function>include</function> does not behave this way, the script will continue regardless. Be sure to have an appropriate <link linkend="ini.include-path">include_path</link> setting as well. </simpara> <simpara> When a file is included, the code it contains inherits the <link linkend="language.variables.scope">variable scope</link> of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. </simpara> <para> <example> <title>Basic <function>include</function> example</title> <programlisting role="php"> <![CDATA[ vars.php <?php $color = 'green'; $fruit = 'apple'; ?> test.php <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?> ]]> </programlisting> </example> </para> <simpara> If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. </simpara> <para> <example> <title>Including within functions</title> <programlisting role="php"> <![CDATA[ <?php function foo() { global $color; include 'vars.php'; echo "A $color $fruit"; } /* vars.php is in the scope of foo() so * * $fruit is NOT available outside of this * * scope. $color is because we declared it * * as global. */ foo(); // A green apple echo "A $color $fruit"; // A green ?> ]]> </programlisting> </example> </para> <simpara> When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within <link linkend="language.basic-syntax.phpmode">valid PHP start and end tags</link>. </simpara> <simpara> If "<link linkend="ini.allow-url-fopen">URL fopen wrappers</link>" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using an URL (via HTTP) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using an URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script. </simpara> <para> <example> <title><function>include</function> through HTTP</title> <programlisting role="php"> <![CDATA[ <?php /* This example assumes that www.example.com is configured to parse .php * * files and not .txt files. Also, 'Works' here means that the variables * * $foo and $bar are available within the included file. */ // Won't work; file.txt wasn't handled by www.example.com as PHP include 'http://www.example.com/file.txt?foo=1&bar=2'; // Won't work; looks for a file named 'file.php?foo=1&bar=2' on the // local filesystem. include 'file.php?foo=1&bar=2'; // Works. include 'http://www.example.com/file.php?foo=1&bar=2'; $foo = 1; $bar = 2; include 'file.txt'; // Works. include 'file.php'; // Works. ?> ]]> </programlisting> </example> See also <link linkend="features.remote-files">Remote files</link>, <function>fopen</function> and <function>file</function> for related information. </para> <para> Because <function>include</function> and <function>require</function> are special language constructs, you must enclose them within a statement block if it's inside a conditional block. </para> <para> <example> <title>include() and conditional blocks</title> <programlisting role="php"> <![CDATA[ <?php // This is WRONG and will not work as desired. if ($condition) include $file; else include $other; // This is CORRECT. if ($condition) { include $file; } else { include $other; } ?> ]]> </programlisting> </example> </para> <simpara> Handling Returns: It is possible to execute a <function>return</function> statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function. </simpara> <note> <simpara> In PHP 3, the return may not appear inside a block unless it's a function block, in which case the <function>return</function> applies to that function and not the whole file. </simpara> </note> <para> <example> <title><function>include</function> and the <function>return</function> statement</title> <programlisting role="php"> <![CDATA[ return.php <?php $var = 'PHP'; return $var; ?> noreturn.php <?php $var = 'PHP'; ?> testreturns.php <?php $foo = include 'return.php'; echo $foo; // prints 'PHP' $bar = include 'noreturn.php'; echo $bar; // prints 1 ?> ]]> </programlisting> </example> </para> <simpara> <literal>$bar</literal> is the value <literal>1</literal> because the include was successful. Notice the difference between the above examples. The first uses <function>return</function> within the included file while the other does not. A few other ways to "include" files into variables are with <function>fopen</function>, <function>file</function> or by using <function>include</function> along with <link linkend="ref.outcontrol">Output Control Functions</link>. </simpara> <simpara> See also <function>require</function>, <function>require_once</function>, <function>include_once</function>, <function>readfile</function>, <function>virtual</function>, and <link linkend="ini.include-path">include_path</link>. </simpara> </sect1> <sect1 id="function.require-once"> <title><function>require_once</function></title> <para> The <function>require_once</function> statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the <function>require</function> statement, with the only difference being that if the code from a file has already been included, it will not be included again. See the documentation for <function>require</function> for more information on how this statement works. </para> <para> <function>require_once</function> should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc. </para> <para> For examples on using <function>require_once</function> and <function>include_once</function>, look at the <ulink url="&url.php.pear;">PEAR</ulink> code included in the latest PHP source code distributions. </para> <note> <para> <function>require_once</function> was added in PHP 4.0.1pl2 </para> </note> <para> See also: <function>require</function>, <function>include</function>, <function>include_once</function>, <function>get_required_files</function>, <function>get_included_files</function>, <function>readfile</function>, and <function>virtual</function>. </para> </sect1> <sect1 id="function.include-once"> <title><function>include_once</function></title> <para> The <function>include_once</function> statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the <function>include</function> statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once. </para> <para> <function>include_once</function> should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc. </para> <para> For more examples on using <function>require_once</function> and <function>include_once</function>, look at the <ulink url="&url.php.pear;">PEAR</ulink> code included in the latest PHP source code distributions. </para> <note> <para> <function>include_once</function> was added in PHP 4.0.1pl2 </para> </note> <para> See also <function>include</function>, <function>require</function>, <function>require_once</function>, <function>get_required_files</function>, <function>get_included_files</function>, <function>readfile</function>, and <function>virtual</function>. </para> </sect1> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: --> Index: phpdoc/cs/language/expressions.xml +++ phpdoc/cs/language/expressions.xml <?xml version="1.0" encoding="iso-8859-2"?> <!-- EN-Revision: 1.19 Maintainer: luk Status: ready --> <chapter id="language.expressions"> <title>Výrazy</title> <simpara> Výrazy jsou nejdùle¾itìj¹ími stavebními kameny PHP. V PHP je témìø v¹e, co napí¹ete, výraz. Nejjednodu¹¹í, a pøece nejpøesnìj¹í definicí výrazu je "v¹echno, co má hodnotu". </simpara> <simpara> Nejzákladnìj¹ími formami výrazù jsou konstanty a promìnné. Kdy¾ napí¹ete "$a = 5", pøiøazujete '5' do $a. '5' má, pochopitelnì, hodnotu 5, nebo jinými slovy, '5' je výraz s hodnotou 5 (v tomto pøípadì je '5' celoèíselnou konstantou). </simpara> <simpara> Po tomto pøiøazení budete oèekávat, ¾e hodnota $a bude 5, tak¾e kdybyste napsali $b = $a, oèekávali byste toté¾, jako pøi napsání $b = 5. Jinými slovy, $a je tedy výraz s hodnotou 5. Pokud v¹e pracuje správnì, pøesnì to se také stane. </simpara> <para> O nìco slo¾itìj¹ím pøíkladem výrazù jsou funkce. Uva¾ujme napø. tuto funkci: <informalexample> <programlisting role="php"> <![CDATA[ function foo () { return 5; } ]]> </programlisting> </informalexample></para> <simpara> Za pøedpokladu, ¾e jste dobøe seznámeni s konceptem funkcí (pokud ne, nahlédnìte do kapitoly o funkcích), byste pøedpokládali, ¾e napsání <literal>$c = foo()</literal> je v zásadì toté¾ jako <literal>$c = 5</literal>, a máte pravdu. Funkce jsou výrazy s hodnotou jejich návratové hodnoty. Funkce foo() vrací 5, tudí¾ hodnota výrazu 'foo()' je 5. Obvykle funkce nevracejí konstantní hodnotu, nýbr¾ nìco vypoèítávají. </simpara> <simpara> Hodnoty v PHP samozøejmì nemusejí být pouze celá èísla, a velmi èasto také nejsou. PHP podporuje tøi typy skalárních hodnot: celoèíselné, reálné (pohyblivá øádová èárka) a øetìzce (skalární hodnoty jsou hodnoty, které nejde "rozbít" na men¹í èásti, narozdíl napø. od polí). PHP podporuje také dva kompozitní (neskalární) typy: pole a objekty. Ka¾dý z tìchto typù hodnot mù¾e být pøiøazen do promìnné nebo vracen z funkce. </simpara> <simpara> U¾ivatelé PHP/FI 2 by nemìli pocítit zmìnu. Ale PHP jde ve výrazech mnohem dále, stejnì jako mnoho jiných programovacích jazykù. PHP je výrazovì orientovaný jazyk, ve smyslu, ¾e témìø v¹e je výraz. Uva¾ujme pøíklad, kterým jsme se ji¾ zabývali, '$a = 5'. Ihned vidíme, ¾e jsou zde zahrnuty dvì hodnoty, celoèíselná konstanta '5' a hodnota $a, která je aktualizována na 5. Ale je pravda, ¾e je tu je¹tì jedna hodnota, je to hodnota samotného pøiøazení. Pøiøazení jako takové ohodnocuje pøiøazovanou hodnotu, v tomto pøípadì 5. V praxi to znamená, ¾e '$a = 5', bez ohledu na to co dìlá, je výraz s hodnotou 5. Proto je '$b = ($a = 5)' toté¾ jako '$a = 5; $b = 5;' (støedník oznaèuje konec výrazu). Proto¾e pøiøezení jsou parsována zprava doleva, mù¾ete také napsat '$b = $a = 5'. </simpara> <simpara> Jiným dobrým pøíkladem orientace na výrazy je pre- a post-inkrementace a dekrementace. U¾ivatelé PHP/FI 2 a mnoha jiných jazykù znají notaci promìnná++ a promìnná--. To jsou inkrementaèní a dekrementaèní operátory. V PHP/FI 2 nemìlo '$a++' ¾ádnou hodnotu (není to výraz), a proto ne¹lo pøiøadit nebo jinak pou¾ít. PHP roz¹iøuje schopnosti pøemìnou tìchto konstrukcí ve výrazy, jako v C. V PHP, stejnì jako v C, existují dva typy inkrementace - pre-inkrementace a post-inkrementace. Oba ve své podstatì inkrementují promìnnou a efekt na tuto promìnnou je toto¾ný. Rozdíl je v hodnotì inkrementaèního výrazu. Pre-inkrementace, zapsaná jako '++$var', ohodnocuje výraz inkrementovanou hodnotou (PHP inkrementuje promìnnou døíve, ne¾ pøeète její hodnotu, proto "pre-inkrementace"). Post-inkrementace, zapsaná jako '$var++', ohodnocuje výraz pùvodní hodnotou promìnné $var, pøed inkrementací (PHP inkrementuje promìnnou po pøeètení její hodnoty, proto "post-inkrement"). </simpara> <simpara> Velmi èastým typem výrazù jsou výrazy porovnávací. Tyto výrazy se ohodnocují 0 a 1 ve významu &false;, resp. &true;. PHP podporuje > (vìt¹í ne¾), >= (vìt¹í nebo rovno), == (rovná se), != (nerovná se), < (men¹í ne¾) a <= (men¹í nebo rovno). Tyto výrazy se nejèastìji pou¾ívají v podmínkách, jako je konstrukt <literal>if</literal>. </simpara> <simpara> Posledním pøíkladem výrazù, kterým se budeme zabývat, je kombinací pøiøazení a operátorù. Ji¾ víte, ¾e kdy¾ chcete inkrementovat $a o jednièku, jednodu¹e napí¹ete '$a++' nebo '++$a'. Ale co kdy¾ chcete hodnotu zvý¹it o jiné èíslo, napø. o 3? Mohli byste napsat '$a++' víckrát za sebou, ale to samozøejmì není efektivní ani pohodlné. Mnohem praktiètìj¹í je napsat '$a = $a + 3'. Výraz '$a + 3' ohodnocuje hodnotu $a plus 3 a je pøiøazen zpìt do $a, co¾ dává $a inkrementované o 3. V PHP, stejnì jako v øadì jiných jazykù (jako je C), to mù¾ete napsat krat¹ím zpùsobem, který se èasem stane jasnìj¹í i rychlej¹í k pochopení. Pøiètení 3 k aktuální hodnotì $a lze zapsat jako '$a += 3'. Pøesnì to znamená "vezmi hodnotu $a, pøièti k ní 3 a pøiøaï zpìt do $a". Kromì krat¹ího a pøehlednìj¹ího zápisu je výhodou také rychlej¹í provedení. Hodnota '$a += 3', jako hodnota regulérního pøiøazení, je pøiøazovaná hodnota. Uvìdomte si, ¾e to NENÍ 3, nýbr¾ $a plus 3 (co¾ je hodnota výrazu pøiøazovaného do $a). Takto lze pou¾ít jakýkoli binární operátor, napøíklad '$a -= 5' (odeèti 5 od hodnoty $a), '$b *= 7' (vynásob hodnotu $b èíslem 7) apod. </simpara> <para> Je tu je¹tì jeden výraz, který se mù¾e zdát zvlá¹tní, pokud jste ho je¹tì nevidìli v jiných jazycích: ternární podmínìný operátor: <informalexample><programlisting> <![CDATA[ $prvni ? $druhy : $treti ]]> </programlisting></informalexample> Pokud hodnota prvního podvýrazu je &true; (nenulová), je ohodnocen druhý podvýraz a je výsledkem celého podmínìného výrazu. Jinak je ohodnocen tøetí podvýraz a je pak hodotou celého výrazu. </para> <para> Následující pøíklad by mìl pomoci lépe pochopit pre- a post-inkrementaci i výrazy obecnì: <informalexample> <programlisting role="php"> <![CDATA[ function double($i) { return $i*2; } $b = $a = 5; /* pøiøaï hodnotu pìt do promìnných $a a $b */ $c = $a++; /* proveï post-inkrement, pøiøaï pùvodní hodnotu $a (5) do $c */ $e = $d = ++$b; /* proveï pre-inkrement, pøiøaï inkrementovanou hodnotu $b (6) do $d a $e */ /* nyní jsou hodnoty promìnných $d a $e rovny 6 */ $f = double($d++); /* pøiøaï dvakrát hodnotu $d <emphasis>pøed</emphasis> inkrementací, 2*6 = 12, do $f */ $g = double(++$e); /* pøiøaï dvakrát hodnotu $e <emphasis>po</emphasis> inkrementaci, 2*7 = 14 do $g */ $h = $g += 10; /* nejdøíve je $g inkrementováno o 10 má pak hodnotu 24. Hodnota pøiøazení (24) se pøiøadí do $h a $h tím získává také hodnotu 24. */ ]]> </programlisting> </informalexample> </para> <simpara> Na zaèátku kapitoly bylo øeèeno, ¾e si popí¹eme rùzné typy konstruktù, a jak bylo slíbeno, výrazy mohou být konstrukty. V tomto pøípadì mají konstrukty formát 'expr' ';', co¾ znamená "výraz následovaný støedníkem. V konstruktu '$b=$a=5;', je $a=5 platný výraz, ale samo o sobì to není konstrukt.'$b=$a=5;' je i platný konstrukt. </simpara> <simpara> <emphasis> Pozn. pøekladatele: Pøedchozím odstavci (obèas i jinde) pou¾ívám termín "konstrukt" pro anglické slovo "statement". Tento pøeklad není pøíli¹ korektní, ale v èeské programátorské mluvì neexistuje vhodný termín. Kdyby nìkdo vìdìl o lep¹ím, napi¹te mi, prosím, na luk@php.net. </emphasis> </simpara> <simpara> Poslední vìcí, která si zaslou¾í zmínku, je pravdivostní hodnota výrazù. V mnoha pøípadech, hlavnì podmínkách a cyklech, vás nezajímá konkrétní hodnota výrazu, nýbr¾ pouze to, jestli je &true; nebo &false;. <!-- (PHP doesn't have a dedicated boolean type) : WRONG, php does. --> Konstanty &true; a &false; (malá/velká písmena nehrají roli) pøedstavují dvì mo¾né boolovské (pravdivostní) hodnoty. V pøípadì potøeby je výraz automaticky pøeveden na typ boolean. Detailnìj¹í informace o zpùsobu konverze - viz <link linkend="language.types.typecasting">sekce o typové konverzi</link>. </simpara> <simpara> PHP poskytuje plnou a silnou implementaci výrazù a úplnì je zdokumentovat pøesahuje rozsah tohoto manuálu. Vý¹e uvedené pøíklady by vám mìli naznaèit, co jsou vùbec výrazy a jak konstruovat u¾iteèné výrazy. Ve zbývající èásti manuálu budeme psát <replaceable>expr</replaceable> jako¾to jakýkoli platný PHP výraz. </simpara> </chapter> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: -->