Some markup structure and whitespace fixes. This stye of markup is more consistent with DB 5. No content changes.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@253176 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Gwynne Raskind 2008-02-18 22:32:54 +00:00
parent 0c705633e9
commit 847b241496
11 changed files with 727 additions and 629 deletions

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.198 $ -->
<!-- $Revision: 1.199 $ -->
<chapter xml:id="language.types" xmlns="http://docbook.org/ns/docbook">
<title>Types</title>
@ -12,126 +12,136 @@
<para>
Four scalar types:
</para>
<itemizedlist>
<itemizedlist>
<listitem>
<simpara>
<type>boolean</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>boolean</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>integer</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>integer</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>float</type> (floating-point number, aka '<type>double</type>')
</simpara>
</listitem>
<listitem>
<simpara>
<type>float</type> (floating-point number, aka '<type>double</type>')
</simpara>
</listitem>
<listitem>
<simpara>
<type>string</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>string</type>
</simpara>
</listitem>
</itemizedlist>
</itemizedlist>
<para>
Two compound types:
</para>
<itemizedlist>
<itemizedlist>
<listitem>
<simpara>
<type>array</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>array</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>object</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>object</type>
</simpara>
</listitem>
</itemizedlist>
</itemizedlist>
<para>
And finally two special types:
</para>
<itemizedlist>
<itemizedlist>
<listitem>
<simpara>
<type>resource</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>resource</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>NULL</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>NULL</type>
</simpara>
</listitem>
</itemizedlist>
</itemizedlist>
This manual also introduces some
<link linkend="language.pseudo-types">pseudo-types</link>
for readability reasons:
<para>
This manual also introduces some
<link linkend="language.pseudo-types">pseudo-types</link> for readability
reasons:
</para>
<itemizedlist>
<itemizedlist>
<listitem>
<simpara>
<type>mixed</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>mixed</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>number</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>number</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>callback</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>callback</type>
</simpara>
</listitem>
</itemizedlist>
</itemizedlist>
<para>
And the pseudo-variable <parameter>$...</parameter>.
</para>
<simpara>
You may also find some references to the type "double". Consider
double the same as float, the two names exist only for historic
reasons.
You may also find some references to the type "double". Consider double the
same as float, the two names exist only for historic reasons.
</simpara>
<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.
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>
<note>
<simpara>
If you want to check out the type and value of a certain <link
linkend="language.expressions">expression</link>, use
If you want to check out the type and value of a certain
<link linkend="language.expressions">expression</link>, use
<function>var_dump</function>.
</simpara>
<para>
If you simply want a human-readable representation of the type for
debugging, use <function>gettype</function>. To check for a certain type,
do <emphasis>not</emphasis> use <function>gettype</function>, but use the
debugging, use <function>gettype</function>. To check for a certain type, do
<emphasis>not</emphasis> use <function>gettype</function>, but use the
<literal>is_<replaceable>type</replaceable></literal> functions. Some
examples:
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a_bool = TRUE; // a boolean
@ -154,26 +164,23 @@ if (is_string($a_bool)) {
}
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
</note>
<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.
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 be evaluated with different values in certain
situations, depending on what type it is at the time. For more
information, see the section on <link
linkend="language.types.type-juggling">Type Juggling</link>. Also, you
may be interested in viewing
<link linkend="types.comparisons">the type comparison tables</link>,
as they show examples of various type related comparisons.
situations, depending on what type it is at the time. For more information,
see the section on <link linkend="language.types.type-juggling">Type
Juggling</link>. Also, you may be interested in viewing
<link linkend="types.comparisons">the type comparison tables</link>, as they
show examples of various type related comparisons.
</simpara>
</sect1>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.array">
<title>Arrays</title>
@ -11,6 +11,7 @@
collection, stack, queue and probably more. Because you can have another PHP
array as a value, you can also quite easily simulate trees.
</para>
<para>
Explanation of those data structures is beyond the scope of this manual, but
you'll find at least one example for each of them. For more information we
@ -22,24 +23,24 @@
<sect3 xml:id="language.types.array.syntax.array-func">
<title>Specifying with <function>array</function></title>
<para>
An <type>array</type> can be created by the <function>array</function>
language-construct. It takes a certain number of comma-separated
<literal><replaceable>key</replaceable> =&gt;
<replaceable>value</replaceable></literal> pairs.
</para>
<para>
<synopsis>
<synopsis>
array( <optional> <replaceable>key</replaceable> =&gt; </optional> <replaceable>value</replaceable>
, ...
)
// <replaceable>key</replaceable> may be an <type>integer</type> or <type>string</type>
// <replaceable>value</replaceable> may be any value
</synopsis>
</para>
<para>
<informalexample>
<programlisting role="php">
</synopsis>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$arr = array("foo" => "bar", 12 => true);
@ -48,9 +49,9 @@ echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<para>
A <varname>key</varname> may be either an <type>integer</type> or a
<type>string</type>. If a key is the standard representation of an
@ -61,10 +62,13 @@ echo $arr[12]; // 1
There are no different indexed and associative array types in PHP; there is
only one array type, which can both contain integer and string indices.
</para>
<para>
A value can be of any PHP type.
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
@ -74,16 +78,18 @@ echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<para>
If you do not specify a key for a given value, then the maximum of the
integer indices is taken, and the new key will be that maximum value + 1. If
you specify a key that already has a value assigned to it, that value will
be overwritten.
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// This array is the same as ...
@ -93,9 +99,9 @@ array(5 => 43, 32, 56, "b" => 12);
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<warning>
<simpara>
As of PHP 4.3.0, the index generation behaviour described above has
@ -105,6 +111,7 @@ array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
the same as positive indices are.
</simpara>
</warning>
<para>
Using &true; as a key will evaluate to <type>integer</type>
<literal>1</literal> as key. Using &false; as a key will evaluate to
@ -113,33 +120,43 @@ array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
the empty string as key will create (or overwrite) a key with the empty
string and its value; it is not the same as using empty brackets.
</para>
<para>
You cannot use arrays or objects as keys. Doing so will result in a warning:
<literal>Illegal offset type</literal>.
</para>
</sect3>
<sect3 xml:id="language.types.array.syntax.modifying">
<title>Creating/modifying with square-bracket syntax</title>
<para>
You can also modify an existing array by explicitly setting values in it.
</para>
<para>
This is done by assigning values to the array while specifying the key in
brackets. You can also omit the key, add an empty pair of brackets
("<literal>[]</literal>") to the variable name in that case.
<synopsis>
</para>
<synopsis>
$arr[<replaceable>key</replaceable>] = <replaceable>value</replaceable>;
$arr[] = <replaceable>value</replaceable>;
// <replaceable>key</replaceable> may be an <type>integer</type> or <type>string</type>
// <replaceable>value</replaceable> may be any value
</synopsis>
</synopsis>
<para>
If <varname>$arr</varname> doesn't exist yet, it will be created. So this is
also an alternative way to specify an array. To change a certain value, just
assign a new value to an element specified with its key. If you want to
remove a key/value pair, you need to <function>unset</function> it.
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$arr = array(5 => 1, 12 => 2);
@ -155,9 +172,9 @@ unset($arr[5]); // This removes the element from the array
unset($arr); // This deletes the whole array
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<note>
<para>
As mentioned above, if you provide the brackets with no key specified, then
@ -166,17 +183,17 @@ unset($arr); // This deletes the whole array
be <literal>0</literal> (zero). If you specify a key that already has a
value assigned to it, that value will be overwritten.
</para>
<para>
<warning>
<simpara>
As of PHP 4.3.0, the index generation behaviour described above has
changed. Now, if you append to an array in which the current maximum key
is negative, then the next key created will be zero
(<literal>0</literal>). Before, the new index would have been set to the
largest existing key + 1, the same as positive indices are.
</simpara>
</warning>
</para>
<warning>
<simpara>
As of PHP 4.3.0, the index generation behaviour described above has
changed. Now, if you append to an array in which the current maximum key
is negative, then the next key created will be zero
(<literal>0</literal>). Before, the new index would have been set to the
largest existing key + 1, the same as positive indices are.
</simpara>
</warning>
<para>
Note that the maximum integer key used for this <emphasis>need not
currently exist in the array</emphasis>. It simply must have existed in the
@ -238,23 +255,28 @@ Array
</informalexample>
</note>
</sect3>
</sect2><!-- end syntax -->
<sect2 xml:id="language.types.array.useful-funcs">
<title>Useful functions</title>
<para>
There are quite a few useful functions for working with arrays. See the
<link linkend="ref.array">array functions</link> section.
</para>
<note>
<para>
The <function>unset</function> function allows unsetting keys of an array.
Be aware that the array will NOT be reindexed. If you only use "usual
integer indices" (starting from zero, increasing by one), you can achieve
the reindex effect by using <function>array_values</function>.
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
@ -271,9 +293,8 @@ $b = array_values($a);
]]>
</programlisting>
</informalexample>
</para>
</note>
<para>
The <link linkend="control-structures.foreach">foreach</link> control
structure exists specifically for arrays. It provides an easy way to traverse
@ -286,12 +307,15 @@ $b = array_values($a);
<sect3 xml:id="language.types.array.foo-bar">
<title>Why is <literal>$foo[bar]</literal> wrong?</title>
<para>
You should always use quotes around a string literal array index. For
example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar] wrong? You
might have seen the following syntax in old scripts:
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo[bar] = 'enemy';
@ -299,8 +323,10 @@ echo $foo[bar];
// etc
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<para>
This is wrong, but it works. Then, why is it wrong? The reason is that this
code has an undefined constant (bar) rather than a string ('bar' - notice
the quotes), and PHP may in future define constants which, unfortunately for
@ -311,6 +337,7 @@ echo $foo[bar];
<constant>bar</constant>, then PHP will substitute in the string
<literal>'bar'</literal> and use that.
</para>
<note>
<simpara>
This does not mean to <emphasis>always</emphasis> quote the key. You do not
@ -319,6 +346,7 @@ echo $foo[bar];
<link linkend="language.variables">variables</link>, as this will prevent
PHP from interpreting them.
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
@ -361,10 +389,13 @@ Good: 2
]]>
</screen>
</note>
<para>
More examples to demonstrate this fact:
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Let's show all errors
@ -409,9 +440,9 @@ print "Hello $_GET['foo']";
print "Hello " . $arr['fruit']; // Hello apple
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<para>
When you turn <function>error_reporting</function> up to show
<constant>E_NOTICE</constant> level errors (such as setting it to
@ -419,26 +450,32 @@ print "Hello " . $arr['fruit']; // Hello apple
<link linkend="ini.error-reporting">error_reporting</link> is turned down to
not show them.
</para>
<para>
As stated in the <link linkend="language.types.array.syntax">syntax</link>
section, there must be an expression between the square brackets
('<literal>[</literal>' and '<literal>]</literal>'). That means that you
can write things like this:
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo $arr[somefunc($bar)];
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<para>
This is an example of using a function return value as the array index. PHP
also knows about constants, as you may have seen the <literal>E_*</literal>
ones before.
ones before.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$error_descriptions[E_ERROR] = "A fatal error has occured";
@ -446,13 +483,17 @@ $error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE] = "This is just an informal notice";
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<para>
Note that <literal>E_ERROR</literal> is also a valid identifier, just like
<literal>bar</literal> in the first example. But the last example is in fact
the same as writing:
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$error_descriptions[1] = "A fatal error has occured";
@ -460,10 +501,13 @@ $error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<para>
because <literal>E_ERROR</literal> equals <literal>1</literal>, etc.
</para>
<para>
As we already explained in the above examples, <literal>$foo[bar]</literal>
still works but is wrong. It works, because <literal>bar</literal> is due to
@ -472,8 +516,10 @@ $error_descriptions[8] = "This is just an informal notice";
you meant <literal>bar</literal> literally, as the string
<literal>"bar"</literal>, but that you forgot to write the quotes.
</para>
<sect4 xml:id="language.types.array.foo-bar.why">
<title>So why is it bad then?</title>
<para>
At some point in the future, the PHP team might want to add another
constant or keyword, or you may introduce another constant into your
@ -482,6 +528,7 @@ $error_descriptions[8] = "This is just an informal notice";
way, since they are special
<link linkend="reserved">reserved keywords</link>.
</para>
<note>
<simpara>
To reiterate, inside a double-quoted <type>string</type>, it's valid to
@ -491,6 +538,7 @@ $error_descriptions[8] = "This is just an informal notice";
strings</link>.
</simpara>
</note>
</sect4>
</sect3>
</sect2>
@ -512,8 +560,10 @@ $error_descriptions[8] = "This is just an informal notice";
the class name prepended to the variable name; protected variables have a '*'
prepended to the variable name. These prepended values have null bytes on
either side. This can result in some unexpected behaviour.
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
@ -529,9 +579,10 @@ class B extends A {
var_dump((array) new B());
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<para>
The above will appear to have two keys named 'AA', although one of them is
actually named '\0A\0A'.
</para>
@ -543,6 +594,7 @@ var_dump((array) new B());
<sect2 xml:id="language.types.array.comparing">
<title>Comparing</title>
<para>
It is possible to compare arrays by <function>array_diff</function> and by
<link linkend="language.operators.array">Array operators</link>.
@ -551,13 +603,14 @@ var_dump((array) new B());
<sect2 xml:id="language.types.array.examples">
<title>Examples</title>
<para>
The array type in PHP is very versatile, so here will be some examples to
show you the full power of arrays.
</para>
<para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// this
@ -582,10 +635,9 @@ $b[] = 'c';
// or simply array('a', 'b', 'c')
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<example>
<title>Using array()</title>
<programlisting role="php">
@ -657,9 +709,11 @@ Do you like yellow?
<para>
Changing values of the array directly is possible since PHP 5 by passing them
as reference. Prior versions need workaround:
<example xml:id="language.types.array.examples.changeloop">
<title>Collection</title>
<programlisting role="php">
</para>
<example xml:id="language.types.array.examples.changeloop">
<title>Collection</title>
<programlisting role="php">
<![CDATA[
<?php
// PHP 5
@ -677,9 +731,9 @@ foreach ($colors as $key => $color) {
print_r($colors);
?>
]]>
</programlisting>
&example.outputs;
<screen>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
@ -689,23 +743,25 @@ Array
[3] => YELLOW
)
]]>
</screen>
</example>
</para>
</screen>
</example>
<para>
This example creates a one-based array.
<example>
<title>One-based index</title>
<programlisting role="php">
</para>
<example>
<title>One-based index</title>
<programlisting role="php">
<![CDATA[
<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>
]]>
</programlisting>
&example.outputs;
<screen>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
@ -714,9 +770,9 @@ Array
[3] => 'March'
)
]]>
</screen>
</example>
</para>
</screen>
</example>
<example>
<title>Filling an array</title>
<programlisting role="php">
@ -732,12 +788,14 @@ closedir($handle);
]]>
</programlisting>
</example>
<para>
Arrays are ordered. You can also change the order using various sorting
functions. See the <link linkend="ref.array">array functions</link> section
for more information. You can count the number of items in an array using the
<function>count</function> function.
</para>
<example>
<title>Sorting an array</title>
<programlisting role="php">
@ -749,10 +807,12 @@ print_r($files);
]]>
</programlisting>
</example>
<para>
Because the value of an array can be anything, it can also be another array.
This way you can make recursive and multi-dimensional arrays.
</para>
<example>
<title>Recursive and multi-dimensional arrays</title>
<programlisting role="php">
@ -786,13 +846,16 @@ $juices["apple"]["green"] = "good";
]]>
</programlisting>
</example>
<para>
You should be aware that array assignment always involves value copying. It
also means that the internal array pointer used by
<function>current</function> and similar functions is reset. You need to use
the reference operator to copy an array by reference.
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$arr1 = array(2, 3);
@ -804,9 +867,9 @@ $arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
</sect2>
</sect1>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.boolean">
<title>Booleans</title>
@ -19,23 +19,27 @@
<para>
To specify a boolean literal, use either the keyword &true; or &false;. Both
are case-insensitive.
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = True; // assign the value TRUE to $foo
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<para>
Usually you use some kind of
<link linkend="language.operators">operator</link> which returns a
<type>boolean</type> value, and then pass it on to a
<link linkend="language.control-structures">control structure</link>.
<informalexample>
<programlisting role="php">
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// == is an operator which test
@ -55,85 +59,91 @@ if ($show_separators) {
}
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="language.types.boolean.casting">
<title>Converting to boolean</title>
<simpara>
To explicitly convert a value to <type>boolean</type>, use either the
<literal>(bool)</literal> or the <literal>(boolean)</literal> cast. However,
in most cases you do not need to use the cast, since a value will be
automatically converted if an operator, function or control structure
requires a <type>boolean</type> argument.
</simpara>
<simpara>
See also <link linkend="language.types.type-juggling">Type Juggling</link>.
</simpara>
<simpara>
To explicitly convert a value to <type>boolean</type>, use either the
<literal>(bool)</literal> or the <literal>(boolean)</literal> cast. However,
in most cases you do not need to use the cast, since a value will be
automatically converted if an operator, function or control structure
requires a <type>boolean</type> argument.
</simpara>
<simpara>
See also <link linkend="language.types.type-juggling">Type Juggling</link>.
</simpara>
<para>
When converting to <type>boolean</type>, the following values are considered
&false;:
<itemizedlist>
<listitem>
<simpara>
the <link linkend="language.types.boolean">boolean</link> &false; itself
</simpara>
</listitem>
<listitem>
<simpara>
the <link linkend="language.types.integer">integer</link> 0 (zero)
</simpara>
</listitem>
<listitem>
<simpara>
the <link linkend="language.types.float">float</link> 0.0 (zero)
</simpara>
</listitem>
<listitem>
<simpara>
the empty <link linkend="language.types.string">string</link>, and the
<link linkend="language.types.string">string</link> "0"
</simpara>
</listitem>
<listitem>
<simpara>
an <link linkend="language.types.array">array</link> with zero elements
</simpara>
</listitem>
<listitem>
<simpara>
an <link linkend="language.types.object">object</link> with zero member
variables (PHP 4 only)
</simpara>
</listitem>
<listitem>
<simpara>
the special type <link linkend="language.types.null">NULL</link>
(including unset variables)
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="ref.simplexml">SimpleXML</link> objects created from empty
tags
</simpara>
</listitem>
</itemizedlist>
<para>
When converting to <type>boolean</type>, the following values are considered
&false;:
</para>
<itemizedlist>
<listitem>
<simpara>
the <link linkend="language.types.boolean">boolean</link> &false; itself
</simpara>
</listitem>
<listitem>
<simpara>
the <link linkend="language.types.integer">integer</link> 0 (zero)
</simpara>
</listitem>
<listitem>
<simpara>
the <link linkend="language.types.float">float</link> 0.0 (zero)
</simpara>
</listitem>
<listitem>
<simpara>
the empty <link linkend="language.types.string">string</link>, and the
<link linkend="language.types.string">string</link> "0"
</simpara>
</listitem>
<listitem>
<simpara>
an <link linkend="language.types.array">array</link> with zero elements
</simpara>
</listitem>
<listitem>
<simpara>
an <link linkend="language.types.object">object</link> with zero member
variables (PHP 4 only)
</simpara>
</listitem>
<listitem>
<simpara>
the special type <link linkend="language.types.null">NULL</link> (including
unset variables)
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="ref.simplexml">SimpleXML</link> objects created from empty
tags
</simpara>
</listitem>
</itemizedlist>
Every other value is considered &true; (including any
<link linkend="language.types.resource">resource</link>).
<warning>
<simpara>
<literal>-1</literal> is considered &true;, like any other non-zero
(whether negative or positive) number!
</simpara>
</warning>
<informalexample>
<programlisting role="php">
<para>
Every other value is considered &true; (including any
<link linkend="language.types.resource">resource</link>).
</para>
<warning>
<simpara>
<literal>-1</literal> is considered &true;, like any other non-zero
(whether negative or positive) number!
</simpara>
</warning>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
var_dump((bool) ""); // bool(false)
@ -146,10 +156,10 @@ var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
?>
]]>
</programlisting>
</informalexample>
</para>
</sect2>
</programlisting>
</informalexample>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file

