Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions language/types/type-juggling.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ var_dump($bar);
<member><literal>(unset)</literal> - cast to <type>NULL</type></member>
</simplelist>

<simpara>
The <link linkend="language.types.void.casting"><literal>(void)</literal></link>
cast is also available as of PHP 8.5.0, but it is not a value conversion.
It is used as a statement to explicitly discard the result of an expression.
</simpara>

<warning>
<para>
<literal>(integer)</literal> is an alias of the <literal>(int)</literal> cast.
Expand Down Expand Up @@ -422,6 +428,7 @@ if ($fst === $str) {
<member><link linkend="language.types.object.casting">Converting to object</link></member>
<member><link linkend="language.types.resource.casting">Converting to resource</link></member>
<member><link linkend="language.types.null.casting">Converting to NULL</link></member>
<member><link linkend="language.types.void.casting">Discarding a value with <literal>(void)</literal></link></member>
<member><link linkend="types.comparisons">The type comparison tables</link></member>
</simplelist>
</para>
Expand Down
33 changes: 33 additions & 0 deletions language/types/void.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@
</simpara>
</note>

<sect2 xml:id="language.types.void.casting">
<title>Discarding a value with <literal>(void)</literal></title>

<simpara>
The <literal>(void)</literal> syntax may be used to explicitly discard the
result of an expression. This is useful to indicate that ignoring a return
value is intentional, especially when calling a function or method marked
with the <classname>NoDiscard</classname>
attribute.
</simpara>

<simpara>
Unlike other casts, <literal>(void)</literal> does not convert the value to
another type and does not produce a value. It is a statement and cannot be
used as part of an expression.
</simpara>

<example>
<title>Discarding a return value</title>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
#[\NoDiscard]
function process(): bool {
return true;
}

(void) process(); // Explicitly discard the return value
?>
]]>
</programlisting>
</example>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
Expand Down