1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

phpで標準入力する方法

Posted at

今回は備忘録をかねて、様々な例を使ってphpで行う標準入力について、まとめてみました。

基本編

app.php
/* 1行のデータを取得する場合
* 例:「hogehoge」
*/
$data = rtrim(fgets(STDIN));

/* ' '(空白行)で分けられた1行のデータを配列で取得する場合
* 例:「hogehoge gehogeho」
*/
$data = explode(' ', rtrim(fgets(STDIN)));

応用編

app.php
/* 先頭の行に数字Nが書かれてて、後続のN行分のデータを配列で取得する場合
* 例:「2
*    hogehoge
*    gehogeho」
*/
$data = rtrim(fgets(STDIN));
  $data_arr=array();
for($i=0;$i<$data;$i++){
  $data_arr[]=rtrim(fgets(STDIN));
}
/* 出力値
*    Array(
*    [0] => gehogeho
*    [1] => hogehoge
*    )
*/
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?