PHP 8.5 allows a (void) statement to explicitly discard a function result, including a NoDiscard return.
What It Does and When to Use It
Explicitly signal that the return value is intentionally unused. A minimum platform requirement controls what syntax can be parsed; target-version tests find runtime and extension incompatibilities.
Requirements, Inputs and Expected Behavior
The code using the pipe operator, NoDiscard, or void cast requires PHP 8.5 or later; code with older syntax can still run on earlier supported PHP releases. (void) is statement-only and does not produce a value or work in an assignment.
Example 1: Basic Operation
Explicitly signal that the return value is intentionally unused.
<?php
#[\NoDiscard]
function queueMessage(): bool { return true; }
(void) queueMessage();
Example 2: Behavior and Validation
Read the annotated function result when your control flow depends on success; PHP 8.5 supports the NoDiscard attribute.
<?php
#[\NoDiscard('Check the result before continuing')]
function verifyTask(): bool { return true; }
$result = verifyTask();
if (!$result) throw new RuntimeException('Task failed');
Practical Example: Application Use
Remember (void) discards a result as a statement; it does not turn it into null.
<?php
function optionalDiagnostic(): string { return 'diagnostic'; }
(void) optionalDiagnostic();
echo 'Done';
Important Behavior and Edge Cases
The second example highlights: Read the annotated function result when your control flow depends on success; PHP 8.5 supports the NoDiscard attribute. The practical example adds: Remember (void) discards a result as a statement; it does not turn it into null.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| Minimum PHP version and target PHP version | A minimum platform requirement controls what syntax can be parsed; target-version tests find runtime and extension incompatibilities. |
| Choosing safely | Do not introduce syntax from a newer minor version before every production host supports it. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming newer syntax parses on older PHP | Lint with the oldest supported runtime; gate or delay newer syntax. |
| Upgrading CLI PHP but not the web worker | Check the FPM binary, all extensions and the Composer platform in staging. |
| Skipping operational constraints | (void) is statement-only and does not produce a value or work in an assignment. |
Key Takeaways
- PHP 8.5 allows a (void) statement to explicitly discard a function result, including a NoDiscard return.
- Remember (void) discards a result as a statement; it does not turn it into null.
- Do not introduce syntax from a newer minor version before every production host supports it.