CVS update: php3/doc/chapters

From: Date: Tue, 25 May 1999 04:15:41 +0000
Subject: CVS update: php3/doc/chapters
Groups: php.dev 
Request: Send a blank email to php-dev+get-5966@lists.php.net to get a copy of this message
Date: Tuesday May 25, 1999 @ 0:15 Author: jim Update of /repository/php3/doc/chapters In directory php:/tmp/cvs-serv30936/chapters Modified Files: lang-const.sgml lang-syntax.sgml Log Message: More shuffling and some new stuff. Still need to consolidate more array and object info under types.array and types.object. Index: php3/doc/chapters/lang-const.sgml diff -u php3/doc/chapters/lang-const.sgml:1.40 php3/doc/chapters/lang-const.sgml:1.41 --- php3/doc/chapters/lang-const.sgml:1.40 Mon May 24 21:32:32 1999 +++ php3/doc/chapters/lang-const.sgml Tue May 25 00:15:40 1999 @@ -169,197 +169,6 @@ </example> </sect1> - <sect1 id="expressions"> - <title>Expressions</title> - - <simpara> - Expressions are the most important building stones of PHP. In PHP, - almost anything you write is an expression. The simplest yet - most accurate way to define an expressions is "anything that has a - value". - - <simpara> - Simple examples that come in mind are constants and variables. - When you type "$a = 5", you're assigning '5' into $a. '5', - obviously, has the value 5, or in other words '5' is an expression - with the value of 5 (in this case, '5' is an integer constant). - - <simpara> - After this assignment, you'd expect $a's value to be 5 as - well, so if you wrote $b = $a, you'd expect it to behave just as - if you wrote $b = 5. In other words, $a is an expression with the - value of 5 as well. If everything works right, this is exactly - what will happen. - - <para> - Slightly more complex examples for expressions are functions. For - instance, consider the following function: - - <informalexample> - <programlisting> -function foo () { - return 5; -} - </programlisting> - </informalexample> - - <simpara> - Assuming you're familiar with the concept of functions (if you're - not, take a look at the chapter about functions), you'd assume - that typing <literal>$c = foo()</literal> is essentially just like - writing <literal>$c = 5</literal>, and you're right. Functions - are expressions with the value of their return value. Since foo() - returns 5, the value of the expression 'foo()' is 5. Usually - functions don't just return a static value but compute something. - - <simpara> - Of course, values in PHP don't have to be integers, and very often - they aren't. PHP supports three scalar value types: integer values, - floating point values and string values (scalar values are values that - you can't 'break' into smaller pieces, unlike arrays, for instance). - PHP also supports two composite (non-scalar) types: arrays and - objects. Each of these value types can be assigned into variables or - returned from functions. - - <simpara> - So far, users of PHP/FI 2 shouldn't feel any change. However, PHP - takes expressions much further, in the same way many other - languages do. PHP is an expression-oriented language, in the - sense that almost everything is an expression. Consider the - example we've already dealt with, '$a = 5'. It's easy to see that - there are two values involved here, the value of the integer - constant '5', and the value of $a which is being updated to 5 as - well. But the truth is that there's one additional value involved - here, and that's the value of the assignment itself. The - assignment itself evaluates to the assigned value, in this case 5. - In practice, it means that '$a = 5', regardless of what it does, - is an expression with the value 5. Thus, writing something like - '$b = ($a = 5)' is like writing '$a = 5; $b = 5;' (a semicolon - marks the end of a statement). Since assignments are parsed in a - right to left order, you can also write '$b = $a = 5'. - - <simpara> - Another good example of expression orientation is pre- and - post-increment and decrement. Users of PHP/FI 2 and many other - languages may be familiar with the notation of variable++ and - variable--. These are increment and decrement operators. In - PHP/FI 2, the statement '$a++' has no value (is not an - expression), and thus you can't assign it or use it in any way. - PHP enhances the increment/decrement capabilities by making - these expressions as well, like in C. In PHP, like in C, there - are two types of increment - pre-increment and post-increment. - Both pre-increment and post-increment essentially increment the - variable, and the effect on the variable is idential. The - difference is with the value of the increment expression. - Pre-increment, which is written '++$variable', evaluates to the - incremented value (PHP increments the variable before reading its - value, thus the name 'pre-increment'). Post-increment, which is - written '$variable++' evaluates to the original value of - $variable, before it was incremented (PHP increments the variable - after reading its value, thus the name 'post-increment'). - - <simpara> - A very common type of expressions are comparison expressions. - These expressions evaluate to either 0 or 1, meaning FALSE or TRUE - (respectively). PHP supports > (bigger than), >= (bigger than or - equal to), == (equal), < (smaller than) and <= (smaller than or - equal to). These expressions are most commonly used inside - conditional execution, such as IF statements. - - <simpara> - The last example of expressions we'll deal with here is combined - operator-assignment expressions. You already know that if you - want to increment $a by 1, you can simply write '$a++' or '++$a'. - But what if you want to add more than one to it, for instance 3? - You could write '$a++' multiple times, but this is obviously not a - very efficient or comfortable way. A much more common practice is - to write '$a = $a + 3'. '$a + 3' evaluates to the value of $a - plus 3, and is assigned back into $a, which results in - incrementing $a by 3. In PHP, as in several other languages - like C, you can write this in a shorter way, which with time would - become clearer and quicker to understand as well. Adding 3 to the - current value of $a can be written '$a += 3'. This means exactly - "take the value of $a, add 3 to it, and assign it back into $a". - In addition to being shorter and clearer, this also results in - faster execution. The value of '$a += 3', like the value of a - regular assignment, is the assigned value. Notice that it is NOT - 3, but the combined value of $a plus 3 (this is the value that's - assigned into $a). Any two-place operator can be used in this - operator-assignment mode, for example '$a -= 5' (subtract 5 from - the value of $a), '$b *= 7' (multiply the value of $b by 7), etc. - - <para> - There is one more expression that may seem odd if you haven't seen - it in other languages, the ternary conditional operator: - - <informalexample><programlisting> -$first ? $second : $third -</programlisting></informalexample> - - If the value of the first subexpression is true (non-zero), then - it the second subexpression is evaluated, and that is the result - of the conditional expression. Otherwise, the third subexpression - is evaluated, and that is the value. - - <para> - The following example should help you understand pre- and - post-increment and expressions in general a bit better: - - <informalexample> - <programlisting> -function double($i) { - return $i*2; -} -$b = $a = 5; /* assign the value five into the variable $a and $b */ -$c = $a++; /* post-increment, assign original value of $a - (5) to $c */ -$e = $d = ++$b; /* pre-increment, assign the incremented value of - $b (6) to $d and $e */ - -/* at this point, both $d and $e are equal to 6 */ - -$f = double($d++); /* assign twice the value of $d <emphasis>before</emphasis> - the increment, 2*6 = 12 to $f */ -$g = double(++$e); /* assign twice the value of $e <emphasis>after</emphasis> - the increment, 2*7 = 14 to $g */ -$h = $g += 10; /* first, $g is incremented by 10 and ends with the - value of 24. the value of the assignment (24) is - then assigned into $h, and $h ends with the value - of 24 as well. */ -</programlisting></informalexample> - - <simpara> - In the beginning of the chapter we said that we'll be describing - the various statement types, and as promised, expressions can be - statements. However, not every expression is a statement. In - this case, a statement has the form of 'expr' ';' that is, an - expression followed by a semicolon. In '$b=$a=5;', $a=5 is a - valid expression, but it's not a statement by itself. '$b=$a=5;' - however is a valid statement. - - <simpara> - One last thing worth mentioning is the truth value of expressions. - In many events, mainly in conditional execution and loops, you're - not interested in the specific value of the expression, but only - care about whether it means TRUE or FALSE (PHP doesn't have a - dedicated boolean type). The truth value of expressions in PHP is - calculated in a similar way to perl. Any numeric non-zero numeric - value is TRUE, zero is FALSE. Be sure to note that negative - values are non-zero and are thus considered TRUE! The empty - string and the string "0" are FALSE; all other strings are TRUE. - With non-scalar values (arrays and objects) - if the value - contains no elements it's considered FALSE, otherwise it's - considered TRUE. - - <simpara> - PHP provides a full and powerful implementation of expressions, and - documenting it entirely goes beyond the scope of this manual. The - above examples should give you a good idea about what expressions - are and how you can construct useful expressions. Throughout the - rest of this manual we'll write 'expr' to indicate any valid PHP - expression. - - <sect1 id="construct.if"> <title>IF</title> @@ -1513,7 +1322,7 @@ <entry>True if $a is not true.</entry> </row> <row> - <entry>$a && $b</entry> + <entry>$a &amp;&amp; $b</entry> <entry>And</entry> <entry>True of both $a and $b are true.</entry> </row> Index: php3/doc/chapters/lang-syntax.sgml diff -u php3/doc/chapters/lang-syntax.sgml:1.39 php3/doc/chapters/lang-syntax.sgml:1.40 --- php3/doc/chapters/lang-syntax.sgml:1.39 Mon May 24 21:13:05 1999 +++ php3/doc/chapters/lang-syntax.sgml Tue May 25 00:15:40 1999 @@ -12,6 +12,7 @@ There are four ways of escaping from HTML and entering "PHP code mode": + <para> <example> <title>Ways of escaping from HTML</title> <programlisting> @@ -25,9 +26,13 @@ &lt;/script> 4. &lt;% echo ("As of PHP 3.0.4 you may optionally use ASP-style tags"); %> + &lt;%= $variable; # This is a shortcut for "&lt;%echo .." %> </programlisting> </example> + <para> + The closing "bracket" for the block will include the immediately + trailing newline if one is present. <sect1 id="syntax-instrsep"> <title>Instruction separation</title> @@ -88,20 +93,18 @@ ?> </programlisting></informalexample> - <sect1 id="variable-types"> - <title>Variable types</title> + <sect1 id="types"> + <title>Types</title> <para> PHP supports the following types: <itemizedlist> - <listitem><simpara>integer - <listitem><simpara>double - <listitem><simpara>string - <listitem><simpara>array - <listitem><simpara>object - <listitem><simpara>pdfdoc (only if enabled PDF support) - <listitem><simpara>pdfinfo (only if enabled PDF support) + <listitem><simpara><link linkend="types.integer">integer</link> + <listitem><simpara><link linkend="types.double">floating-point numbers</link> + <listitem><simpara><link linkend="types.string">string</link> + <listitem><simpara><link linkend="types.array">array</link> + <listitem><simpara><link linkend="types.object">object</link> </itemizedlist> <simpara> @@ -121,53 +124,124 @@ information, see the section on <link linkend="variable-typejuggling">Type Juggling</link>. - </sect1> + <sect2 id="types.integer"> + <title>Integers</title> + <para> + Integers can be specified using any of the following syntaxes: + <informalexample><programlisting> +$a = 1234; # decimal number +$a = -123; # a negative number +$a = 0123; # octal number (equivalent to 83 decimal) +$a = 0x12; # hexadecimal number (equivalent to 18 decimal) +</programlisting></informalexample> - <sect1 id="variable-init"> - <title>Variable initialization</title> + <sect2 id="types.double"> + <title>Floating point numbers</title> + <para> + Floating point numbers ("doubles") can be specified using any + of the following syntaxes: + <informalexample><programlisting> +$a = 1.234; +$a = 1.2e3; +</programlisting></informalexample> - <simpara> - To initialize a variable in PHP, you simply assign a value to - it. For most types, this is straightforward; arrays and objects, - however, can use slightly different mechanisms. + <sect2 id="types.string"> + <title>Strings</title> + <para> + Strings can be specified using one of two sets of delimiters. + <para> + If the string is enclosed in double-quotes ("), variables + within the string will be expanded (subject to some parsing + limitations). As in C and Perl, the backslash ("\") character + can be used in specifying special characters: + <table> + <title>Escaped characters</title> + <tgroup cols="2"> + <thead> + <row> + <entry>sequence</entry> + <entry>meaning</entry> + </row> + </thead> + <tbody> + <row> + <entry><literal>\n</literal></entry> + <entry>newline</entry> + </row> + <row> + <entry><literal>\r</literal></entry> + <entry>carriage</entry> + </row> + <row> + <entry><literal>\t</literal></entry> + <entry>horizontal tab</entry> + </row> + <row> + <entry><literal>\\</literal></entry> + <entry>backslash</entry> + </row> + <row> + <entry><literal>\$</literal></entry> + <entry>dollar sign</entry> + </row> + <row> + <entry><literal>\"</literal></entry> + <entry>double-quote</entry> + </row> + </tbody> + </tgroup> + </table> + <para> + You can escape any other character, but a warning will be issued + at the highest warning level. + <para> + The second way to delimit a string uses the single-quote ("'") + character, which does not do any variable expansion or backslash + processing (except for "\\" and "\'" so you can insert backslashes + and single-quotes in a singly-quoted string). - <sect2 id="variable-init.array"> - <title>Initializing Arrays</title> + <sect2 id="types.array"> + <title>Arrays</title> - <simpara> - An array may be initialized in one of two ways: by the sequential - assigning of values, and by using the <function>array</function> - construct (which is documented in the <link - linkend="ref.array">Array functions</link> section). + <sect3 id="types.array.init"> + <title>Array Initialization</title> + + <simpara> + An array may be initialized in one of two ways: by the sequential + assigning of values, and by using the <function>array</function> + construct (which is documented in the <link + linkend="ref.array">Array functions</link> section). - <para> - To sequentially add values to an array, you simply assign to the - array variable using an empty subscript. The value will be added - as the last element of the array. + <para> + To sequentially add values to an array, you simply assign to the + array variable using an empty subscript. The value will be added + as the last element of the array. - <informalexample> - <programlisting> + <informalexample> + <programlisting> $names[] = "Jill"; // $names[0] = "Jill" $names[] = "Jack"; // $names[1] = "Jack" - </programlisting> - </informalexample> + </programlisting> + </informalexample> - <para> - As in C and Perl, array elements are numbered starting with 0, - not 1. + <para> + As in C and Perl, array elements are numbered starting with 0, + not 1. </sect2> + <sect2 id="types.object"> + <title>Objects</title> - <sect2 id="variable-init.object"> - <title>Initializing objects</title> + <sect3 id="types.object.init"> + <title>Object Initialization</title> - <para> - To initialize an object, you use the new statement to instantiate - the object to a variable. + <para> + To initialize an object, you use the new statement to instantiate + the object to a variable. - <informalexample> - <programlisting> + <informalexample> + <programlisting> class foo { function do_foo () { echo "Doing foo."; @@ -176,16 +250,201 @@ $bar = new foo; $bar -> do_foo (); - </programlisting> - </informalexample> + </programlisting> + </informalexample> - </sect2> + <sect1 id="expressions"> + <title>Expressions</title> + + <simpara> + Expressions are the most important building stones of PHP. In PHP, + almost anything you write is an expression. The simplest yet + most accurate way to define an expressions is "anything that has a + value". + + <simpara> + Simple examples that come in mind are constants and variables. + When you type "$a = 5", you're assigning '5' into $a. '5', + obviously, has the value 5, or in other words '5' is an expression + with the value of 5 (in this case, '5' is an integer constant). + + <simpara> + After this assignment, you'd expect $a's value to be 5 as + well, so if you wrote $b = $a, you'd expect it to behave just as + if you wrote $b = 5. In other words, $a is an expression with the + value of 5 as well. If everything works right, this is exactly + what will happen. + + <para> + Slightly more complex examples for expressions are functions. For + instance, consider the following function: - </sect1> + <informalexample> + <programlisting> +function foo () { + return 5; +} + </programlisting> + </informalexample> + <simpara> + Assuming you're familiar with the concept of functions (if you're + not, take a look at the chapter about functions), you'd assume + that typing <literal>$c = foo()</literal> is essentially just like + writing <literal>$c = 5</literal>, and you're right. Functions + are expressions with the value of their return value. Since foo() + returns 5, the value of the expression 'foo()' is 5. Usually + functions don't just return a static value but compute something. + + <simpara> + Of course, values in PHP don't have to be integers, and very often + they aren't. PHP supports three scalar value types: integer values, + floating point values and string values (scalar values are values that + you can't 'break' into smaller pieces, unlike arrays, for instance). + PHP also supports two composite (non-scalar) types: arrays and + objects. Each of these value types can be assigned into variables or + returned from functions. + + <simpara> + So far, users of PHP/FI 2 shouldn't feel any change. However, PHP + takes expressions much further, in the same way many other + languages do. PHP is an expression-oriented language, in the + sense that almost everything is an expression. Consider the + example we've already dealt with, '$a = 5'. It's easy to see that + there are two values involved here, the value of the integer + constant '5', and the value of $a which is being updated to 5 as + well. But the truth is that there's one additional value involved + here, and that's the value of the assignment itself. The + assignment itself evaluates to the assigned value, in this case 5. + In practice, it means that '$a = 5', regardless of what it does, + is an expression with the value 5. Thus, writing something like + '$b = ($a = 5)' is like writing '$a = 5; $b = 5;' (a semicolon + marks the end of a statement). Since assignments are parsed in a + right to left order, you can also write '$b = $a = 5'. + + <simpara> + Another good example of expression orientation is pre- and + post-increment and decrement. Users of PHP/FI 2 and many other + languages may be familiar with the notation of variable++ and + variable--. These are increment and decrement operators. In + PHP/FI 2, the statement '$a++' has no value (is not an + expression), and thus you can't assign it or use it in any way. + PHP enhances the increment/decrement capabilities by making + these expressions as well, like in C. In PHP, like in C, there + are two types of increment - pre-increment and post-increment. + Both pre-increment and post-increment essentially increment the + variable, and the effect on the variable is idential. The + difference is with the value of the increment expression. + Pre-increment, which is written '++$variable', evaluates to the + incremented value (PHP increments the variable before reading its + value, thus the name 'pre-increment'). Post-increment, which is + written '$variable++' evaluates to the original value of + $variable, before it was incremented (PHP increments the variable + after reading its value, thus the name 'post-increment'). + + <simpara> + A very common type of expressions are comparison expressions. + These expressions evaluate to either 0 or 1, meaning FALSE or TRUE + (respectively). PHP supports > (bigger than), >= (bigger than or + equal to), == (equal), < (smaller than) and <= (smaller than or + equal to). These expressions are most commonly used inside + conditional execution, such as IF statements. + + <simpara> + The last example of expressions we'll deal with here is combined + operator-assignment expressions. You already know that if you + want to increment $a by 1, you can simply write '$a++' or '++$a'. + But what if you want to add more than one to it, for instance 3? + You could write '$a++' multiple times, but this is obviously not a + very efficient or comfortable way. A much more common practice is + to write '$a = $a + 3'. '$a + 3' evaluates to the value of $a + plus 3, and is assigned back into $a, which results in + incrementing $a by 3. In PHP, as in several other languages + like C, you can write this in a shorter way, which with time would + become clearer and quicker to understand as well. Adding 3 to the + current value of $a can be written '$a += 3'. This means exactly + "take the value of $a, add 3 to it, and assign it back into $a". + In addition to being shorter and clearer, this also results in + faster execution. The value of '$a += 3', like the value of a + regular assignment, is the assigned value. Notice that it is NOT + 3, but the combined value of $a plus 3 (this is the value that's + assigned into $a). Any two-place operator can be used in this + operator-assignment mode, for example '$a -= 5' (subtract 5 from + the value of $a), '$b *= 7' (multiply the value of $b by 7), etc. + <para> + There is one more expression that may seem odd if you haven't seen + it in other languages, the ternary conditional operator: + + <informalexample><programlisting> +$first ? $second : $third +</programlisting></informalexample> + + If the value of the first subexpression is true (non-zero), then + it the second subexpression is evaluated, and that is the result + of the conditional expression. Otherwise, the third subexpression + is evaluated, and that is the value. + + <para> + The following example should help you understand pre- and + post-increment and expressions in general a bit better: + + <informalexample> + <programlisting> +function double($i) { + return $i*2; +} +$b = $a = 5; /* assign the value five into the variable $a and $b */ +$c = $a++; /* post-increment, assign original value of $a + (5) to $c */ +$e = $d = ++$b; /* pre-increment, assign the incremented value of + $b (6) to $d and $e */ + +/* at this point, both $d and $e are equal to 6 */ + +$f = double($d++); /* assign twice the value of $d <emphasis>before</emphasis> + the increment, 2*6 = 12 to $f */ +$g = double(++$e); /* assign twice the value of $e <emphasis>after</emphasis> + the increment, 2*7 = 14 to $g */ +$h = $g += 10; /* first, $g is incremented by 10 and ends with the + value of 24. the value of the assignment (24) is + then assigned into $h, and $h ends with the value + of 24 as well. */ +</programlisting></informalexample> + + <simpara> + In the beginning of the chapter we said that we'll be describing + the various statement types, and as promised, expressions can be + statements. However, not every expression is a statement. In + this case, a statement has the form of 'expr' ';' that is, an + expression followed by a semicolon. In '$b=$a=5;', $a=5 is a + valid expression, but it's not a statement by itself. '$b=$a=5;' + however is a valid statement. + + <simpara> + One last thing worth mentioning is the truth value of expressions. + In many events, mainly in conditional execution and loops, you're + not interested in the specific value of the expression, but only + care about whether it means TRUE or FALSE (PHP doesn't have a + dedicated boolean type). The truth value of expressions in PHP is + calculated in a similar way to perl. Any numeric non-zero numeric + value is TRUE, zero is FALSE. Be sure to note that negative + values are non-zero and are thus considered TRUE! The empty + string and the string "0" are FALSE; all other strings are TRUE. + With non-scalar values (arrays and objects) - if the value + contains no elements it's considered FALSE, otherwise it's + considered TRUE. + + <simpara> + PHP provides a full and powerful implementation of expressions, and + documenting it entirely goes beyond the scope of this manual. The + above examples should give you a good idea about what expressions + are and how you can construct useful expressions. Throughout the + rest of this manual we'll write 'expr' to indicate any valid PHP + expression. + <sect1 id="variable-scope"> - <title>Variable Scope</title> + <title>Variable scope</title> <simpara> The scope of a variable is the context within which it is defined. @@ -207,7 +466,6 @@ </informalexample> <simpara> - This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a @@ -326,7 +584,6 @@ </programlisting> </informalexample> - <sect1 id="variable-variable"> <title>Variable variables</title> @@ -535,7 +792,6 @@ <sect2> <title>Server configuration directives</title> <simpara></simpara> - <sect1 id="variable-typejuggling"> <title>Type juggling</title> -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#5966) next »