PHP stream_isatty() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_isatty() returns true when the given stream is connected to a terminal (TTY), and false when it is not.

Interactive CLI programs use this to decide whether to show colors, progress bars, or plain log text. File streams, pipes, and php://memory handles are not TTYs, so they return false.

Syntax

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

PHP
$isTty = stream_isatty($stream);
CallWhat it does
stream_isatty(STDIN)true when standard input is an interactive terminal.
stream_isatty(STDOUT)true when standard output is a terminal, false when redirected to a file.
stream_isatty($fp)false for ordinary file or memory streams.

Results for STDIN/STDOUT depend on how you launch PHP. Tutorials below use php://memory so the printed answer stays fixed: not a TTY.

Library declarations
PHP
stream_isatty(resource $stream): bool

Parameters

ParameterDescription
streamRequired. The stream resource to test.

Return Value

Return ValueDescription
booltrue if the stream is a TTY; false otherwise.

PHP Examples

Example 1 — Memory Streams Are Not TTYs

Open php://memory and show that stream_isatty() returns false.

PHP
<?php
$fp = fopen("php://memory", "r+");
echo stream_isatty($fp) ? "tty" : "not tty", PHP_EOL;
fclose($fp);

How It Works

  1. A memory stream is opened.
  2. stream_isatty() checks whether that handle is a terminal.
  3. php://memory is never a TTY, so the result is false.
  4. The script prints not tty.

Example 2 — Temp Streams Are Not TTYs Either

Repeat the check with php://temp for the same boolean answer.

PHP
<?php
$fp = fopen("php://temp", "r+");
var_dump(stream_isatty($fp));
fclose($fp);

How It Works

  1. php://temp opens a temporary stream.
  2. stream_isatty() returns false.
  3. var_dump() prints bool(false) so the type is clear.
  4. Only real terminal devices return true.

Example 3 — Choose Plain Output When Not a TTY

Branch on the boolean to pick a plain label versus a fancy label.

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

if (stream_isatty($out)) {
    $label = "[color-on] done";
} else {
    $label = "done";
}

echo $label, PHP_EOL;
fclose($out);

How It Works

  1. The output handle is a memory stream, not a terminal.
  2. stream_isatty() is false, so the plain branch runs.
  3. The script prints done without color markers.
  4. The same pattern works with STDOUT in real CLI apps.

Frequently Asked Questions

It returns true when a stream is attached to an interactive terminal, and false for files, pipes, and memory streams.
So you can enable colors or live progress only when a human is watching, and keep plain text when output is redirected.
No. It is true only when input really is a terminal. Piped input makes it false.
Yes. It simply returns false because that stream is not a TTY.
stream_isatty() asks "is this a terminal?". stream_is_local() asks "is this a local wrapper/path?". A local file is local but not a TTY.

Next: stream_notification_callback()

stream_notification_callback() documents the callback signature used with the notification.

stream_notification_callback() tutorial →

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