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