forked from php/doc-ja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.xml
158 lines (146 loc) · 5 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 5fccbe5195820cd9ec0045674312ec567ef5f6ed Maintainer: mumumu Status: ready -->
<chapter xml:id="language.types" xmlns="http://docbook.org/ns/docbook">
<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 は、動的に型付けを行う言語です。
これは、PHP が実行時に型を決定するため、
デフォルトでは変数の型を指定する必要がないということです。
しかし、
<link linkend="language.types.declarations">型宣言</link> を使うことで、
その一部に静的に型を指定することができます。
</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>
<link linkend="language.expressions">式</link>の型と値を知りたい場合は、
<function>var_dump</function> 関数を使用してください。
<link linkend="language.expressions">式</link> の型を知りたい場合は、
<function>get_debug_type</function> を使用してください。
式がある型であるかどうかをチェックするには
<!-- TODO When PhD support is there: <function>is_<replaceable>type</replaceable></function> -->
<literal>is_<replaceable>type</replaceable></literal>
関数を代わりに使用してください。
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a_bool = true; // bool
$a_str = "foo"; // string
$a_str2 = 'foo'; // string
$an_int = 12; // 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 が文字列であれば, それをprintする
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?>
]]>
</programlisting>
&example.outputs.8;
<screen>
<![CDATA[
bool
string
int(16)
]]>
</screen>
</informalexample>
</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.value;
&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
-->