PHP stream_clear_errors() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_clear_errors() discards the stream errors that PHP has stored from earlier stream operations.

Call it before a new stream task, then use stream_last_errors() afterwards to see only the errors from that task. The function is available in PHP 8.5 and later.

Syntax

Call stream_clear_errors() with the values you need. Start with this line.

PHP
stream_clear_errors();
CallWhat it does
stream_clear_errors()Removes all stored stream errors.
stream_clear_errors(); $e = stream_last_errors();Clears first, so $e starts as an empty array.

This function was added in PHP 8.5. It takes no arguments and does not touch the streams themselves.

Library declarations
PHP
stream_clear_errors(): void

Parameters

ParameterDescription
NoneThis function takes no parameters.

Return Value

Return ValueDescription
voidNothing is returned. The stored stream errors are removed.

PHP Examples

Example 1 — Clear the Error List

Clear the stored errors, then count what is left.

PHP
<?php
stream_clear_errors();

$errors = stream_last_errors();
echo "Stored errors: ", count($errors), PHP_EOL;

How It Works

  1. Call stream_clear_errors(). Any stored stream errors are discarded.
  2. Ask for the stored errors. stream_last_errors() returns the current list.
  3. Count the list. count() gives the number of errors.
  4. Get 0. The list is empty after a clear.

Example 2 — Clear After a Failed Open

Open a file that does not exist, then clear before checking.

PHP
<?php
$fp = @fopen("missing-file.txt", "r");

if ($fp === false) {
    echo "fopen failed", PHP_EOL;
}

stream_clear_errors();
echo "After clear: ", count(stream_last_errors()), PHP_EOL;

How It Works

  1. Try to open a missing file. The @ hides the warning.
  2. Check for false. A failed fopen() returns false.
  3. Call stream_clear_errors(). The stored errors are removed.
  4. Count again. The list is now empty.

Example 3 — Guard for Older PHP Versions

Check that the function exists before calling it.

PHP
<?php
if (function_exists("stream_clear_errors")) {
    stream_clear_errors();
    echo "Errors cleared", PHP_EOL;
} else {
    echo "Requires PHP 8.5 or later", PHP_EOL;
}

How It Works

  1. Test with function_exists(). It is true only on PHP 8.5 and later.
  2. Call the function. The stored errors are discarded.
  3. Print a message. This tells you the clear step ran.
  4. Use the else branch for old PHP. The script keeps running without an error.

Frequently Asked Questions

It discards the stream errors that PHP has stored from earlier stream operations, so stream_last_errors() starts again from an empty list.
It is available in PHP 8.5 and later. On older versions the function does not exist, so guard calls with function_exists() if you need to support them.
No. It only clears the stored error list. Open streams stay open and keep working.
Nothing. Its return type is void, so there is no value to assign or test.
Call it right before a stream task when you plan to read stream_last_errors() afterwards, so old errors do not get mixed in.

Next: stream_context_create()

stream_context_create() builds a stream context resource that stores options and parameters for wrappers such as http, ssl, or ftp.

stream_context_create() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful