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
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="language.types" xmlns="http://docbook.org/ns/docbook">
<title>Types</title>
<sect1 xml:id="language.types.intro">
<title>Introduction</title>
<para>
Every single expression in PHP has one of the following
built-in types depending on its value:
<itemizedlist>
<listitem><simpara><type>null</type></simpara></listitem>
<listitem><simpara><type>bool</type></simpara></listitem>
<listitem><simpara><type>int</type></simpara></listitem>
<listitem><simpara><type>float</type> (floating-point number)</simpara></listitem>
<listitem><simpara><type>string</type></simpara></listitem>
<listitem><simpara><type>array</type></simpara></listitem>
<listitem><simpara><type>object</type></simpara></listitem>
<listitem><simpara><type>callable</type></simpara></listitem>
<listitem><simpara><type>resource</type></simpara></listitem>
</itemizedlist>
</para>
<para>
PHP is a dynamically typed language, which means that by default there is
no need to specify the type of a variable, as this will be determined at
runtime. However, it is possible to statically type some aspect of the
language via the use of
<link linkend="language.types.declarations">type declarations</link>.
Different types that are supported by PHP's type system can be found at the
<link linkend="language.types.type-system">type system</link> page.
</para>
<para>
Types restrict the kind of operations that can be performed on them.
However, if an expression/variable is used in an operation which
its type does not support, PHP will attempt to
<link linkend="language.types.type-juggling">type juggle</link>
the value into a type that supports the operation.
This process depends on the context in which the value is used.
For more information, see the section on
<link linkend="language.types.type-juggling">Type Juggling</link>.
</para>
<tip>
<simpara>
<link linkend="types.comparisons">The type comparison tables</link>
may also be useful, as various examples of comparison between values of
different types are present.
</simpara>
</tip>
<note>
<simpara>
It is possible to force an expression to be evaluated to a certain type by
using a <link linkend="language.types.typecasting">type cast</link>.
A variable can also be type cast in-place by using the
<function>settype</function> function on it.
</simpara>
</note>
<para>
To check the value and type of an
<link linkend="language.expressions">expression</link>,
use the <function>var_dump</function> function.
To retrieve the type of an
<link linkend="language.expressions">expression</link>,
use the <function>get_debug_type</function> function.
However, to check if an expression is of a certain type use the
<!-- TODO When PhD support is there: <function>is_<replaceable>type</replaceable></function> -->
<literal>is_<replaceable>type</replaceable></literal> functions instead.
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a_bool = true; // a bool
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an int
echo get_debug_type($a_bool), "\n";
echo get_debug_type($a_str), "\n";
// If this is an integer, increment it by four
if (is_int($an_int)) {
$an_int += 4;
}
var_dump($an_int);
// If $a_bool is a string, print it out
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?>
]]>
</programlisting>
&example.outputs.8;
<screen>
<![CDATA[
bool
string
int(16)
]]>
</screen>
</informalexample>
</para>
<note>
<simpara>
Prior to PHP 8.0.0, where the <function>get_debug_type</function> is not
available, the <function>gettype</function> function can be used instead.
However, it doesn't use the canonical type names.
</simpara>
</note>
</sect1>
&language.types.type-system;
&language.types.null;
&language.types.boolean;
&language.types.integer;
&language.types.float;
&language.types.string;
&language.types.numeric-strings;
&language.types.array;
&language.types.object;
&language.types.enumerations;
&language.types.resource;
&language.types.callable;
&language.types.mixed;
&language.types.void;
&language.types.never;
&language.types.relative-class-types;
&language.types.value;
&language.types.iterable;
&language.types.declarations;
&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
-->
|