大网数据广告活动
PHP 语言与运行手册

Void

· 52CE 我爱测

void is a return-only type declaration indicating the function does not return a value, but the function may still terminate. Therefore, it cannot be part of a union type declaration. Available as of PHP 7.1.0.

Void

void is a return-only type declaration indicating the function does not return a value, but the function may still terminate. Therefore, it cannot be part of a union type declaration. Available as of PHP 7.1.0.

A return statement with an expression in a void function is a compile-time error, even if the return would not be reached during execution. This includes return null;; use a bare return; to exit the function early. The null mentioned below is only the implicit value of the call.

Note:

Even if a function has a return type of void, it will still return a value. This value is always null.

Discarding a value with (void)

The (void) 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 NoDiscard attribute.

Unlike other casts, (void) 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.

Example #1 Discarding a return value

<?php
#[\NoDiscard]
function process(): bool {
    return true;
}

(void) process(); // Explicitly discard the return value
?>

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。