How to take input from stdin in php?

The CLI SAPI defines a few constants for I/O streams to make programming for the command line a bit easier.

CLI specific Constants
ConstantDescription
STDIN

An already opened stream to stdin. This saves opening it with

<?php
$stdin 
fopen('php://stdin''r');
?>

If you want to read single line from stdin, you can use

<?php
$line 
trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN"%d\n"$number); // reads number from STDIN
?>

STDOUT

An already opened stream to stdout. This saves opening it with

<?php
$stdout 
fopen('php://stdout''w');
?>

STDERR

An already opened stream to stderr. This saves opening it with

<?php
$stderr 
fopen('php://stderr''w');
?>

Given the above, you don't need to open e.g. a stream for stderr yourself but simply use the constant instead of the stream resource:

php -r 'fwrite(STDERR, "stderr\n");'

You do not need to explicitly close these streams, as they are closed automatically by PHP when your script ends.

Note:

These constants are not available if reading the PHP script from stdin.


#PHP #Linux

Seperti yang sudah saya bahas pada tulisan sebelumnya. PHP CLI merupakan PHP yang berjalan di atas Command Line. Biasanya, program yang berbasis teks memiliki fungsi tersendiri untuk mengambil input. Contohnya Python menggunakan fungsi input() dan raw_input(), java menggunakan Scanner dan BufferReader, C++ menggunakan cin, Pascal menggunakan readln(), dsb. Lalu, Bagaiaman dengan PHP?

Table of Contents

  • Menggunakan Fungsi fopen()
  • Menggunakan STDIN
  • Baca Juga ini
  • How to take input from stdin in php?
  • How to read input value in php?
  • What is stdin php?
  • How do you input numbers in php?

Menggunakan Fungsi fopen()

Cara pertama mengambil input di PHP CLI adalah dengan menggunakan fungsi fopen(). Pada dasarnya fungsi ini digunakan untuk membuka atau membaca file. Namun, dapat digunakan juga untuk mengambil input. Contoh penggunaan:

<?php

echo "Siapa nama kamu: ";
$input_nama = fopen("php://stdin","r");
$nama = trim(fgets($input_nama));

echo "Hello $nama, apa kabar?\n";

?>

Hasilnya ketika dieksekusi:

Pada contoh di atas, fungsi fopen() harus diberikan paramater "php://stdin" dan "r" untuk membaca apa yang diketik di Terminal. Kemudian, kita membutuhkan dua fungsi lagi agar mendapatkan nilai yang murni. Fungsi fgets() untuk membaca baris inputan, kemudian fungsi trim() untuk menghapus karakter enter ("\n") atau spasi di akhir inputan yang ikut dibaca. Bila kita tidak menggunakan fungsi trim() maka nilai yang akan dibaca ketika diinputkan “Petani Kode” adalah “Petani Kode\n”. “\n” itu didapatkan dari tombol enter.

Menggunakan STDIN

Ada cara yang lebih sederhana, yaitu menggunakan STDIN. Cara ini sama juga seperti yang tadi, cuma perbedaanya tidak menggunakan fungsi fopen(). Kita bisa langsung mengambil nilai dengan fungsi fgets(). Lalu kita gunakan fungsi trim() untuk membersihkannya dari “\n”. Contoh:

<?php

echo "Dari mana anda berasl: ";
$asal = trim(fgets(STDIN));
echo "Oh, dari $asal ya\n";

?>

Hasil output ketika dieksekusi:

Itulah dua cara mengambil input di PHP CLI, manakah cara yang paling mudah? Silahkan anda tentukan sendiri. Kalau pun ada cara lain silahkan ditambahkan dan kalau masih tidak paham silahkan ditanyakan.

Referensi: https://stackoverflow.com/questions/6543841/php-cli-getting-input-from-user-and-then-dumping-into-variable-possible

Baca Juga ini

© 2022 Petani Kode · Made with ❤️ using Hugo 0.98.0

One of the most common command line tricks in Linux is to chain multiple commands using the pipe symbol i.e. you feed the output of one command as the input of another. This is called standard input and standard output redirection. PHP is usually used to program for the web but it can also be used as a shell scripting language to automate tasks and like any good shell scripting language it is possible to read from standard input and write to standard output using PHP.

To read from standard input you use php://stdin and to write to standard output you use php://stdout. For example reading standard input into a string variable and then echoing that variable:

$stdin = file_get_contents( 'php://stdin' ) ;
echo $stdin;

To write to standard output you use php://stdout:

$handle = fopen( 'php://stdout', 'w' ) ;
fwrite( $handle, "Hello World! \n" );
fclose( $handle );

The above code displays “Hello World!”. It’s equivalent to using the echo statement to write something on screen.

Two resource constants STDIN and STDOUT can also be used as file handles. This saves you from using fopen:

fwrite( STDOUT, "Hello World! \n" );

To write to standard error you can use php://stderr or the resource constant STDERR:

fwrite( STDERR, "ERROR ERROR! \n" );

How to take input from stdin in php?

It is possible to read the stdin by creating a file handle to php://stdin and then read from it with fgets() for a line for example (or, as you already stated, fgetc() for a single character): <? php $f = fopen( 'php://stdin', 'r' ); while( $line = fgets( $f ) ) { echo $line; } fclose( $f ); ?>

How to read input value in php?

Use PHP's $_POST or $_GET superglobals to retrieve the value of the input tag via the name of the HTML tag.

What is stdin php?

php://stdin, php://stdout and php://stderr allow direct access to standard input stream device, standard output stream and error stream to a PHP process respectively. Predefined constants STDIN, STDOUT and STDERR respectively represent these streams.

How do you input numbers in php?

Adding in form in PHP..

<body>.

<form>.

Enter First Number:.

<input type="number" name="number1" /><br><br>.

Enter Second Number:.

<input type="number" name="number2" /><br><br>.

<input type="submit" name="submit" value="Add">.

</form>.

Which method allows us to read data from the standard input stdin php?

Method 1: Using readline() function is a built-in function in PHP. This function is used to read console input.

What is stdin php?

php://stdin, php://stdout and php://stderr allow direct access to standard input stream device, standard output stream and error stream to a PHP process respectively. Predefined constants STDIN, STDOUT and STDERR respectively represent these streams.

How can I take input from HTML to php?

Use PHP's $_POST or $_GET superglobals to retrieve the value of the input tag via the name of the HTML tag. To show the value: <? php echo $_GET['subject']; ?>

How do I prompt a user in php?

Prompting users for input To get input from users, you also have to prompt them to enter something. You can use PHP's `readline() function to get this input from the console.