0
0

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]標準入力する

Last updated at Posted at 2021-10-23

#➀1行だけの場合
##入力値

1 2

##コード

$num = trim(fgets(STDIN));
echo $num;

##結果

1 2

#➁複数行の場合
##入力値

1 2
3 4
5 6

##コード

while($num = trim(fgets(STDIN))){ 
    echo $num,PHP_EOL;            
    #PHP_EOLで改行
}

##結果

1 2
3 4
5 6

#➂半角スペース等で区切られた文字列が複数行ある場合

##入力値

0 1 2 3 4
5 6 7 8 9

##コード

while($num = trim(fgets(STDIN))){ 
    $array[] = explode(" ", $num);
}

#出力
var_dump($array);

1.explode関数を使用して、入力値を半角スペースで分割する
2.分割した入力値を配列に入れていく
##結果

array(2) {
  [0]=>
  array(5) {
    [0]=>
    string(1) "0"
    [1]=>
    string(1) "1"
    [2]=>
    string(1) "2"
    [3]=>
    string(1) "3"
    [4]=>
    string(1) "4"
  }
  [1]=>
  array(5) {
    [0]=>
    string(1) "5"
    [1]=>
    string(1) "6"
    [2]=>
    string(1) "7"
    [3]=>
    string(1) "8"
    [4]=>
    string(1) "9"
  }
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?