CVS update: php3/doc/chapters

From: Date: Sat, 26 Dec 1998 11:20:20 +0000
Subject: CVS update: php3/doc/chapters
Groups: php.dev 
Request: Send a blank email to php-dev+get-3046@lists.php.net to get a copy of this message
Date: Saturday December 26, 1998 @ 6:20 Author: eschmid Update of /repository/php3/doc/chapters In directory asf:/u/temp/cvs-serv27798 Modified Files: lang-syntax.sgml Log Message: More cleanups. Index: php3/doc/chapters/lang-syntax.sgml diff -c php3/doc/chapters/lang-syntax.sgml:1.28 php3/doc/chapters/lang-syntax.sgml:1.29 *** php3/doc/chapters/lang-syntax.sgml:1.28 Fri Dec 25 13:57:05 1998 --- php3/doc/chapters/lang-syntax.sgml Sat Dec 26 06:20:19 1998 *************** *** 107,112 **** --- 107,113 ---- 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> $names[] = "Jill"; // $names[0] = "Jill" *************** *** 114,145 **** </programlisting> </informalexample> ! <para> ! As in C and Perl, array elements are numbered starting with 0, not 1. </sect2> <sect2 id="variable-init.object"> <title>Initializing objects</title> <para> To initialize an object, you use the new statement to instantiate the object to a variable. <informalexample> <programlisting> class foo { ! function do_foo() { ! echo "Doing foo."; ! } } $bar = new foo; ! $bar-&gt;do_foo(); </programlisting> </informalexample> </sect2> </sect1> <sect1 id="variable-scope"> <title>Variable Scope</title> --- 115,152 ---- </programlisting> </informalexample> ! <para> ! As in C and Perl, array elements are numbered starting with 0, ! not 1. </sect2> + <sect2 id="variable-init.object"> <title>Initializing objects</title> <para> To initialize an object, you use the new statement to instantiate the object to a variable. + <informalexample> <programlisting> class foo { ! function do_foo () { ! echo "Doing foo."; ! } } + $bar = new foo; ! $bar -> do_foo (); </programlisting> </informalexample> + </sect2> </sect1> + <sect1 id="variable-scope"> <title>Variable Scope</title> *************** *** 152,165 **** <informalexample> <programlisting> ! $a=1; /* global scope */ ! Function Test() { echo $a; /* reference to local scope variable */ } ! Test(); ! </programlisting></informalexample> <simpara> --- 159,173 ---- <informalexample> <programlisting> ! $a = 1; /* global scope */ ! Function Test () { echo $a; /* reference to local scope variable */ } ! Test (); ! </programlisting> ! </informalexample> <simpara> *************** *** 173,191 **** global variables must be declared global inside a function if they are going to be used in that function. An example: ! <informalexample><programlisting> ! $a=1; ! $b=2; ! Function Sum() { ! global $a,$b; $b = $a + $b; } ! Sum(); echo $b; ! </programlisting></informalexample> <simpara> The above script will output &quot;3&quot;. By declaring $a and --- 181,201 ---- global variables must be declared global inside a function if they are going to be used in that function. An example: ! <informalexample> ! <programlisting> ! $a = 1; ! $b = 2; ! Function Sum () { ! global $a, $b; $b = $a + $b; } ! Sum (); echo $b; ! </programlisting> ! </informalexample> <simpara> The above script will output &quot;3&quot;. By declaring $a and *************** *** 198,214 **** the special PHP-defined $GLOBALS array. The previous example can be rewritten as: ! <informalexample><programlisting> ! $a=1; ! $b=2; ! Function Sum() { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } ! Sum(); echo $b; ! </programlisting></informalexample> <simpara> The $GLOBALS array is an associative array with the name of the --- 208,226 ---- the special PHP-defined $GLOBALS array. The previous example can be rewritten as: ! <informalexample> ! <programlisting> ! $a = 1; ! $b = 2; ! Function Sum () { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; } ! Sum (); echo $b; ! </programlisting> ! </informalexample> <simpara> The $GLOBALS array is an associative array with the name of the *************** *** 222,234 **** when program execution leaves this scope. Consider the following example: ! <informalexample><programlisting> ! Function Test() { ! $a=0; echo $a; $a++; } ! </programlisting></informalexample> <simpara> This function is quite useless since every time it is called it --- 234,248 ---- when program execution leaves this scope. Consider the following example: ! <informalexample> ! <programlisting> ! Function Test () { ! $a = 0; echo $a; $a++; } ! </programlisting> ! </informalexample> <simpara> This function is quite useless since every time it is called it *************** *** 238,250 **** which will not lose track of the current count, the $a variable is declared static: ! <informalexample><programlisting> ! Function Test() { ! static $a=0; echo $a; $a++; } ! </programlisting></informalexample> <simpara> Now, every time the Test() function is called it will print the --- 252,266 ---- which will not lose track of the current count, the $a variable is declared static: ! <informalexample> ! <programlisting> ! Function Test () { ! static $a = 0; echo $a; $a++; } ! </programlisting> ! </informalexample> <simpara> Now, every time the Test() function is called it will print the *************** *** 258,311 **** have an adequate way of terminating the recursion. The following simple function recursively counts to 10: ! <informalexample><programlisting> ! Function Test() { ! static $count=0; $count++; echo $count; ! if($count < 10) { ! Test(); } $count--; } ! </programlisting></informalexample> <sect1 id="variable-variable"> <title>Variable variables</title> <simpara> ! Sometimes it is convenient to be able to have variable variable names. ! That is, a variable name which can be set and used dynamically. ! A normal variable is set with a statement such as: ! <informalexample><programlisting> $a = "hello"; ! </programlisting></informalexample> <simpara> A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, <emphasis>hello</emphasis>, can be used as the name of a variable by using two dollar signs. ie. ! <informalexample><programlisting> $$a = "world"; ! </programlisting></informalexample> <simpara> At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement: ! <informalexample><programlisting> echo "$a ${$a}"; ! </programlisting></informalexample> <simpara> produces the exact same output as: ! <informalexample><programlisting> echo "$a $hello"; ! </programlisting></informalexample> <simpara> ie. they both produce: <emphasis>hello world</emphasis>. --- 274,339 ---- have an adequate way of terminating the recursion. The following simple function recursively counts to 10: ! <informalexample> ! <programlisting> ! Function Test () { ! static $count = 0; $count++; echo $count; ! if ($count < 10) { ! Test (); } $count--; } ! </programlisting> ! </informalexample> ! <sect1 id="variable-variable"> <title>Variable variables</title> <simpara> ! Sometimes it is convenient to be able to have variable variable ! names. That is, a variable name which can be set and used ! dynamically. A normal variable is set with a statement such as: ! <informalexample> ! <programlisting> $a = "hello"; ! </programlisting> ! </informalexample> <simpara> A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, <emphasis>hello</emphasis>, can be used as the name of a variable by using two dollar signs. ie. ! ! <informalexample> ! <programlisting> $$a = "world"; ! </programlisting> ! </informalexample> <simpara> At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement: ! <informalexample> ! <programlisting> echo "$a ${$a}"; ! </programlisting> ! </informalexample> <simpara> produces the exact same output as: ! <informalexample> ! <programlisting> echo "$a $hello"; ! </programlisting> ! </informalexample> <simpara> ie. they both produce: <emphasis>hello world</emphasis>. *************** *** 318,323 **** --- 346,352 ---- that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second. + <sect1 id="variable-external"> <title>Variables from outside PHP</title> <simpara></simpara> *************** *** 333,344 **** <para> <example> <title>Simple form variable</title> - <programlisting> ! &lt;form action="foo.php3" method="post"&gt; ! Name: &lt;input type="text" name="name"&gt;&lt;br&gt; ! &lt;input type="submit"&gt; ! &lt;/form&gt; </programlisting> </example> --- 362,372 ---- <para> <example> <title>Simple form variable</title> <programlisting> ! &lt;form action="foo.php3" method="post"> ! Name: &lt;input type="text" name="name">&lt;br> ! &lt;input type="submit"> ! &lt;/form> </programlisting> </example> *************** *** 358,373 **** <example> <title>More complex form variables</title> <programlisting> ! &lt;form action="array.html" method="post"&gt; ! Name: &lt;input type="text" name="personal[name]"&gt;&lt;br&gt; ! Email: &lt;input type="text" name="personal[email]"&gt;&lt;br&gt; ! Beer: &lt;br&gt; ! &lt;select multiple name="beer[]"&gt; ! &lt;option value="warthog"&gt;Warthog ! &lt;option value="guinness"&gt;Guinness ! &lt;/select&gt; ! &lt;input type="submit"&gt; ! &lt;/form&gt; </programlisting> </example> --- 386,401 ---- <example> <title>More complex form variables</title> <programlisting> ! &lt;form action="array.html" method="post"> ! Name: &lt;input type="text" name="personal[name]">&lt;br> ! Email: &lt;input type="text" name="personal[email]">&lt;br> ! Beer: &lt;br> ! &lt;select multiple name="beer[]"> ! &lt;option value="warthog">Warthog ! &lt;option value="guinness">Guinness ! &lt;/select> ! &lt;input type="submit"> ! &lt;/form> </programlisting> </example> *************** *** 386,394 **** When submitting a form, it is possible to use an image instead of the standard submit button with a tag like: ! <informalexample><programlisting> ! &lt;input type=image src="image.gif" name="sub"&gt; ! </programlisting></informalexample> <simpara> When the user clicks somewhere on the image, the accompanying --- 414,424 ---- When submitting a form, it is possible to use an image instead of the standard submit button with a tag like: ! <informalexample> ! <programlisting> ! &lt;input type=image src="image.gif" name="sub"> ! </programlisting> ! </informalexample> <simpara> When the user clicks somewhere on the image, the accompanying *************** *** 399,404 **** --- 429,435 ---- rather than an underscore, but PHP converts the period to an underscore automatically. + <sect2> <title>HTTP Cookies</title> *************** *** 419,427 **** If you wish to assign multiple values to a single cookie, just add <emphasis>[]</emphasis> to the cookie name. For example: ! <informalexample><programlisting> SetCookie ("MyCookie[]", "Testing", time()+3600); ! </programlisting></informalexample> <simpara> Note that a cookie will replace a previous cookie by the same --- 450,460 ---- If you wish to assign multiple values to a single cookie, just add <emphasis>[]</emphasis> to the cookie name. For example: ! <informalexample> ! <programlisting> SetCookie ("MyCookie[]", "Testing", time()+3600); ! </programlisting> ! </informalexample> <simpara> Note that a cookie will replace a previous cookie by the same *************** *** 438,469 **** </programlisting> </example> <sect2> <title>Environment variables</title> ! <para> ! PHP automatically makes environment variables available as ! normal PHP variables. ! <informalexample><programlisting> echo $HOME; /* Shows the HOME environment variable, if set. */ ! </programlisting></informalexample> ! <para> ! Since information coming in via GET, POST and Cookie mechanisms ! also automatically create PHP variables, it is sometimes best to ! explicitly read a variable from the environment in order to make ! sure that you are getting the right version. The ! <function>getenv</function> function can be used for this. You ! can also set an environment variable with the ! <function>putenv</function> function. ! </para> <sect2> <title>Server configuration directives</title> ! <simpara></simpara> <sect1 id="variable-typejuggling"> <title>Type juggling</title> <simpara> PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the --- 471,508 ---- </programlisting> </example> + <sect2> <title>Environment variables</title> ! ! <para> ! PHP automatically makes environment variables available as normal ! PHP variables. ! <informalexample> ! <programlisting> echo $HOME; /* Shows the HOME environment variable, if set. */ ! </programlisting> ! </informalexample> ! <para> ! Since information coming in via GET, POST and Cookie mechanisms ! also automatically create PHP variables, it is sometimes best to ! explicitly read a variable from the environment in order to make ! sure that you are getting the right version. The ! <function>getenv</function> function can be used for this. You ! can also set an environment variable with the ! <function>putenv</function> function. + <sect2> <title>Server configuration directives</title> ! <simpara></simpara> ! <sect1 id="variable-typejuggling"> <title>Type juggling</title> + <simpara> PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the *************** *** 482,495 **** change the types of the operands themselves; the only change is in how the operands are evaluated. ! <informalexample><programlisting> $foo = "0"; // $foo is a string (ASCII 48) $foo++; // $foo is the string "1" (ASCII 49) $foo += 1; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a double (3.3) $foo = 5 + "10 Little Piggies"; // $foo is a integer (15) $foo = 5 + "10 Small Pigs"; // $foo is an integer (15) ! </programlisting></informalexample> <simpara> If the last two examples above seem odd, see <link --- 521,536 ---- change the types of the operands themselves; the only change is in how the operands are evaluated. ! <informalexample> ! <programlisting> $foo = "0"; // $foo is a string (ASCII 48) $foo++; // $foo is the string "1" (ASCII 49) $foo += 1; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a double (3.3) $foo = 5 + "10 Little Piggies"; // $foo is a integer (15) $foo = 5 + "10 Small Pigs"; // $foo is an integer (15) ! </programlisting> ! </informalexample> <simpara> If the last two examples above seem odd, see <link *************** *** 523,532 **** desired type is written in parentheses before the variable which is to be cast. ! <informalexample><programlisting> $foo = 10; // $foo is an integer $bar = (double) $foo; // $bar is a double ! </programlisting></informalexample> <para> The casts allowed are: --- 564,575 ---- desired type is written in parentheses before the variable which is to be cast. ! <informalexample> ! <programlisting> $foo = 10; // $foo is an integer $bar = (double) $foo; // $bar is a double ! </programlisting> ! </informalexample> <para> The casts allowed are: *************** *** 542,551 **** Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent: ! <informalexample><programlisting> $foo = (int) $bar; $foo = ( int ) $bar; ! </programlisting></informalexample> <sect2 id="lang-syntax.stringconv"> --- 585,596 ---- Note that tabs and spaces are allowed inside the parentheses, so the following are functionally equivalent: ! <informalexample> ! <programlisting> $foo = (int) $bar; $foo = ( int ) $bar; ! </programlisting> ! </informalexample> <sect2 id="lang-syntax.stringconv"> *************** *** 568,574 **** exponent. The exponent is an 'e' or 'E' followed by one or more digits. ! <informalexample><programlisting> $foo = 1 + "10.5"; // $foo is a double (11.5) $foo = 1 + "-1.3e3"; // $foo is a double (-1299) $foo = 1 + "bob-1.3e3"; // $foo is an integer (1) --- 613,620 ---- exponent. The exponent is an 'e' or 'E' followed by one or more digits. ! <informalexample> ! <programlisting> $foo = 1 + "10.5"; // $foo is a double (11.5) $foo = 1 + "-1.3e3"; // $foo is a double (-1299) $foo = 1 + "bob-1.3e3"; // $foo is an integer (1) *************** *** 576,582 **** $foo = 1 + "10 Small Pigs"; // $foo is an integer (11) $foo = 1 + "10 Little Piggies"; // $foo is an integer (11); // the string contains 'e' ! </programlisting></informalexample> <simpara> For more information on this conversion, see the Unix manual --- 622,629 ---- $foo = 1 + "10 Small Pigs"; // $foo is an integer (11) $foo = 1 + "10 Little Piggies"; // $foo is an integer (11); // the string contains 'e' ! </programlisting> ! </informalexample> <simpara> For more information on this conversion, see the Unix manual *************** *** 592,611 **** <function>list</function> or <function>array</function> functions, or you can explicitly set each array element value. ! <informalexample><programlisting> $a[0] = "abc"; $a[1] = "def"; $b["foo"] = 13; ! </programlisting></informalexample> <para> You can also create an array by simply adding values to the array. ! <informalexample><programlisting> $a[] = "hello"; // $a[2] == "hello" $a[] = "world"; // $a[3] == "world" ! </programlisting></informalexample> <para> Arrays may be sorted using the <function>asort</function>, --- 639,662 ---- <function>list</function> or <function>array</function> functions, or you can explicitly set each array element value. ! <informalexample> ! <programlisting> $a[0] = "abc"; $a[1] = "def"; $b["foo"] = 13; ! </programlisting> ! </informalexample> <para> You can also create an array by simply adding values to the array. ! <informalexample> ! <programlisting> $a[] = "hello"; // $a[2] == "hello" $a[] = "world"; // $a[3] == "world" ! </programlisting> ! </informalexample> <para> Arrays may be sorted using the <function>asort</function>, -- 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 (#3046) next »