Detect if stdin is a tty device (terminal) or pipe in PHP?

前端 未结 2 2205
春和景丽
春和景丽 2021-02-20 05:22

I wrote a php script. I want it show help message when called with standard input connected to a tty device (terminal) before reading and executing interactively, but dont show

相关标签:
2条回答
  • 2021-02-20 06:02

    Use posix_isatty.

    This function accepts both a file descriptor (an integer) and a PHP stream. If it receives a PHP stream, it automatically attempts to cast it in order to obtain a file descriptor and use it instead.

    0 讨论(0)
  • 2021-02-20 06:11

    Since PHP 7.2 you can use stream_isatty, which works on Windows too.

    For example:

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

    Results in

    bool(true)
    

    But

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

    Results in

    bool(false)
    

    (this of course works on STDOUT too).

    0 讨论(0)
提交回复
热议问题