PHP stream_last_errors() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_last_errors() returns the stream errors stored by the most recent stream operation, as an array. The array is empty when there were no errors.

Use it after fopen(), file_get_contents(), or a similar call when false is not enough and you want more detail. Pair it with stream_clear_errors() to start from a clean list. The function is available in PHP 8.5 and later.

Syntax

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

PHP
$errors = stream_last_errors();
CallWhat it does
stream_last_errors()Returns the stored errors, or an empty array.
count(stream_last_errors())Gives the number of stored errors.
empty(stream_last_errors())True when there are no stored errors.

This function was added in PHP 8.5. It takes no arguments and only reads the stored errors; it does not clear them.

Library declarations
PHP
stream_last_errors(): array

Parameters

ParameterDescription
NoneThis function takes no parameters.

Return Value

Return ValueDescription
arrayThe stored errors of the most recent stream operation. An empty array if there are none.

PHP Examples

Example 1 — Check for Stored Errors

Start clean, then ask for the stored errors.

PHP
<?php
stream_clear_errors();

$errors = stream_last_errors();
echo count($errors) === 0 ? "No stream errors" : "Errors found", PHP_EOL;

How It Works

  1. Clear first. stream_clear_errors() removes older errors.
  2. Call stream_last_errors(). It returns an array.
  3. Count the array. An empty array has a count of 0.
  4. Print the result. The ternary picks the message.

Example 2 — A Successful Open Leaves No Errors

Open an in-memory stream and check the list afterwards.

PHP
<?php
$fp = fopen("php://memory", "r+");

$errors = stream_last_errors();
echo "Errors after fopen: ", count($errors), PHP_EOL;

fclose($fp);

How It Works

  1. Open php://memory. This always works, so no error is stored.
  2. Read the stored errors. stream_last_errors() returns the list.
  3. Count them. The result is 0.
  4. Close the stream. fclose() frees the handle.

Example 3 — Read the Result After a Failed Open

Open a missing file and confirm the result is an array.

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

if ($fp === false) {
    $errors = stream_last_errors();
    echo "fopen failed", PHP_EOL;
    echo "Result is an array: ", is_array($errors) ? "yes" : "no", PHP_EOL;
}

How It Works

  1. Try to open a missing file. The @ hides the warning.
  2. Test for false. A failed fopen() returns false.
  3. Call stream_last_errors() right away. Do this before another stream call.
  4. Check the type. The result is always an array.

Frequently Asked Questions

It returns the stream errors that were stored by the most recent stream operation, as an array. The array is empty when there are no errors.
It is available in PHP 8.5 and later. Guard calls with function_exists() if your code must also run on older versions.
It returns an empty array, so count() gives 0 and empty() is true.
Call stream_clear_errors(). After that, stream_last_errors() starts again from an empty list.
Call it right after the stream call that failed, such as fopen() or file_get_contents(), before another stream operation replaces the stored errors.

Next: stream_notification_callback()

stream_notification_callback() documents the callback signature used with the notification parameter of a stream context.

stream_notification_callback() 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