Skip to content

Instantly share code, notes, and snippets.

@Restoration
Last active October 18, 2016 05:59
Show Gist options
  • Select an option

  • Save Restoration/7d8bf739acf38beb61dc6f098ec94e3b to your computer and use it in GitHub Desktop.

Select an option

Save Restoration/7d8bf739acf38beb61dc6f098ec94e3b to your computer and use it in GitHub Desktop.

変数の型を調べる

/*
gettype関数またはver_dump関数の使用

PHPにおいて変数はプログラマが設定するものではない
その変数が使用される文脈に応じて
PHPが実行時に決定する
また、型が自動で変換される場合もある

*/

//変数に数字を代入
$a = 123;
//変数に文字列を代入
$b = '321';

echo '$aの型:' .gettype($a). '<br >';
echo '$bの型: ' .gettype($b). '<br>';

//is_int()関数は整数型かどうかを返す
if(is_int($a)){
	echo '$aはintegerです<br>';
} else {
	echo '$aはintegerではありません<br>';
}

//is_string関数は文字列かどうかを返します
if(is_string($b)){
	echo '$bはstringです<br>';
} else {
	echo '$bはstringではありません<br>';
}


//ver_dumpでも変数の型がわかります。
echo '<pre>';
echo var_dump($a);
echo var_dump($b);
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment