Needs more tweaking, I don't know if every PHP4/Zend quirks are in.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@21809 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Egon Schmid 2000-03-20 23:32:11 +00:00
parent af4a9d7832
commit a4a26c5a66

View file

@ -1,9 +1,8 @@
<chapter id="language.types">
<title>Types</title>
<para>
PHP supports the following types:
<chapter id="language.types">
<title>Types</title>
<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
@ -146,7 +143,6 @@ $a = 1.234; $a = 1.2e3;
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,7 +151,6 @@ $a = 1.234; $a = 1.2e3;
a single-quoted string. Variables will <emphasis>not</emphasis> be
expanded inside a single-quoted string.
</para>
<para>
Another way to delimit strings is by using here doc syntax
("&lt;&lt;&lt;"). One should provide an identifier after
@ -178,20 +173,17 @@ EOD;
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>
@ -231,13 +223,13 @@ $last = $str[strlen($str)-1];
<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
@ -245,13 +237,12 @@ $last = $str[strlen($str)-1];
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)
@ -264,19 +255,17 @@ $foo = "10.0 pigs " + 1; // $foo is integer (11)
$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 ) . "&lt;br&gt;\n";
echo "\$foo==$foo; type is " . gettype ($foo) . "&lt;br&gt;\n";
</programlisting>
</informalexample>
</para>
@ -289,7 +278,8 @@ echo "\$foo==$foo; type is " . gettype( $foo ) . "&lt;br&gt;\n";
<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>
@ -299,7 +289,6 @@ echo "\$foo==$foo; type is " . gettype( $foo ) . "&lt;br&gt;\n";
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";
@ -308,36 +297,34 @@ $b["foo"] = 13;
</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>
@ -347,7 +334,6 @@ $a[] = "world"; // $a[3] == "world"
<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
@ -359,8 +345,8 @@ $a[3]["bar"] = $f; # (you can mix numeric and associative indices)
$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
@ -371,25 +357,20 @@ $a[3]['bar'] = 'Bob';
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';
@ -397,14 +378,12 @@ echo "This will work: {$a[3][bar]}";
</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:
@ -424,12 +403,11 @@ $a = array(
3 => 4
);
</programlisting>
</informalexample></para>
</informalexample>
</para>
<para>
The <function>array</function> function can be nested for
multi-dimensional arrays:
<informalexample>
<programlisting role="php">
&lt;?
@ -471,10 +449,10 @@ echo $a["apple"]["taste"]; # will output "sweet"
statement to instantiate the object to a variable.
<informalexample>
<programlisting role="php">
<programlisting role="php">
&lt;?php
class foo {
function do_foo () {
function do_foo() {
echo "Doing foo.";
}
}
@ -485,17 +463,16 @@ $bar->do_foo();
</programlisting>
</informalexample>
</para>
<simpara>
For a full discussion, please read the section <link
linkend="language.oop">Classes and Objects</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
@ -506,7 +483,6 @@ $bar->do_foo();
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
@ -515,7 +491,6 @@ $bar->do_foo();
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)
@ -527,37 +502,31 @@ $foo = 5 + "10 Small Pigs"; // $foo is integer (15)
</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 ) . "&lt;br&gt;\n";
echo "\$foo==$foo; type is " . gettype ($foo) . "&lt;br&gt;\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
@ -565,12 +534,10 @@ $a[0] = "f"; // $a becomes an array, with $a[0] holding "f"
</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
@ -578,14 +545,12 @@ $a[0] = "f"; // What about string offsets? What happens?
</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
@ -594,20 +559,19 @@ $a[0] = "f"; // What about string offsets? What happens?
</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>
@ -628,11 +592,9 @@ $bar = (double) $foo; // $bar is a double
</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;
@ -640,13 +602,11 @@ $foo = ( int ) $bar;
</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:
@ -658,7 +618,6 @@ echo $arr[0]; // outputs 'ciao'
</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
@ -677,19 +636,19 @@ echo $obj-&gt;scalar; // outputs 'ciao'
</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:
-->