Content · Web Development · PHP

PHP 8.5 NoDiscard Attribute: Preventing Ignored Return Values

PHP 8.5 NoDiscard Attribute: Preventing Ignored Return Values: The NoDiscard attribute in PHP 8.5 warns when a function or method return value is silently ignored. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

The NoDiscard attribute in PHP 8.5 warns when a function or method return value is silently ignored.

The examples below begin with the core behavior, then move to an application-oriented use.

How PHP 8.5 NoDiscard Attribute: Preventing Ignored Return Values Works


The NoDiscard attribute in PHP 8.5 warns when a function or method return value is silently ignored.

Compatibility note: Requires PHP 8.5 or later.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

#[NoDiscard]
function saveDraft(string $text): bool { return $text !== ""; }
$wasSaved = saveDraft("Hello");
var_dump($wasSaved);

The NoDiscard attribute in PHP 8.5 warns when a function or method return value is silently ignored.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

#[NoDiscard]
function parseTicket(string $ticket): ?int { return ctype_digit($ticket) ? (int) $ticket : null; }
$ticketId = parseTicket("42");
var_dump($ticketId);

It raises a warning, not a runtime exception; use the returned result or explicitly discard it when intentional.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

#[NoDiscard]
function updateInventory(int $available): int { return max(0, $available - 1); }
$remaining = updateInventory(10);
echo "Remaining: $remaining";

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • It raises a warning, not a runtime exception; use the returned result or explicitly discard it when intentional.
  • The NoDiscard attribute in PHP 8.5 warns when a function or method return value is silently ignored.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
PHP 8.5 NoDiscard Attribute: Preventing Ignored Return ValuesThe NoDiscard attribute in PHP 8.5 warns when a function or method return value is silently ignored.
Alternative approachDocumentation alone cannot enforce the same return-value acknowledgment.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of PHP 8.5 NoDiscard Attribute: Preventing Ignored Return ValuesIt raises a warning, not a runtime exception; use the returned result or explicitly discard it when intentional.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyDocumentation alone cannot enforce the same return-value acknowledgment.

Key Takeaways


  • The NoDiscard attribute in PHP 8.5 warns when a function or method return value is silently ignored.
  • It raises a warning, not a runtime exception; use the returned result or explicitly discard it when intentional.
  • Use the example that matches the values, runtime version and dependencies in your application.

PHP 8.5 NoDiscard Attribute: Preventing Ignored Return Values is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.