1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297028 $ -->
<chapter xml:id="language.types" xmlns="http://docbook.org/ns/docbook">
<title>Types</title>
<sect1 xml:id="language.types.intro">
<title>Introduction</title>
<simpara>
PHP supports eight primitive types.
</simpara>
<para>
Four scalar types:
</para>
<itemizedlist>
<listitem>
<simpara>
<type>boolean</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>string</type>
</simpara>
</listitem>
</itemizedlist>
<para>
Two compound types:
</para>
<itemizedlist>
<listitem>
<simpara>
<type>array</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>object</type>
</simpara>
</listitem>
</itemizedlist>
<para>
And finally two special types:
</para>
<itemizedlist>
<listitem>
<simpara>
<type>resource</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>NULL</type>
</simpara>
</listitem>
</itemizedlist>
<para>
This manual also introduces some
<link linkend="language.pseudo-types">pseudo-types</link> for readability
reasons:
</para>
<itemizedlist>
<listitem>
<simpara>
<type>mixed</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>number</type>
</simpara>
</listitem>
<listitem>
<simpara>
<type>callback</type>
</simpara>
</listitem>
</itemizedlist>
<para>
And the pseudo-variable <parameter>$...</parameter>.
</para>
<simpara>
Some references to the type "double" may remain in the manual. Consider
double the same as float; the two names exist only for historic reasons.
</simpara>
<simpara>
The type of a variable is not usually 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>
To check the type and value of an
<link linkend="language.expressions">expression</link>, use the
<function>var_dump</function> function.
</simpara>
<para>
To get a human-readable representation of a type for debugging, use the
<function>gettype</function> function. To check for a certain type, do
<emphasis>not</emphasis> use <function>gettype</function>, but rather the
<literal>is_<replaceable>type</replaceable></literal> functions. Some
examples:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer
echo gettype($a_bool); // prints out: boolean
echo gettype($a_str); // prints out: string
// If this is an integer, increment it by four
if (is_int($an_int)) {
$an_int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?>
]]>
</programlisting>
</informalexample>
</note>
<simpara>
To forcibly convert a variable to a certain type, 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>. <link linkend="types.comparisons">The type comparison
tables</link> may also be useful, as they show examples of various
type-related comparisons.
</simpara>
</sect1>
&language.types.boolean;
&language.types.integer;
&language.types.float;
&language.types.string;
&language.types.array;
&language.types.object;
&language.types.resource;
&language.types.null;
&language.types.pseudo-types;
&language.types.type-juggling;
</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
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|