0
0

More than 3 years have passed since last update.

PHPの標準入力について

Posted at

// 標準入力からの入力値を変数に代入します
$single_line_input = fgets(STDIN);

// 取得した入力値を半角スペースで分解します
$array = explode(" ", $input_line);
ex)入力12 13 14 15
  →[12,13,14,15]

// 単一行の入力の場合はこれだけで各入力値が配列の要素として使えます

・標準入力が複数行の場合

// 標準入力を一行ずつ配列に代入します
//trimは入力された文字列の前後の空白をなくす
while ($line = fgets(STDIN)) {
$tmp[] = trim($line);
}

// 配列の各要素をさらに分解します
foreach ($tmp as $key => $value) {
$array[] = explode(" ", $value);}

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