"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Read Standard Input (stdin) in PHP?

How Can I Read Standard Input (stdin) in PHP?

Published on 2024-11-03
Browse:153

How Can I Read Standard Input (stdin) in PHP?

Reading Standard Input in PHP

While PHP is commonly used for web development, it's also a general-purpose scripting language. One aspect that may be less familiar is the possibility of reading input from standard input (stdin). This article explores how to achieve this in PHP, particularly using the fgetc() function.

In PHP, standard output is generally available through functions such as print and echo. However, to read from stdin, we must create a file handle and then use the fgets() function for line input or fgetc() for character input.

The following script demonstrates how to read from stdin using PHP:

This script opens a file handle to stdin using fopen('php://stdin', 'r'). The fgets() function is then employed to read lines from stdin, and each line is echoed to the standard output.

By following these steps, you can easily read standard input in PHP, expanding its functionality beyond web development into general-purpose scripting scenarios.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3