-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathtypes.xml
executable file
·138 lines (127 loc) · 4.48 KB
/
types.xml
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
<?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[
$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
-->