mirror of
https://github.com/php/doc-zh.git
synced 2026-03-23 22:52:08 +01:00
140 lines
4.5 KiB
XML
Executable File
140 lines
4.5 KiB
XML
Executable File
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: e587d0655e426f97b3fcb431453da5030e743b23 Maintainer: dallas Status: ready -->
|
||
<!-- CREDITS: HonestQiao, Gregory, Haohappy, mowangjuanzi, Luffy -->
|
||
<chapter xml:id="language.types" xmlns="http://docbook.org/ns/docbook" annotations="interactive">
|
||
<title>类型</title>
|
||
|
||
<sect1 xml:id="language.types.intro">
|
||
<title>简介</title>
|
||
|
||
<para>
|
||
PHP 中的每个表达式都属于以下某个内置类型,具体取决于值:
|
||
<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>(浮点数)</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 是动态类型语言,这意味着默认不需要指定变量的类型,因为会在运行时确定。然而,可以通过使用<link
|
||
linkend="language.types.declarations">类型声明</link>对语言的一些方面进行类型静态化。可以在<link
|
||
linkend="language.types.type-system">类型系统</link>页面找到 PHP 类型系统支持的不同类型。
|
||
</para>
|
||
|
||
<para>
|
||
类型限制了可以对其执行的操作。然而,如果使用的表达式/变量不支持该操作,PHP 将尝试将该值<link
|
||
linkend="language.types.type-juggling">类型转换</link>为操作支持的类型。此过程取决于使用该值的上下文。更多信息参阅<link
|
||
linkend="language.types.type-juggling">类型转换</link>。
|
||
</para>
|
||
|
||
<tip>
|
||
<simpara>
|
||
<link linkend="types.comparisons">类型比较表</link>也很有用,因为存在不同类型之间的值的各种比较示例。
|
||
</simpara>
|
||
</tip>
|
||
|
||
<note>
|
||
<simpara>
|
||
使用<link linkend="language.types.typecasting">类型转换</link>,强制将表达式的值转换为某种类型。还可以使用
|
||
<function>settype</function> 函数就地对变量进行类型转换。
|
||
</simpara>
|
||
</note>
|
||
|
||
<para>
|
||
使用 <function>var_dump</function> 函数检查<link linkend="language.expressions">表达式</link>的值和类型。使用
|
||
<function>get_debug_type</function> 检索<link linkend="language.expressions">表达式</link>的值和类型。使用
|
||
<literal>is_<replaceable>type</replaceable></literal> 检查表达式是否属于某种类型。
|
||
|
||
<example>
|
||
<title>不同类型</title>
|
||
<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";
|
||
|
||
// 如果是整型,就加上 4
|
||
if (is_int($an_int)) {
|
||
$an_int += 4;
|
||
}
|
||
var_dump($an_int);
|
||
|
||
// 如果 $a_bool 是字符串,就打印出来
|
||
if (is_string($a_bool)) {
|
||
echo "String: $a_bool";
|
||
}
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.8;
|
||
<screen>
|
||
<![CDATA[
|
||
bool
|
||
string
|
||
int(16)
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<note>
|
||
<simpara>
|
||
PHP 8.0.0 之前,<function>get_debug_type</function> 无效,可以使用 <function>gettype</function>
|
||
函数代替。但是没有使用规范的类型名称。
|
||
</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.singleton;
|
||
&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
|
||
-->
|