Skip to content

Commit 2aaaf19

Browse files
GirgiasTimWolla
andauthored
PHP 8.4: exit() is now a function (#4070)
Co-authored-by: Tim Düsterhus <tim@bastelstu.be>
1 parent 82142fe commit 2aaaf19

File tree

2 files changed

+156
-98
lines changed

2 files changed

+156
-98
lines changed

reference/misc/functions/die.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
<refentry xml:id="function.die" xmlns="http://docbook.org/ns/docbook">
44
<refnamediv>
55
<refname>die</refname>
6-
<refpurpose>Equivalent to <literal>exit</literal></refpurpose>
6+
<refpurpose>&Alias; <function>exit</function></refpurpose>
77
</refnamediv>
8-
8+
99
<refsect1 role="description">
1010
&reftitle.description;
11-
<para>
12-
This language construct is equivalent to <function>exit</function>.
13-
</para>
11+
<simpara>
12+
&info.function.alias;
13+
<function>exit</function>.
14+
</simpara>
1415
</refsect1>
15-
1616
</refentry>
17-
1817
<!-- Keep this comment at the end of the file
1918
Local variables:
2019
mode: sgml

reference/misc/functions/exit.xml

Lines changed: 150 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,153 @@
33
<refentry xml:id="function.exit" xmlns="http://docbook.org/ns/docbook">
44
<refnamediv>
55
<refname>exit</refname>
6-
<refpurpose>Output a message and terminate the current script</refpurpose>
6+
<refpurpose>Terminate the current script with a status code or message</refpurpose>
77
</refnamediv>
8-
8+
99
<refsect1 role="description">
1010
&reftitle.description;
1111
<methodsynopsis>
12-
<type>void</type><methodname>exit</methodname>
13-
<methodparam choice="opt"><type>string</type><parameter>status</parameter></methodparam>
14-
</methodsynopsis>
15-
<methodsynopsis>
16-
<type>void</type><methodname>exit</methodname>
17-
<methodparam><type>int</type><parameter>status</parameter></methodparam>
12+
<type>never</type><methodname>exit</methodname>
13+
<methodparam choice="opt"><type class="union"><type>string</type><type>int</type></type><parameter>status</parameter><initializer>0</initializer></methodparam>
1814
</methodsynopsis>
19-
<para>
15+
<simpara>
2016
Terminates execution of the script.
2117
<link linkend="function.register-shutdown-function">Shutdown functions</link>
2218
and <link linkend="language.oop5.decon.destructor">object destructors</link>
23-
will always be executed even if <literal>exit</literal> is called.
24-
</para>
25-
<para>
26-
<literal>exit</literal> is a language construct and it can be called
27-
without parentheses if no <parameter>status</parameter> is passed.
28-
</para>
19+
will always be executed even if <function>exit</function> is called.
20+
However, &finally; blocks are never executed.
21+
</simpara>
22+
<simpara>
23+
An exit code of <literal>0</literal> is used to indicate that the program
24+
succeeded in its tasks.
25+
Any other value indicates some sort of error occurred during execution.
26+
</simpara>
27+
<simpara>
28+
<function>exit</function> is a special function,
29+
because it has a dedicated token in the parser,
30+
as such it can be used like a statement (i.e. without parentheses) to
31+
terminate the script with the default status code.
32+
</simpara>
33+
<caution>
34+
<simpara>
35+
It is not possible to disable, or create a namespaced function shadowing
36+
the global <function>exit</function> function.
37+
</simpara>
38+
</caution>
2939
</refsect1>
3040

3141
<refsect1 role="parameters">
3242
&reftitle.parameters;
33-
<para>
34-
<variablelist>
35-
<varlistentry>
36-
<term><parameter>status</parameter></term>
37-
<listitem>
38-
<para>
39-
If <parameter>status</parameter> is a string, this function prints the
40-
<parameter>status</parameter> just before exiting.
41-
</para>
42-
<para>
43-
If <parameter>status</parameter> is an <type>int</type>, that
44-
value will be used as the exit status and not printed. Exit statuses should be in
45-
the range 0 to 254, the exit status 255 is reserved by PHP and shall
46-
not be used. The status 0 is used to terminate the program
47-
successfully.
48-
</para>
49-
</listitem>
50-
</varlistentry>
51-
</variablelist>
52-
</para>
43+
<variablelist>
44+
<varlistentry>
45+
<term><parameter>status</parameter></term>
46+
<listitem>
47+
<simpara>
48+
If <parameter>status</parameter> is a string,
49+
this function prints the <parameter>status</parameter> just before exiting.
50+
The exit code returned by PHP is <literal>0</literal>.
51+
</simpara>
52+
<para>
53+
If <parameter>status</parameter> is an <type>int</type>,
54+
the exit code returned by PHP will be <parameter>status</parameter>.
55+
<note>
56+
<simpara>
57+
Exit codes should be in the range <literal>0</literal> to <literal>254</literal>,
58+
the exit code <literal>255</literal> is reserved by PHP and should not be used.
59+
</simpara>
60+
</note>
61+
</para>
62+
<warning>
63+
<simpara>
64+
Prior to PHP 8.4.0, <function>exit</function> did not follow PHP's standard
65+
<link linkend="language.types.type-juggling.function">type juggling semantics</link>,
66+
nor respect the
67+
<link linkend="language.types.declarations.strict"><literal>strict_types</literal></link>
68+
declare.
69+
</simpara>
70+
<simpara>
71+
Any value not of type <type>int</type> was cast to <type>string</type>
72+
including <type>resource</type> and <type>array</type> values.
73+
As of PHP 8.4.0, it follows the usual type juggling semantics and throws a
74+
<exceptionname>TypeError</exceptionname> on invalid values.
75+
</simpara>
76+
</warning>
77+
</listitem>
78+
</varlistentry>
79+
</variablelist>
5380
</refsect1>
5481

5582
<refsect1 role="returnvalues">
5683
&reftitle.returnvalues;
57-
<para>
58-
&return.void;
59-
</para>
84+
<simpara>
85+
As this terminates the PHP script, no value is returned.
86+
</simpara>
87+
</refsect1>
88+
89+
<refsect1 role="changelog">
90+
&reftitle.changelog;
91+
<informaltable>
92+
<tgroup cols="2">
93+
<thead>
94+
<row>
95+
<entry>&Version;</entry>
96+
<entry>&Description;</entry>
97+
</row>
98+
</thead>
99+
<tbody>
100+
<row>
101+
<entry>8.4.0</entry>
102+
<entry>
103+
<function>exit</function> is now a proper function,
104+
therefore it follows the usual
105+
<link linkend="language.types.type-juggling.function">type juggling semantics</link>
106+
is affected by the
107+
<link linkend="language.types.declarations.strict"><literal>strict_types</literal></link>
108+
declare, can be called with named arguments, and be a
109+
<link linkend="functions.variable-functions">variable functions</link>.
110+
</entry>
111+
</row>
112+
</tbody>
113+
</tgroup>
114+
</informaltable>
60115
</refsect1>
61116

62117
<refsect1 role="examples">
63118
&reftitle.examples;
64-
<para>
65-
<example>
66-
<title><literal>exit</literal> example</title>
67-
<programlisting role="php">
119+
<example>
120+
<title>Basic <function>exit</function> example</title>
121+
<programlisting role="php">
68122
<![CDATA[
69123
<?php
70124
71-
$filename = '/path/to/data-file';
72-
$file = fopen($filename, 'r')
73-
or exit("unable to open file ($filename)");
125+
// exit program normally
126+
exit();
127+
exit(0);
128+
129+
// exit with an error code
130+
exit(1);
74131
75132
?>
76133
]]>
77-
</programlisting>
78-
</example>
79-
</para>
80-
<para>
81-
<example>
82-
<title><literal>exit</literal> status example</title>
83-
<programlisting role="php">
134+
</programlisting>
135+
</example>
136+
<example>
137+
<title><function>exit</function> example with a <type>string</type></title>
138+
<programlisting role="php">
84139
<![CDATA[
85140
<?php
86141
87-
//exit program normally
88-
exit;
89-
exit();
90-
exit(0);
91-
92-
//exit with an error code
93-
exit(1);
94-
exit(0376); //octal
142+
$filename = '/path/to/data-file';
143+
$file = fopen($filename, 'r')
144+
or exit("unable to open file ($filename)");
95145
96146
?>
97147
]]>
98-
</programlisting>
99-
</example>
100-
</para>
101-
<para>
102-
<example>
103-
<title>Shutdown functions and destructors run regardless</title>
104-
<programlisting role="php">
148+
</programlisting>
149+
</example>
150+
<example>
151+
<title>Shutdown functions and destructors run regardless</title>
152+
<programlisting role="php">
105153
<![CDATA[
106154
<?php
107155
class Foo
@@ -124,41 +172,52 @@ exit();
124172
echo 'This will not be output.';
125173
?>
126174
]]>
127-
</programlisting>
128-
&example.outputs;
129-
<screen>
130-
<![CDATA[
131-
Shutdown: shutdown()
132-
Destruct: Foo::__destruct()
133-
]]>
134-
</screen>
135-
</example>
136-
</para>
175+
</programlisting>
176+
&example.outputs;
177+
<screen>
178+
<![CDATA[
179+
Shutdown: shutdown()
180+
Destruct: Foo::__destruct()
181+
]]>
182+
</screen>
183+
</example>
184+
<example>
185+
<title><function>exit</function> as a statement</title>
186+
<programlisting role="php">
187+
<![CDATA[
188+
<?php
189+
190+
// exit program normally with exit code 0
191+
exit;
192+
193+
?>
194+
]]>
195+
</programlisting>
196+
</example>
137197
</refsect1>
138198

139199
<refsect1 role="notes">
140200
&reftitle.notes;
141-
142-
&note.language-construct;
143-
144-
<note>
145-
<para>
146-
This language construct is equivalent to <function>die</function>.
147-
</para>
148-
</note>
201+
<warning>
202+
<simpara>
203+
Prior to PHP 8.4.0, <function>exit</function> was a language construct
204+
and not a function, therefore it was not possible to call it using
205+
<link linkend="functions.variable-functions">variable functions</link>,
206+
or <link linkend="functions.named-arguments">named arguments</link>.
207+
</simpara>
208+
</warning>
149209
</refsect1>
150210

151211
<refsect1 role="seealso">
152212
&reftitle.seealso;
153-
<para>
154-
<simplelist>
155-
<member><function>register_shutdown_function</function></member>
156-
</simplelist>
157-
</para>
213+
<simplelist>
214+
<member><function>register_shutdown_function</function></member>
215+
<member><link linkend="function.register-shutdown-function">Shutdown functions</link></member>
216+
<member><link linkend="language.oop5.decon.destructor">object destructors</link></member>
217+
</simplelist>
158218
</refsect1>
159219

160220
</refentry>
161-
162221
<!-- Keep this comment at the end of the file
163222
Local variables:
164223
mode: sgml

0 commit comments

Comments
 (0)