mirror of
https://github.com/php/doc-en.git
synced 2026-03-26 16:52:25 +01:00
This commit rewrites the whole type section to (hopefully) be better structured and future proof for further additions to the type system. * Each type now gets their individual page instead of being shoved in the type declaration page. * A type system page is added which describes PHP's type system, regardless if it is possible to declare the type in userland or not. Therefore, the type declaration page only has information related to writing type declarations in userland. * The description of strict_type and the type coercion is moved into the type juggling page. * Remove outdated information in string implementation section * Add paragraph about using non string in string context can throw Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
158 lines
4.7 KiB
XML
158 lines
4.7 KiB
XML
<?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>.
|
|
</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.literal;
|
|
&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
|
|
-->
|