【PHP】変数の型を取得する gettype()

  • URLをコピーしました!
目次

gettype関数を使い変数の型を取得する

gettype関数は変数の型を取得します。

返り値

返された文字列は、以下のいずれかの値を持ちます。

  • “boolean“
  • “integer“
  • “double“
  • “string“
  • “array“
  • “object“
  • “resource“
  • “resource (closed)” 
  • “NULL“
  • “unknown type”

コード

echo gettype(true) . PHP_EOL;
echo gettype(0) . PHP_EOL;
echo gettype(0.1) . PHP_EOL;
echo gettype('abc') . PHP_EOL;
echo gettype(array(0,1,2)) . PHP_EOL;
echo gettype(new datetime()) . PHP_EOL;
echo gettype(NULL) . PHP_EOL;

$fp = fopen('sample.txt','r');
echo gettype($fp) . PHP_EOL;
fclose($fp);
echo gettype($fp) . PHP_EOL;

実行結果

boolean
integer
double
string
array
object
NULL
resource
resource (closed)

参考サイト

PHP: gettype – Manual

[A8_TechAcademy065]

[Footer]

この記事が気に入ったら
フォローしてね!

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次