mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 16:38:54 +00:00

As of PHP 8.0.0, the `mixed` type is available as proper type, so we move the respective info out of the pseudo types section, which can be removed now, since it is practically empty. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@351804 c90b9560-bf6c-de11-be94-00142212c4b1
202 lines
4.3 KiB
XML
202 lines
4.3 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>
|
|
|
|
<simpara>
|
|
PHP supports ten primitive types.
|
|
</simpara>
|
|
|
|
<para>
|
|
Four scalar types:
|
|
</para>
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
<simpara>
|
|
<type>bool</type>
|
|
</simpara>
|
|
</listitem>
|
|
|
|
<listitem>
|
|
<simpara>
|
|
<type>int</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>
|
|
Four compound types:
|
|
</para>
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
<simpara>
|
|
<type>array</type>
|
|
</simpara>
|
|
</listitem>
|
|
|
|
<listitem>
|
|
<simpara>
|
|
<type>object</type>
|
|
</simpara>
|
|
</listitem>
|
|
|
|
<listitem>
|
|
<simpara>
|
|
<type>callable</type>
|
|
</simpara>
|
|
</listitem>
|
|
|
|
<listitem>
|
|
<simpara>
|
|
<type>iterable</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>
|
|
|
|
<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 $a_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.numeric-strings;
|
|
&language.types.array;
|
|
&language.types.iterable;
|
|
&language.types.object;
|
|
&language.types.resource;
|
|
&language.types.null;
|
|
&language.types.callable;
|
|
&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
|
|
-->
|