downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

fgetcsv> <fflush
Last updated: Fri, 19 Feb 2010

view this page in

fgetc

(PHP 4, PHP 5)

fgetcファイルポインタから1文字取り出す

説明

string fgetc ( resource $handle )

指定したファイルポインタから 1 文字読み出します。

パラメータ

handle

ファイルポインタは、有効なファイルポインタである必要があり、 fopen() または fsockopen() で正常にオープンされた (そしてまだ fclose() でクローズされていない) ファイルを指している必要があります。

返り値

handle が指すファイルポインタから 1 文字読み出し、 その文字からなる文字列を返します。EOF の場合に FALSE を返します。

警告

この関数は論理値 FALSE を返す可能性がありますが、FALSE として評価される 0 や "" といった値を返す可能性もあります。 詳細については 論理値の セクションを参照してください。この関数の返り値を調べるには ===演算子 を 使用してください。

例1 fgetc() の例

<?php
$fp 
fopen('somefile.txt''r');
if (!
$fp) {
    echo 
'somefile.txt をオープンできませんでした';
}
while (
false !== ($char fgetc($fp))) {
    echo 
"$char\n";
}
?>

注意

注意: この関数はバイナリデータに対応しています。

参考

  • fread() - バイナリセーフなファイルの読み込み
  • fopen() - ファイルまたは URL をオープンする
  • popen() - プロセスへのファイルポインタをオープンする
  • fsockopen() - インターネット接続もしくはUnix ドメインソケット接続をオープンする
  • fgets() - ファイルポインタから 1 行取得する



add a note add a note User Contributed Notes
fgetc
alex at alexdemers dot me
11-May-2009 05:30
The best and simplest way to get input from a user in the CLI with only PHP is to use fgetc() function with the STDIN constant:

<?php

echo 'Are you sure you want to quit? (y/n) ';
$input = fgetc(STDIN);

if (
$input == 'y')
{
    exit(
0);
}

?>
ktraas at gmail dot com (Kevin Traas)
24-Mar-2009 03:08
I was using command-line PHP to create an interactive script and wanted the user to enter just one character of input - in response a Yes/No question.  Had some trouble finding a way to do so using fgets(), fgetc(), various suggestions using readline(), popen(), etc.  Came up with the following that works quite nicely:

$ans = strtolower( trim( `bash -c "read -n 1 -t 10 ANS ; echo \\\$ANS"` ) );

fgetcsv> <fflush
Last updated: Fri, 19 Feb 2010
 
 
show source | credits | sitemap | contact | advertising | mirror sites