100
88

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 5 years have passed since last update.

PHPで標準入力

Posted at

#PHPで標準入力
##1行のみの読み込み

###PHPのコード
fgets(STDIN)で標準入力を読み込める

single.php
<?php  
$stdin = trim(fgets(STDIN));  
var_dump($stdin);
?>

###コマンドラインのコード


C:\xampp\htdocs\PhpProject1>php ./single.php
1
string(1) "1"

コマンドラインでphp 実行したいファイル名を押下してエンター
そうすると標準入力できるので1を入力しエンター
var_dumpで結果を表示。

##複数行の標準入力 (値を入力せずエンターを押下すると入力が終わる)
###PHPのコード

multi.php
<?php  
$stdins = array();
	while(true)
	{
		$stdin = trim(fgets(STDIN));
		if ($stdin === '')
	 	{
			var_dump($stdins);
			return;
		}  
		$stdins[] = $stdin;
	}
?>

###コマンドラインのコード

C:\xampp\htdocs\PhpProject1>php ./multi.php
1
2
3
テスト
5

array(5) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
  [3]=>
  string(6) "テスト"
  [4]=>
  string(1) "5"
}

100
88
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
100
88

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?