現在の言語: 日本語

戻る

get_debug_type
php型宣言

get_debug_typeについて
直感的で正確な名前(例:クラス名など)を返します。


[サンプル]

copy
class test1
{
	function test1()
	{
		$tmp = [];
		$tmp[]="100";
		$cls = new stdClass();
		$this->judgeType("123");//value is numeric :123
		$this->judgeType("test");//value is string :test
		$this->judgeType(123);//value is numeric :123
		$this->judgeType(1.23);//value is numeric :1.23
		$this->judgeType($tmp);//value is array
		$this->judgeType(null);//value is null
		$this->judgeType($cls);//value is other type
	}
	private function judgeType($value)
	{
		if(is_numeric($value) === true)
		{
			echo "value is numeric :$value".PHP_EOL;
		}
		else if(is_string($value) === true)
		{
			echo "value is string :$value".PHP_EOL;
		}
		else if(is_int($value) === true)
		{
			echo "value is int :$value".PHP_EOL;
		}
		else if(is_float($value) === true)
		{
			echo "value is float :$value".PHP_EOL;
		}
		else if(is_array($value) === true)
		{
			echo "value is array".PHP_EOL;
		}
		else if(is_null($value) === true)
		{
			echo "value is null".PHP_EOL;
		}
		else
		{
			echo "value is other type".PHP_EOL;
		}

	}
	function test2()
	{
		$tmp = [];
		$tmp[]="100";
		$cls = new stdClass();
		$this->judgeTypePHPFunction("123", "123[string]");
		$this->judgeTypePHPFunction("test", "test[string]");
		$this->judgeTypePHPFunction(123, "123[int]");
		$this->judgeTypePHPFunction(1.23, "123[float]");
		$this->judgeTypePHPFunction($tmp, "array");
		$this->judgeTypePHPFunction(null, "null");
		$this->judgeTypePHPFunction($cls, "class");
		$this->judgeTypePHPFunction(fopen("memo.txt", "r"), "fopen");
		//[結果]
		/*
		get_debug_type(123[string]):string
		gettype(123[string]):string
		get_debug_type(test[string]):string
		gettype(test[string]):string
		get_debug_type(123[int]):int
		gettype(123[int]):integer
		get_debug_type(123[float]):float
		gettype(123[float]):double
		get_debug_type(array):array
		gettype(array):array
		get_debug_type(null):null
		gettype(null):NULL
		get_debug_type(class):stdClass
		gettype(class):object
		get_debug_type(fopen):resource (stream)
		gettype(fopen):resource
		*/
	}
	private function judgeTypePHPFunction($value ,string $memo)
	{
		echo "get_debug_type($memo):".get_debug_type($value).PHP_EOL;
		echo "gettype($memo):".gettype($value).PHP_EOL;
	}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();
$cls1->test2();
echo "</pre>";
copy
class test1
{ 
	function test1() 
	{ 
		$tmp = []; 
		$tmp[]="100"; 
		$cls = new stdClass(); 
		$this->judgeType("123");//value is numeric :123 
		$this->judgeType("test");//value is string :test 
		$this->judgeType(123);//value is numeric :123 
		$this->judgeType(1.23);//value is numeric :1.23 
		$this->judgeType($tmp);//value is array 
		$this->judgeType(null);//value is null 
		$this->judgeType($cls);//value is other type 
	} 
	private function judgeType($value) 
	{ 
		if(is_numeric($value) === true) 
		{ 
			echo "value is numeric :$value".PHP_EOL; 
		} 
		else if(is_string($value) === true) 
		{ 
			echo "value is string :$value".PHP_EOL; 
		} 
		else if(is_int($value) === true) 
		{ 
			echo "value is int :$value".PHP_EOL; 
		} 
		else if(is_float($value) === true) 
		{ 
			echo "value is float :$value".PHP_EOL; 
		} 
		else if(is_array($value) === true) 
		{ 
			echo "value is array".PHP_EOL; 
		} 
		else if(is_null($value) === true) 
		{ 
			echo "value is null".PHP_EOL; 
		} 
		else 
		{ 
			echo "value is other type".PHP_EOL; 
		} 
	
	} 
	function test2() 
	{ 
		$tmp = []; 
		$tmp[]="100"; 
		$cls = new stdClass(); $this->judgeTypePHPFunction("123", "123[string]"); 
		$this->judgeTypePHPFunction("test", "test[string]"); 
		$this->judgeTypePHPFunction(123, "123[int]"); 
		$this->judgeTypePHPFunction(1.23, "123[float]"); 
		$this->judgeTypePHPFunction($tmp, "array"); 
		$this->judgeTypePHPFunction(null, "null"); 
		$this->judgeTypePHPFunction($cls, "class"); 
		$this->judgeTypePHPFunction(fopen("memo.txt", "r"), "fopen"); 
		//[result] 
		/* 
		get_debug_type(123[string]):string 
		gettype(123[string]):string get_debug_type(test[string]):string 
		gettype(test[string]):string 
		get_debug_type(123[int]):int 
		gettype(123[int]):integer 
		get_debug_type(123[float]):float 
		gettype(123[float]):double 
		get_debug_type(array):array 
		gettype(array):array 
		get_debug_type(null):null 
		gettype(null):NULL 
		get_debug_type(class):stdClass 
		gettype(class):object 
		get_debug_type(fopen):resource (stream) 
		gettype(fopen):resource 
		*/ 
	} 
	private function judgeTypePHPFunction($value ,string $memo) 
	{ 
		echo "get_debug_type($memo):".get_debug_type($value).PHP_EOL; 
		echo "gettype($memo):".gettype($value).PHP_EOL; 
	}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();
$cls1->test2();
echo "</pre>";

get_debug_type($var) (推奨)
PHP 8.0以降で利用可能です。
gettype よりも直感的で正確な名前(例:クラス名など)を返すため、現在のPHP開発ではこちらが推奨されます。
gettype($var)
古くからある関数で、"integer" や "string" などの文字列を返します。
ただし、歴史的経緯により浮動小数点数が "double" と返されるなど、現在の慣習と異なる場合があります。
デバッグや型判定の文字列取得には gettype よりも get_debug_type が推奨されています。
これは、クラス名などをより自然な形で返してくれるためです。


戻る

著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。