2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:17:58 +00:00
|
|
|
<!-- $Revision$ -->
|
2008-02-14 20:50:33 +00:00
|
|
|
<chapter xml:id="language.types" xmlns="http://docbook.org/ns/docbook">
|
|
|
|
<title>Types</title>
|
1999-06-20 02:25:34 +00:00
|
|
|
|
2008-02-14 20:50:33 +00:00
|
|
|
<sect1 xml:id="language.types.intro">
|
2001-05-16 15:22:22 +00:00
|
|
|
<title>Introduction</title>
|
|
|
|
|
|
|
|
<simpara>
|
2017-05-25 08:04:46 +00:00
|
|
|
PHP supports ten primitive types.
|
2001-05-16 15:22:22 +00:00
|
|
|
</simpara>
|
|
|
|
|
2000-03-20 23:32:11 +00:00
|
|
|
<para>
|
2002-11-04 15:45:11 +00:00
|
|
|
Four scalar types:
|
2008-02-18 22:32:54 +00:00
|
|
|
</para>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<itemizedlist>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2020-11-02 15:39:04 +00:00
|
|
|
<type>bool</type>
|
2008-02-18 22:32:54 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2020-11-02 15:39:04 +00:00
|
|
|
<type>int</type>
|
2008-02-18 22:32:54 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
2008-02-21 18:43:17 +00:00
|
|
|
<type>float</type> (floating-point number, aka <type>double</type>)
|
2008-02-18 22:32:54 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<type>string</type>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
</itemizedlist>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<para>
|
2017-04-08 17:03:13 +00:00
|
|
|
Four compound types:
|
2008-02-18 22:32:54 +00:00
|
|
|
</para>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<itemizedlist>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<type>array</type>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<type>object</type>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2016-08-05 08:23:18 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<type>callable</type>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
|
2017-04-08 17:03:13 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<type>iterable</type>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
</itemizedlist>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<para>
|
2012-11-01 21:54:27 +00:00
|
|
|
And finally two special types:
|
2008-02-18 22:32:54 +00:00
|
|
|
</para>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<itemizedlist>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<type>resource</type>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<type>NULL</type>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2001-05-16 15:22:22 +00:00
|
|
|
|
2008-02-18 22:32:54 +00:00
|
|
|
</itemizedlist>
|
2002-10-26 06:58:16 +00:00
|
|
|
|
2007-09-10 20:02:10 +00:00
|
|
|
<simpara>
|
2008-02-21 18:43:17 +00:00
|
|
|
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.
|
2007-09-10 20:02:10 +00:00
|
|
|
</simpara>
|
2001-05-19 20:45:31 +00:00
|
|
|
|
1999-12-02 23:20:50 +00:00
|
|
|
<simpara>
|
2008-02-21 18:43:17 +00:00
|
|
|
The type of a variable is not usually set by the programmer; rather, it is
|
2008-02-18 22:32:54 +00:00
|
|
|
decided at runtime by PHP depending on the context in which that variable is
|
|
|
|
used.
|
1999-11-16 02:23:40 +00:00
|
|
|
</simpara>
|
2008-02-14 20:50:33 +00:00
|
|
|
|
2002-02-08 14:15:22 +00:00
|
|
|
<note>
|
2001-10-28 18:47:39 +00:00
|
|
|
<simpara>
|
2008-02-21 18:43:17 +00:00
|
|
|
To check the type and value of an
|
|
|
|
<link linkend="language.expressions">expression</link>, use the
|
|
|
|
<function>var_dump</function> function.
|
2001-10-28 18:47:39 +00:00
|
|
|
</simpara>
|
2008-02-14 20:50:33 +00:00
|
|
|
|
2002-11-04 15:45:11 +00:00
|
|
|
<para>
|
2008-02-21 18:43:17 +00:00
|
|
|
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
|
2002-11-04 15:45:11 +00:00
|
|
|
<literal>is_<replaceable>type</replaceable></literal> functions. Some
|
|
|
|
examples:
|
2008-02-18 22:32:54 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<informalexample>
|
|
|
|
<programlisting role="php">
|
2002-11-04 15:45:11 +00:00
|
|
|
<![CDATA[
|
2003-01-21 07:17:32 +00:00
|
|
|
<?php
|
2006-03-09 06:39:45 +00:00
|
|
|
$a_bool = TRUE; // a boolean
|
|
|
|
$a_str = "foo"; // a string
|
|
|
|
$a_str2 = 'foo'; // a string
|
|
|
|
$an_int = 12; // an integer
|
2002-11-04 15:45:11 +00:00
|
|
|
|
2006-03-09 06:39:45 +00:00
|
|
|
echo gettype($a_bool); // prints out: boolean
|
|
|
|
echo gettype($a_str); // prints out: string
|
2002-11-04 15:45:11 +00:00
|
|
|
|
|
|
|
// If this is an integer, increment it by four
|
2006-03-09 06:39:45 +00:00
|
|
|
if (is_int($an_int)) {
|
|
|
|
$an_int += 4;
|
2002-11-04 15:45:11 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 11:59:44 +00:00
|
|
|
// If $a_bool is a string, print it out
|
2002-11-04 15:45:11 +00:00
|
|
|
// (does not print out anything)
|
2006-03-09 06:39:45 +00:00
|
|
|
if (is_string($a_bool)) {
|
|
|
|
echo "String: $a_bool";
|
2002-11-04 15:45:11 +00:00
|
|
|
}
|
2003-01-21 07:17:32 +00:00
|
|
|
?>
|
2002-11-04 15:45:11 +00:00
|
|
|
]]>
|
2008-02-18 22:32:54 +00:00
|
|
|
</programlisting>
|
|
|
|
</informalexample>
|
2002-02-08 14:15:22 +00:00
|
|
|
</note>
|
2008-02-14 20:50:33 +00:00
|
|
|
|
1999-12-02 23:20:50 +00:00
|
|
|
<simpara>
|
2008-02-21 18:43:17 +00:00
|
|
|
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.
|
1999-11-16 02:23:40 +00:00
|
|
|
</simpara>
|
2008-02-14 20:50:33 +00:00
|
|
|
|
1999-12-02 23:20:50 +00:00
|
|
|
<simpara>
|
2002-11-04 15:45:11 +00:00
|
|
|
Note that a variable may be evaluated with different values in certain
|
2008-02-18 22:32:54 +00:00
|
|
|
situations, depending on what type it is at the time. For more information,
|
|
|
|
see the section on <link linkend="language.types.type-juggling">Type
|
2008-02-21 18:43:17 +00:00
|
|
|
Juggling</link>. <link linkend="types.comparisons">The type comparison
|
|
|
|
tables</link> may also be useful, as they show examples of various
|
|
|
|
type-related comparisons.
|
1999-12-02 23:20:50 +00:00
|
|
|
</simpara>
|
2002-11-04 15:45:11 +00:00
|
|
|
</sect1>
|
|
|
|
|
2008-02-14 19:57:17 +00:00
|
|
|
&language.types.boolean;
|
|
|
|
&language.types.integer;
|
|
|
|
&language.types.float;
|
|
|
|
&language.types.string;
|
2020-11-27 15:56:30 +00:00
|
|
|
&language.types.numeric-strings;
|
2008-02-14 19:57:17 +00:00
|
|
|
&language.types.array;
|
2017-04-08 17:03:13 +00:00
|
|
|
&language.types.iterable;
|
2008-02-14 19:57:17 +00:00
|
|
|
&language.types.object;
|
2021-10-28 23:07:57 +00:00
|
|
|
&language.types.enumerations;
|
2008-02-14 19:57:17 +00:00
|
|
|
&language.types.resource;
|
|
|
|
&language.types.null;
|
2012-03-12 05:55:59 +00:00
|
|
|
&language.types.callable;
|
2020-11-12 18:39:19 +00:00
|
|
|
&language.types.declarations;
|
2008-02-14 19:57:17 +00:00
|
|
|
&language.types.type-juggling;
|
2002-10-26 06:58:16 +00:00
|
|
|
|
2008-02-14 20:50:33 +00:00
|
|
|
</chapter>
|
1999-06-20 02:25:34 +00:00
|
|
|
|
2000-03-20 23:32:11 +00:00
|
|
|
<!-- 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
|
2001-09-21 22:47:49 +00:00
|
|
|
sgml-indent-step:1
|
2000-03-20 23:32:11 +00:00
|
|
|
sgml-indent-data:t
|
2001-12-12 20:47:43 +00:00
|
|
|
indent-tabs-mode:nil
|
2000-03-20 23:32:11 +00:00
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2000-03-20 23:32:11 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
2001-09-21 22:47:49 +00:00
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
2000-03-20 23:32:11 +00:00
|
|
|
-->
|