Date: 08/31/00
- Next message: eschmid+sic <email protected>: "Re: [PHP-DOC] cvs: phpdoc /en/functions math.xml"
- Previous message: Luca Perugini: "[PHP-DOC] cvs: phpdoc /it/language operators.xml"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
perugini Thu Aug 31 15:24:36 2000 EDT
Modified files:
/phpdoc/it/language types.xml
Log:
Sync with en tree.
Index: phpdoc/it/language/types.xml
diff -u phpdoc/it/language/types.xml:1.1 phpdoc/it/language/types.xml:1.2
--- phpdoc/it/language/types.xml:1.1 Sat Dec 18 17:01:09 1999
+++ phpdoc/it/language/types.xml Thu Aug 31 15:24:36 2000
@@ -1,9 +1,8 @@
- <chapter id="language.types">
- <title>Types</title>
+ <chapter id="language.types">
+ <title>Types</title>
- <para>
- PHP supports the following types:
-
+ <para>
+ PHP supports the following types:
<itemizedlist>
<listitem>
<simpara>
@@ -12,7 +11,8 @@
</listitem>
<listitem>
<simpara>
- <link linkend="language.types.double">floating-point numbers</link>
+ <link linkend="language.types.double">floating-point numbers
+ </link>
</simpara>
</listitem>
<listitem>
@@ -32,20 +32,17 @@
</listitem>
</itemizedlist>
</para>
-
<simpara>
The type of a variable is usually not set by the programmer;
rather, it is decided at runtime by PHP depending on the context in
which that variable is used.
</simpara>
-
<simpara>
If you would like to force a variable to be converted to a certain
type, you may either <link
linkend="language.types.typecasting">cast</link> the variable or
use the <function>settype</function> function on it.
</simpara>
-
<simpara>
Note that a variable may behave in different manners in certain
situations, depending on what type it is at the time. For more
@@ -65,6 +62,9 @@
$a = 0x12; # hexadecimal number (equivalent to 18 decimal)
</programlisting>
</informalexample>
+ The size of an integer is platform-dependent, although a
+ maximum value of about 2 billion is the usual value
+ (that's 32 bits signed).
</para>
</sect1>
@@ -78,7 +78,35 @@
$a = 1.234; $a = 1.2e3;
</programlisting>
</informalexample>
- </para>
+ The size of a floating point number is platform-dependent,
+ although a maximum of ~1.8e308 with a precision of roughly 14
+ decimal digits is a common value (that's 64 bit IEEE format).
+ </para>
+ <warning id="warn.float-precision">
+ <para>
+ It is quite usual that simple decimal fractions like
+ <literal>0.1</literal> or <literal>0.7</literal> cannot be
+ converted into their internal binary counterparts without a
+ little loss of precision. This can lead to confusing results: for
+ example, <literal>floor((0.1+0.7)*10)</literal> will usually
+ return <literal>7</literal> instead of the expected
+ <literal>8</literal> as the result of the internal representation
+ really being something like <literal>7.9999999999...</literal>.
+ </para>
+ <para>
+ This is related to the fact that it is impossible to exactly
+ express some fractions in decimal notation with a finite number
+ of digits. For instance, <literal>1/3</literal> in decimal form
+ becomes <literal>0.3333333. . .</literal>.
+ </para>
+ <para>
+ So never trust floating number results to the last digit and
+ never compare floating point numbers for equality. If you really
+ need higher precision, you should use the <link
+ linkend="ref.bc">arbitrary precision math functions</link>
+ instead.
+ </para>
+ </warning>
</sect1>
<sect1 id="language.types.string">
@@ -103,15 +131,15 @@
<tbody>
<row>
<entry><literal>\n</literal></entry>
- <entry>newline</entry>
+ <entry>linefeed (LF or 0x0A in ASCII)</entry>
</row>
<row>
<entry><literal>\r</literal></entry>
- <entry>carriage</entry>
+ <entry>carriage return (CR or 0x0D in ASCII)</entry>
</row>
<row>
<entry><literal>\t</literal></entry>
- <entry>horizontal tab</entry>
+ <entry>horizontal tab (HT or 0x09 in ASCII)</entry>
</row>
<row>
<entry><literal>\\</literal></entry>
@@ -146,7 +174,6 @@
You can escape any other character, but a warning will be issued
at the highest warning level.
</para>
-
<para>
The second way to delimit a string uses the single-quote ("'")
character. When a string is enclosed in single quotes, the only
@@ -155,42 +182,69 @@
a single-quoted string. Variables will <emphasis>not</emphasis> be
expanded inside a single-quoted string.
</para>
-
- <para>
+ <simpara>
Another way to delimit strings is by using here doc syntax
- (">>>"). One should provide an identifier after
- <literal>>>></literal>, then the string, and then the
- same identifier to close the quotation.
+ ("<<<"). One should provide an identifier after
+ <literal><<<</literal>, then the string, and then the
+ same identifier to close the quotation. The closing identifier
+ <emphasis>must</emphasis> begin in the first column of the line.
+ </simpara>
+ <para>
+ Here doc text behaves just like a double-quoted string, without
+ the double-quotes. This means that you do not need to escape quotes
+ in your here docs, but you can still use the escape codes listed
+ above. Variables are expanded, but the same care must be taken
+ when expressing complex variables inside a here doc as with
+ strings.
<example>
<title>Here doc string quoting example</title>
<programlisting>
-$str = >>>EOD
+<?php
+$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
+
+/* More complex example, with variables. */
+class foo {
+ var $foo;
+ var $bar;
+
+ function foo() {
+ $this->foo = 'Foo';
+ $this->bar = array('Bar1', 'Bar2', 'Bar3');
+ }
+}
+
+$foo = new foo();
+$name = 'MyName';
+
+echo <<<EOT
+My name is "$name". I am printing some $foo->foo.
+Now, I am printing some {$foo->bar[1]}.
+This should print a capital 'A': \x41
+EOT;
+?>
</programlisting>
</example>
</para>
<note>
<para>
- Here docs was added in PHP 4.
+ Here doc support was added in PHP 4.
</para>
</note>
-
<para>
Strings may be concatenated using the '.' (dot) operator. Note
that the '+' (addition) operator will not work for this. Please
see <link linkend="language.operators.string">String
operators</link> for more information.
</para>
-
<para>
Characters within strings may be accessed by treating the string
as a numerically-indexed array of characters, using C-like
syntax. See below for examples.
</para>
-
<para>
<example>
<title>Some string examples</title>
@@ -230,13 +284,13 @@
<simpara>
When a string is evaluated as a numeric value, the resulting
- value and type are determined as follows.</simpara>
-
+ value and type are determined as follows.
+ </simpara>
<simpara>
The string will evaluate as a double if it contains any of the
characters '.', 'e', or 'E'. Otherwise, it will evaluate as an
- integer.</simpara>
-
+ integer.
+ </simpara>
<para>
The value is given by the initial portion of the string. If the
string starts with valid numeric data, this will be the value
@@ -244,13 +298,12 @@
is an optional sign, followed by one or more digits (optionally
containing a decimal point), followed by an optional
exponent. The exponent is an 'e' or 'E' followed by one or more
- digits.</para>
-
+ digits.
+ </para>
<simpara>
When the first expression is a string, the type of the variable
will depend on the second expression.
</simpara>
-
<informalexample>
<programlisting role="php">
$foo = 1 + "10.5"; // $foo is double (11.5)
@@ -263,19 +316,17 @@
$foo = "10.0 pigs " + 1.0; // $foo is double (11)
</programlisting>
</informalexample>
-
<simpara>
For more information on this conversion, see the Unix manual page
for strtod(3).
</simpara>
-
<para>
If you would like to test any of the examples in this section,
you can cut and paste the examples and insert the following line
to see for yourself what's going on:
<informalexample>
<programlisting role="php">
-echo "\$foo==$foo; type is " . gettype( $foo ) . "<br>\n";
+echo "\$foo==$foo; type is " . gettype ($foo) . "<br>\n";
</programlisting>
</informalexample>
</para>
@@ -288,7 +339,8 @@
<para>
Arrays actually act like both hash tables (associative arrays) and
- indexed arrays (vectors).</para>
+ indexed arrays (vectors).
+ </para>
<sect2 id="language.types.array.single-dim">
<title>Single Dimension Arrays</title>
@@ -298,7 +350,6 @@
is no difference between the two. You can create an array using
the <function>list</function> or <function>array</function>
functions, or you can explicitly set each array element value.
-
<informalexample>
<programlisting role="php">
$a[0] = "abc";
@@ -307,36 +358,34 @@
</programlisting>
</informalexample>
</para>
-
<para>
You can also create an array by simply adding values to the
array. When you assign a value to an array variable using empty
brackets, the value will be added onto the end of the array.
-
<informalexample>
<programlisting role="php">
$a[] = "hello"; // $a[2] == "hello"
$a[] = "world"; // $a[3] == "world"
</programlisting>
- </informalexample></para>
-
+ </informalexample>
+ </para>
<para>
Arrays may be sorted using the <function>asort</function>,
<function>arsort</function>, <function>ksort</function>,
<function>rsort</function>, <function>sort</function>,
<function>uasort</function>, <function>usort</function>, and
<function>uksort</function> functions depending on the type of
- sort you want.</para>
-
+ sort you want.
+ </para>
<para>
You can count the number of items in an array using the
- <function>count</function> function.</para>
-
+ <function>count</function> function.
+ </para>
<para>
- You can traverse an array using <function>next</function> and
- <function>prev</function> functions. Another common way to
- traverse an array is to use the <function>each</function>
- function.
+ You can traverse an array using <function>next</function> and
+ <function>prev</function> functions. Another common way to
+ traverse an array is to use the <function>each</function>
+ function.
</para>
</sect2>
@@ -346,7 +395,6 @@
<para>
Multi-dimensional arrays are actually pretty simple. For each
dimension of the array, you add another [key] value to the end:
-
<informalexample>
<programlisting role="php">
$a[1] = $f; # one dimensional examples
@@ -358,8 +406,8 @@
$a["foo"][4]["bar"][0] = $f; # four dimensional!
</programlisting>
- </informalexample></para>
-
+ </informalexample>
+ </para>
<para>
In PHP3 it is not possible to reference multidimensional arrays
directly within strings. For instance, the following will not
@@ -370,25 +418,20 @@
echo "This won't work: $a[3][bar]";
</programlisting>
</informalexample>
-
In PHP3, the above will output <computeroutput>This won't work:
Array[bar]</computeroutput>. The string concatenation operator,
however, can be used to overcome this:
-
<informalexample>
<programlisting role="php">
$a[3]['bar'] = 'Bob';
echo "This will work: " . $a[3][bar];
</programlisting>
</informalexample>
-
</para>
-
<para>
In PHP4, however, the whole problem may be circumvented by
enclosing the array reference (inside the string) in curly
braces:
-
<informalexample>
<programlisting role="php">
$a[3]['bar'] = 'Bob';
@@ -396,14 +439,12 @@
</programlisting>
</informalexample>
</para>
-
<para>
You can "fill up" multi-dimensional arrays in many ways, but the
trickiest one to understand is how to use the
<function>array</function> command for associative arrays. These
two snippets of code fill up the one-dimensional array in the
same way:
-
<informalexample>
<programlisting role="php">
# Example 1:
@@ -423,12 +464,11 @@
3 => 4
);
</programlisting>
- </informalexample></para>
-
+ </informalexample>
+ </para>
<para>
The <function>array</function> function can be nested for
multi-dimensional arrays:
-
<informalexample>
<programlisting role="php">
<?
@@ -453,8 +493,12 @@
echo $a["apple"]["taste"]; # will output "sweet"
?>
</programlisting>
- </informalexample></para></sect2></sect1>
+ </informalexample>
+ </para>
+ </sect2>
+ </sect1>
+
<sect1 id="language.types.object">
<title>Objects</title>
@@ -462,27 +506,34 @@
<title>Object Initialization</title>
<para>
- To initialize an object, you use the new statement to instantiate
- the object to a variable.
+ To initialize an object, you use the <literal>new</literal>
+ statement to instantiate the object to a variable.
<informalexample>
- <programlisting role="php">
+ <programlisting role="php">
+<?php
class foo {
- function do_foo () {
+ function do_foo() {
echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo();
+?>
</programlisting>
</informalexample>
</para>
+ <simpara>
+ For a full discussion, please read the section <link
+ linkend="language.oop">Classes and Objects</link>.
+ </simpara>
+
</sect2>
</sect1>
<sect1 id="language.types.type-juggling">
- <title>Type juggling</title>
+ <title>Type Juggling</title>
<simpara>
PHP does not require (or support) explicit type definition in
@@ -493,7 +544,6 @@
integer value to <parameter>var</parameter>, it becomes an
integer.
</simpara>
-
<para>
An example of PHP's automatic type conversion is the addition
operator '+'. If any of the operands is a double, then all
@@ -502,7 +552,6 @@
and the result will also be an integer. Note that this does NOT
change the types of the operands themselves; the only change is in
how the operands are evaluated.
-
<informalexample>
<programlisting role="php">
$foo = "0"; // $foo is string (ASCII 48)
@@ -514,37 +563,31 @@
</programlisting>
</informalexample>
</para>
-
<simpara>
If the last two examples above seem odd, see <link
linkend="language.types.string.conversion">String
conversion</link>.
</simpara>
-
<simpara>
If you wish to force a variable to be evaluated as a certain type,
see the section on <link linkend="language.types.typecasting">Type
casting</link>. If you wish to change the type of a variable, see
<function>settype</function>.
</simpara>
-
<para>
If you would like to test any of the examples in this section, you
can cut and paste the examples and insert the following line to
see for yourself what's going on:
<informalexample>
<programlisting role="php">
-echo "\$foo==$foo; type is " . gettype( $foo ) . "<br>\n";
+echo "\$foo==$foo; type is " . gettype ($foo) . "<br>\n";
</programlisting>
</informalexample>
</para>
-
-
<note>
<para>
The behaviour of an automatic conversion to array is currently
undefined.
-
<informalexample>
<programlisting role="php">
$a = 1; // $a is an integer
@@ -552,12 +595,10 @@
</programlisting>
</informalexample>
</para>
-
<para>
While the above example may seem like it should clearly result in
$a becoming an array, the first element of which is 'f', consider
this:
-
<informalexample>
<programlisting role="php">
$a = "1"; // $a is a string
@@ -565,14 +606,12 @@
</programlisting>
</informalexample>
</para>
-
<para>
Since PHP supports indexing into strings via offsets using the
same syntax as array indexing, the example above leads to a
problem: should $a become an array with its first element being
"f", or should "f" become the first character of the string $a?
</para>
-
<para>
For this reason, as of PHP 3.0.12 and PHP 4.0b3-RC4, the result
of this automatic conversion is considered to be undefined. Fixes
@@ -581,20 +620,19 @@
</note>
<sect2 id="language.types.typecasting">
- <title>Type casting</title>
+ <title>Type Casting</title>
<para>
Type casting in PHP works much as it does in C: the name of the
desired type is written in parentheses before the variable which
is to be cast.
-
<informalexample>
<programlisting role="php">
$foo = 10; // $foo is an integer
$bar = (double) $foo; // $bar is a double
</programlisting>
- </informalexample></para>
-
+ </informalexample>
+ </para>
<para>
The casts allowed are:
<itemizedlist>
@@ -615,11 +653,9 @@
</listitem>
</itemizedlist>
</para>
-
<para>
Note that tabs and spaces are allowed inside the parentheses, so
the following are functionally equivalent:
-
<informalexample>
<programlisting role="php">
$foo = (int) $bar;
@@ -627,13 +663,11 @@
</programlisting>
</informalexample>
</para>
-
<para>
It may not be obvious exactly what will happen when casting
between certain types. For instance, the following should be
noted.
</para>
-
<para>
When casting from a scalar or a string variable to an array, the
variable will become the first element of the array:
@@ -645,7 +679,6 @@
</programlisting>
</informalexample>
</para>
-
<para>
When casting from a scalar or a string variable to an object, the
variable will become an attribute of the object; the attribute
@@ -664,19 +697,19 @@
</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
- sgml-parent-document:nil
- sgml-default-dtd-file:"../../manual.ced"
- sgml-exposed-tags:nil
- sgml-local-catalogs:nil
- sgml-local-ecat-files:nil
- End:
- -->
+<!-- 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
+sgml-parent-document:nil
+sgml-default-dtd-file:"../../manual.ced"
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+-->
- Next message: eschmid+sic <email protected>: "Re: [PHP-DOC] cvs: phpdoc /en/functions math.xml"
- Previous message: Luca Perugini: "[PHP-DOC] cvs: phpdoc /it/language operators.xml"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

