PHP 8.5.0 RC 5 available for testing

stream_isatty

(PHP 7 >= 7.2.0, PHP 8)

stream_isattyストリームがターミナル型のデバイスかを調べる

説明

stream_isatty(resource $stream): bool

stream が、有効なターミナル型のデバイスを参照しているかを調べます。 この関数は、posix_isatty() よりも移植性に優れたバージョンです。 なぜなら、この関数は Windows でも動作するからです。

パラメータ

stream

戻り値

成功した場合に true を、失敗した場合に false を返します。

例1 stream_isatty() の例

このコマンドは、標準出力 / 標準エラー出力 がファイルにリダイレクトされているかどうかを調べるのに使えます。

php -r "var_export(stream_isatty(STDERR));"

上の例の出力は、 たとえば以下のようになります。


true
php -r "var_export(stream_isatty(STDERR));" 2>output.txt

上の例の出力は、 たとえば以下のようになります。


false

add a note

User Contributed Notes 1 note

up
0
frmphp at dyadic dot org
1 day ago
This function returns False (output is being redirected) regardless of the form of redirection. On Windows, both of these are redirected:
- php.exe script.php > outFle.txt
- php.exe script.php | Tee outFle.txt
In the second case, Tee causes the redirection to also echo to the console.

An edge usage is: in debugging a long-running script, output is wanted both in a file for later review and also in the console so it's visible in real time. But if the script alters its output based on this function, then in the second case it will produce output as if for redirection only, even though Tee enables console output.
To Top