How Can I Use PHP Standard Input for Scripting?

Linda Hamilton
Release: 2024-11-02 12:43:30
Original
347 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template