View file

@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.float">
<title>Floating point numbers</title>
<para>
Floating point numbers (AKA "floats", "doubles" or "real numbers") can be
specified using any of the following syntaxes:
specified using any of the following syntaxes:
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = 1.234;
@ -16,24 +17,27 @@ $b = 1.2e3;
$c = 7E-10;
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<para>
Formally:
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
LNUM [0-9]+
DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
The size of a float 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>
The size of a float 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 xml:id="warn.float-precision">
@ -55,6 +59,7 @@ EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})
<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

View file

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.integer">
<title>Integers</title>
<simpara>
An <type>integer</type> is a number of the set
An <type>integer</type> is a number of the set
Z = {..., -2, -1, 0, 1, 2, ...}.
</simpara>
@ -27,10 +27,11 @@
If you use the octal notation, you must precede the number with a
<literal>0</literal> (zero), to use hexadecimal notation precede the number
with <literal>0x</literal>.
</para>
<example>
<title>Integer literals</title>
<programlisting role="php">
<example>
<title>Integer literals</title>
<programlisting role="php">
<![CDATA[
<?php
$a = 1234; // decimal number
@ -39,13 +40,15 @@ $a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
?>
]]>
</programlisting>
</example>
</programlisting>
</example>
<para>
Formally the possible structure for integer literals is:
<informalexample>
<programlisting>
</para>
<informalexample>
<programlisting>
<![CDATA[
decimal : [1-9][0-9]*
| 0
@ -58,9 +61,10 @@ integer : [+-]?decimal
| [+-]?hexadecimal
| [+-]?octal
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<para>
The size of an integer is platform-dependent, although a maximum value of
about two billion is the usual value (that's 32 bits signed). PHP does not
support unsigned integers. Integer size can be determined from
@ -72,18 +76,18 @@ integer : [+-]?decimal
<para>
If an invalid digit is passed to octal integer (i.e. 8 or 9), the rest of
the number is ignored.
</para>
<example>
<title>Octal weirdness</title>
<programlisting role="php">
<example>
<title>Octal weirdness</title>
<programlisting role="php">
<![CDATA[
<?php
var_dump(01090); // 010 octal = 8 decimal
?>
]]>
</programlisting>
</example>
</para>
</programlisting>
</example>
</warning>
</sect2>
@ -95,9 +99,10 @@ var_dump(01090); // 010 octal = 8 decimal
it will be interpreted as a <type>float</type> instead. Also, if you perform
an operation that results in a number beyond the bounds of the
<type>integer</type> type, a <type>float</type> will be returned instead.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$large_number = 2147483647;
@ -122,32 +127,32 @@ var_dump($large_number);
// output: float(50000000000)
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</informalexample>
<warning>
<simpara>
Unfortunately, there was a bug in PHP so that this does not always work
correctly when there are negative numbers involved. For example: when you
do <literal>-50000 * $million</literal>, the result will be
<literal>-429496728</literal>. However, when both operands are positive
there is no problem.
</simpara>
<simpara>
This is solved in PHP 4.1.0.
</simpara>
</warning>
</para>
<warning>
<simpara>
Unfortunately, there was a bug in PHP so that this does not always work
correctly when there are negative numbers involved. For example: when you do
<literal>-50000 * $million</literal>, the result will be
<literal>-429496728</literal>. However, when both operands are positive
there is no problem.
</simpara>
<simpara>
This is solved in PHP 4.1.0.
</simpara>
</warning>
<para>
There is no integer division operator in PHP. <literal>1/2</literal> yields
the <type>float</type> <literal>0.5</literal>. You can cast the value to an
integer to always round it downwards, or you can use the
<function>round</function> function.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
var_dump(25/7); // float(3.5714285714286)
@ -155,98 +160,97 @@ var_dump((int) (25/7)); // int(3)
var_dump(round(25/7)); // float(4)
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
</sect2>
<sect2 xml:id="language.types.integer.casting">
<title>Converting to integer</title>
<simpara>
To explicitly convert a value to <type>integer</type>, use either the
<literal>(int)</literal> or the <literal>(integer)</literal> cast. However,
in most cases you do not need to use the cast, since a value will be
automatically converted if an operator, function or control structure
requires an <type>integer</type> argument. You can also convert a value to
integer with the function <function>intval</function>.
</simpara>
<simpara>
To explicitly convert a value to <type>integer</type>, use either the
<literal>(int)</literal> or the <literal>(integer)</literal> cast. However,
in most cases you do not need to use the cast, since a value will be
automatically converted if an operator, function or control structure
requires an <type>integer</type> argument. You can also convert a value to
integer with the function <function>intval</function>.
</simpara>
<simpara>
See also <link linkend="language.types.type-juggling">type-juggling</link>.
</simpara>
<simpara>
See also <link linkend="language.types.type-juggling">type-juggling</link>.
</simpara>
<sect3 xml:id="language.types.integer.casting.from-boolean">
<title>From <link linkend="language.types.boolean">booleans</link></title>
<sect3 xml:id="language.types.integer.casting.from-boolean">
<title>From <link linkend="language.types.boolean">booleans</link></title>
<simpara>
&false; will yield <literal>0</literal> (zero), and &true; will yield
<literal>1</literal> (one).
</simpara>
</sect3>
<simpara>
&false; will yield <literal>0</literal> (zero), and &true; will yield
<literal>1</literal> (one).
</simpara>
</sect3>
<sect3 xml:id="language.types.integer.casting.from-float">
<title>
From <link linkend="language.types.float">floating point
numbers</link>
</title>
<sect3 xml:id="language.types.integer.casting.from-float">
<title>
From <link linkend="language.types.float">floating point numbers</link>
</title>
<simpara>
When converting from float to integer, the number will be rounded <emphasis>towards zero</emphasis>.
</simpara>
<simpara>
When converting from float to integer, the number will be rounded
<emphasis>towards zero</emphasis>.
</simpara>
<para>
If the float is beyond the boundaries of integer (usually
<literal>+/- 2.15e+9 = 2^31</literal>), the result is undefined, since the
float hasn't got enough precision to give an exact integer result. No
warning, not even a notice will be issued in this case!
</para>
<warning>
<para>
If the float is beyond the boundaries of integer (usually
<literal>+/- 2.15e+9 = 2^31</literal>), the result is undefined, since the
float hasn't got enough precision to give an exact integer result. No
warning, not even a notice will be issued in this case!
Never cast an unknown fraction to <type>integer</type>, as this can
sometimes lead to unexpected results.
</para>
<warning>
<para>
Never cast an unknown fraction to <type>integer</type>, as this can
sometimes lead to unexpected results.
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo (int) ( (0.1+0.7) * 10 ); // echoes 7!
?>
]]>
</programlisting>
</informalexample>
See for more information the <link linkend="warn.float-precision">warning
about float-precision</link>.
</para>
</warning>
</sect3>
<sect3 xml:id="language.types.integer.casting.from-string">
<title>From strings</title>
<simpara>
See <link linkend="language.types.string.conversion">String conversion to
numbers</link>
</simpara>
</sect3>
<sect3 xml:id="language.types.integer.casting.from-other">
<title>From other types</title>
</programlisting>
</informalexample>
<para>
<caution>
<simpara>
Behaviour of converting to integer is undefined for other types.
Currently, the behaviour is the same as if the value was first
<link linkend="language.types.boolean.casting">converted to
boolean</link>. However, do <emphasis>not</emphasis> rely on this
behaviour, as it can change without notice.
</simpara>
</caution>
See for more information the <link linkend="warn.float-precision">warning
about float-precision</link>.
</para>
</sect3>
</warning>
</sect3>
<sect3 xml:id="language.types.integer.casting.from-string">
<title>From strings</title>
<simpara>
See <link linkend="language.types.string.conversion">String conversion to
numbers</link>
</simpara>
</sect3>
<sect3 xml:id="language.types.integer.casting.from-other">
<title>From other types</title>
<caution>
<simpara>
Behaviour of converting to integer is undefined for other types. Currently,
the behaviour is the same as if the value was first
<link linkend="language.types.boolean.casting">converted to boolean</link>.
However, do <emphasis>not</emphasis> rely on this behaviour, as it can
change without notice.
</simpara>
</caution>
</sect3>
</sect2>
</sect1>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.null">
<title>NULL</title>
@ -16,25 +16,25 @@
<para>
A variable is considered to be &null; if
<itemizedlist>
<listitem>
<para>
it has been assigned the constant &null;.
</para>
</listitem>
<listitem>
<para>
it has not been set to any value yet.
</para>
</listitem>
<listitem>
<para>
it has been <function>unset</function>.
</para>
</listitem>
</itemizedlist>
</para>
<itemizedlist>
<listitem>
<para>
it has been assigned the constant &null;.
</para>
</listitem>
<listitem>
<para>
it has not been set to any value yet.
</para>
</listitem>
<listitem>
<para>
it has been <function>unset</function>.
</para>
</listitem>
</itemizedlist>
<sect2 xml:id="language.types.null.syntax">
<title>Syntax</title>
@ -42,17 +42,17 @@
<para>
There is only one value of type &null;, and that is the case-insensitive
keyword &null;.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$var = NULL;
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<para>
See also <function>is_null</function> and <function>unset</function>.

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.object">
<title>Objects</title>
@ -9,9 +9,10 @@
<para>
To initialize an object, you use the <literal>new</literal> statement to
instantiate the object to a variable.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
class foo
@ -26,14 +27,14 @@ $bar = new foo;
$bar->do_foo();
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<simpara>
For a full discussion, please read the section
<link linkend="language.oop">Classes and Objects</link>.
</simpara>
</sect2>
<sect2 xml:id="language.types.object.casting">
@ -47,18 +48,18 @@ $bar->do_foo();
properties named by array keys and with corresponding values. For any other
value, a member variable named <literal>scalar</literal> will contain the
value.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$obj = (object) 'ciao';
echo $obj->scalar; // outputs 'ciao'
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
</sect2>
</sect1>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.1 $ -->
<!-- $Revision: 1.2 $ -->
<sect1 xml:id="language.pseudo-types">
<title>Pseudo-types and variables used in this documentation</title>
@ -15,6 +15,7 @@
<function>gettype</function> for example will accept all PHP types, while
<function>str_replace</function> will accept strings and arrays.
</para>
</sect2>
<sect2 xml:id="language.types.number">
@ -24,6 +25,7 @@
<literal>number</literal> indicates that a parameter can be either
<type>integer</type> or <type>float</type>.
</para>
</sect2>
<sect2 xml:id="language.types.callback">
@ -62,12 +64,11 @@
can be used to create an anonymous callback function.
</para>
<para>
<example>
<title>
Callback function examples
</title>
<programlisting role="php">
<example>
<title>
Callback function examples
</title>
<programlisting role="php">
<![CDATA[
<?php
@ -112,9 +113,8 @@ class B extends A {
call_user_func(array('B', 'parent::who')); // A
?>
]]>
</programlisting>
</example>
</para>
</programlisting>
</example>
<note>
<simpara>
@ -134,6 +134,7 @@ call_user_func(array('B', 'parent::who')); // A
useless. <literal>void</literal> in parameters list means that the function
doesn't accept any parameters.
</para>
</sect2>
<sect2 xml:id="language.types.dotdotdot">

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.resource">
<title>Resource</title>
@ -39,16 +39,16 @@
Java). When this is the case, all resources that were in use for this resource
are made free by the garbage collector. For this reason, it is rarely ever
necessary to free the memory manually by using some free_result function.
<note>
<simpara>
Persistent database links are special, they are <emphasis>not</emphasis>
destroyed by the garbage collector. See also the section about
<link linkend="features.persistent-connections">persistent
connections</link>.
</simpara>
</note>
</para>
<note>
<simpara>
Persistent database links are special, they are <emphasis>not</emphasis>
destroyed by the garbage collector. See also the section about
<link linkend="features.persistent-connections">persistent
connections</link>.
</simpara>
</note>
</sect2>
</sect1>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.string">
<title>Strings</title>
@ -24,26 +24,26 @@
<para>
A string literal can be specified in three different ways.
<itemizedlist>
<listitem>
<simpara>
<link linkend="language.types.string.syntax.single">single quoted</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.string.syntax.double">double quoted</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.string.syntax.heredoc">heredoc syntax</link>
</simpara>
</listitem>
</itemizedlist>
</para>
<itemizedlist>
<listitem>
<simpara>
<link linkend="language.types.string.syntax.single">single quoted</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.string.syntax.double">double quoted</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.string.syntax.heredoc">heredoc syntax</link>
</simpara>
</listitem>
</itemizedlist>
<sect3 xml:id="language.types.string.syntax.single">
<title>Single quoted</title>
@ -59,25 +59,26 @@
you need to double it. Note that if you try to escape any other character,
the backslash will also be printed! So usually there is no need to escape
the backslash itself.
</para>
<note>
<simpara>
In PHP 3, a warning will be issued at the <literal>E_NOTICE</literal>
level when this happens.
</simpara>
</note>
<note>
<simpara>
In PHP 3, a warning will be issued at the <literal>E_NOTICE</literal> level
when this happens.
</simpara>
</note>
<note>
<simpara>
Unlike the two other syntaxes,
<link linkend="language.variables">variables</link> and escape sequences
for special characters will <emphasis>not</emphasis> be expanded when they
occur in single quoted strings.
</simpara>
</note>
<note>
<simpara>
Unlike the two other syntaxes,
<link linkend="language.variables">variables</link> and escape sequences
for special characters will <emphasis>not</emphasis> be expanded when they
occur in single quoted strings.
</simpara>
</note>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo 'this is a simple string';
@ -102,9 +103,8 @@ echo 'This will not expand: \n a newline';
echo 'Variables do not $expand $either';
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
</sect3>
@ -112,9 +112,8 @@ echo 'Variables do not $expand $either';
<title>Double quoted</title>
<para>
If the string is enclosed in double-quotes ("),
PHP understands more escape sequences for special
characters:
If the string is enclosed in double-quotes ("), PHP understands more escape
sequences for special characters:
</para>
<table>
@ -233,10 +232,11 @@ echo 'Variables do not $expand $either';
<para>
It is not allowed to use heredoc syntax in initializing class members. Use
other string syntaxes instead.
</para>
<example>
<title>Invalid example</title>
<programlisting role="php">
<example>
<title>Invalid example</title>
<programlisting role="php">
<![CDATA[
<?php
class foo {
@ -246,9 +246,8 @@ EOT;
}
?>
]]>
</programlisting>
</example>
</para>
</programlisting>
</example>
</warning>
<para>
@ -257,10 +256,11 @@ EOT;
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 heredoc as with strings.
</para>
<example>
<title>Heredoc string quoting example</title>
<programlisting role="php">
<example>
<title>Heredoc string quoting example</title>
<programlisting role="php">
<![CDATA[
<?php
$str = <<<EOD
@ -292,9 +292,8 @@ This should print a capital 'A': \x41
EOT;
?>
]]>
</programlisting>
</example>
</para>
</programlisting>
</example>
<note>
<para>
@ -418,18 +417,19 @@ echo "This square is $square->{width}00 centimeters broad.";
<simpara>
This isn't called complex because the syntax is complex, but because you
can include complex expressions this way.
</simpara>
</simpara>
<simpara>
In fact, you can include any value that is in the namespace in strings with
this syntax. You simply write the expression the same way as you would
outside the string, and then include it in { and }. Since you can't escape
'{', this syntax will only be recognised when the $ is immediately following
the {. (Use "{\$" to get a literal "{$"). Some examples to make it clear:
</simpara>
<simpara>
In fact, you can include any value that is in the namespace in strings with
this syntax. You simply write the expression the same way as you would
outside the string, and then include it in { and }. Since you can't escape
'{', this syntax will only be recognised when the $ is immediately
following the {. (Use "{\$" to get a literal "{$"). Some examples to make
it clear:
</simpara>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Let's show all errors
@ -495,6 +495,7 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips";
variable parsing.
</para>
</note>
</sect4>
</sect3>
@ -516,11 +517,9 @@ echo "I'd like to have another {${ strrev('reeb') }}, hips";
</simpara>
</note>
<para>
<example>
<title>Some string examples</title>
<programlisting role="php">
<example>
<title>Some string examples</title>
<programlisting role="php">
<![CDATA[
<?php
// Get the first character of a string
@ -543,9 +542,8 @@ $third = $str{2};
?>
]]>
</programlisting>
</example>
</para>
</programlisting>
</example>
<note>
<para>
@ -553,8 +551,8 @@ $third = $str{2};
variables of other type silently returns &null;.
</para>
</note>
</sect3>
</sect3>
</sect2><!-- end syntax -->
<sect2 xml:id="language.types.string.useful-funcs">
@ -720,17 +718,17 @@ $foo = "10.0 pigs " + 1.0; // $foo is float (11)
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:
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo "\$foo==$foo; type is " . gettype ($foo) . "<br />\n";
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<para>
Do not expect to get the code of one character by converting it to integer

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 1.2 $ -->
<!-- $Revision: 1.3 $ -->
<sect1 xml:id="language.types.type-juggling">
<title>Type Juggling</title>
@ -19,9 +19,10 @@
integers, 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.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = "0"; // $foo is string (ASCII 48)
@ -50,9 +51,8 @@ examples:
- -'abc' = 'abc'
-->
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<simpara>
If the last two examples above seem odd, see
@ -79,9 +79,10 @@ examples:
Also, because PHP supports indexing into strings via offsets using the same
syntax as array indexing, the following example holds true for all PHP
versions:
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = 'car'; // $a is a string
@ -89,9 +90,8 @@ $a[0] = 'b'; // $a is still a string
echo $a; // bar
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<para>
See the section titled <link linkend="language.types.string.substr">String
@ -105,47 +105,47 @@ echo $a; // bar
<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.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is a boolean
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<para>
The casts allowed are:
<itemizedlist>
<listitem>
<simpara>(int), (integer) - cast to integer</simpara>
</listitem>
<listitem>
<simpara>(bool), (boolean) - cast to boolean</simpara>
</listitem>
<listitem>
<simpara>(float), (double), (real) - cast to float</simpara>
</listitem>
<listitem>
<simpara>(string) - cast to string</simpara>
</listitem>
<listitem>
<simpara>(binary) - cast to binary string (PHP 6)</simpara>
</listitem>
<listitem>
<simpara>(array) - cast to array</simpara>
</listitem>
<listitem>
<simpara>(object) - cast to object</simpara>
</listitem>
</itemizedlist>
</para>
<itemizedlist>
<listitem>
<simpara>(int), (integer) - cast to integer</simpara>
</listitem>
<listitem>
<simpara>(bool), (boolean) - cast to boolean</simpara>
</listitem>
<listitem>
<simpara>(float), (double), (real) - cast to float</simpara>
</listitem>
<listitem>
<simpara>(string) - cast to string</simpara>
</listitem>
<listitem>
<simpara>(binary) - cast to binary string (PHP 6)</simpara>
</listitem>
<listitem>
<simpara>(array) - cast to array</simpara>
</listitem>
<listitem>
<simpara>(object) - cast to object</simpara>
</listitem>
</itemizedlist>
<para>
(binary) casting and b prefix forward support was added in PHP 5.2.1
</para>
@ -153,39 +153,40 @@ $bar = (boolean) $foo; // $bar is a boolean
<para>
Note that tabs and spaces are allowed inside the parentheses, so the
following are functionally equivalent:
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = (int) $bar;
$foo = ( int ) $bar;
?>
]]>
</programlisting>
</programlisting>
<para>
Casting a literal strings and variables to binary strings:
</para>
<para>
Casting a literal strings and variables to binary strings:
</para>
<programlisting role="php">
<programlisting role="php">
<![CDATA[
<?php
$binary = (binary)$string;
$binary = b"binary string";
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
<note>
<para>
Instead of casting a variable to string, you can also enclose
the variable in double quotes.
Instead of casting a variable to string, you can also enclose the variable
in double quotes.
</para>
<informalexample>
<programlisting role="php">
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = 10; // $foo is an integer
@ -198,57 +199,65 @@ if ($fst === $str) {
}
?>
]]>
</programlisting>
</informalexample>
</para>
</programlisting>
</informalexample>
</note>
<para>
It may not be obvious exactly what will happen when casting between certain
types. For more info, see these sections:
<itemizedlist>
<listitem>
<simpara><link linkend="language.types.boolean.casting">Converting to
boolean</link></simpara>
</listitem>
<listitem>
<simpara><link linkend="language.types.integer.casting">Converting to
integer</link></simpara>
</listitem>
<listitem>
<simpara><link linkend="language.types.float.casting">Converting to
float</link></simpara>
</listitem>
<listitem>
<simpara><link linkend="language.types.string.casting">Converting to
string</link></simpara>
</listitem>
<listitem>
<simpara><link linkend="language.types.array.casting">Converting to
array</link></simpara>
</listitem>
<listitem>
<simpara><link linkend="language.types.object.casting">Converting to
object</link></simpara>
</listitem>
<listitem>
<simpara><link linkend="language.types.resource.casting">Converting to
resource</link></simpara>
</listitem>
<!-- don't exist yet
<listitem>
<simpara><link linkend="language.types.null.casting">Converting to
&null;</link></simpara>
</listitem>
-->
<listitem>
<simpara>
<link linkend="types.comparisons">The type comparison tables</link>
</simpara>
</listitem>
</itemizedlist>
</para>
<itemizedlist>
<listitem>
<simpara>
<link linkend="language.types.boolean.casting">Converting to boolean</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.integer.casting">Converting to integer</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.float.casting">Converting to float</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.string.casting">Converting to string</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.array.casting">Converting to array</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.object.casting">Converting to object</link>
</simpara>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.resource.casting">Converting to
resource</link>
</simpara>
</listitem>
<!-- don't exist yet
<listitem>
<simpara>
<link linkend="language.types.null.casting">Converting to &null;</link>
</simpara>
</listitem>
-->
<listitem>
<simpara>
<link linkend="types.comparisons">The type comparison tables</link>
</simpara>
</listitem>
</itemizedlist>
</sect2>
</sect1>