Home >Backend Development >PHP Tutorial >How Can I Use PHP Standard Input for Scripting?

How Can I Use PHP Standard Input for Scripting?

Linda Hamilton
Linda HamiltonOriginal
2024-11-02 12:43:30384browse

How Can I Use PHP Standard Input for Scripting?

PHP Standard Input

PHP is primarily known for web development where standard input is unavailable. However, PHP's versatility extends to general-purpose scripting when adhering to its web-based conventions. This article explores the possibility of PHP scripts retrieving input from stdin using fgetc() or other input functions.

To input data from stdin using PHP, a file handle to php://stdin can be created. Subsequently, data can be read using fgets() (for line input) or fgetc() (for single character input).

Here's an example PHP script:

<code class="php"><?php
$f = fopen('php://stdin', 'r');

while ($line = fgets($f)) {
  echo $line;
}

fclose($f);
?></code>

By executing this script, the PHP program reads and displays input provided through the standard input channel.

The above is the detailed content of How Can I Use PHP Standard Input for Scripting?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn