In some rare cases a class instance object returns false when an object but gettype() returns "object".
<?php
$x = new classvar();
$save = serialize($x);
// ......
$obj = unserialize($save);
// here sometimes is_object() returns FALSE
if (is_object($x) || gettype($x) === "object")
{
// ... do something
}
?>
gettype
(PHP 4, PHP 5)
gettype — 변수의 자료형을 얻습니다
설명
PHP 변수 var 의 자료형을 반환합니다.
Warning
gettype()을 어떤 자료형인지 시험하기 위해 사용하지 마십시오. 반환 문자열은 앞으로 나올 버전에서 바뀔 수 있습니다. 또한, 문자열 비교를 수행하기 때문에 느립니다.
대신, is_* 함수를 사용하십시오.
인수
- var
-
자료형을 확인할 변수
반환값
반환 문자열의 가능한 값은:
- "boolean"
- "integer"
- "double" (역사적인 이유로 float의 경우에도 단순히 "float"가 아닌 "double"을 반환합니다)
- "string"
- "array"
- "object"
- "resource"
- "NULL"
- "unknown type"
예제
Example #1 gettype() 예제
<?php
$data = array(1, 1., NULL, new stdClass, 'foo');
foreach ($data as $value) {
echo gettype($value), "\n";
}
?>
위 예제의 출력 예시:
integer double NULL object string
참고
- settype() - 변수의 자료형을 설정
- is_array() - 변수가 배열인지 확인
- is_bool() - 변수가 논리형인지 확인
- is_float() - 변수의 자료형이 소수인지 확인합니다
- is_int() - 변수의 자료형이 정수인지 확인합니다
- is_null() - 변수가 NULL인지 확인합니다
- is_numeric() - 변수가 수나 수 문자열인지 확인합니다
- is_object() - 변수가 객체인지 확인합니다
- is_resource() - 변수가 자원인지 확인
- is_scalar() - 변수가 스칼라인지 확인
- is_string() - 변수의 자료형이 문자열인지 확인합니다
- function_exists() - Return TRUE if the given function has been defined
- method_exists() - 클래스 메쏘드가 존재하는지 확인
gettype
skatebiker at hotmail dot com
22-Feb-2008 10:51
22-Feb-2008 10:51
sneskid at hotmail dot com
06-Mar-2007 06:56
06-Mar-2007 06:56
I wrote my own gettype function by just using the default is_? functions, but it took twice as long as gettype... So I decided to use gettype with a twist.
Taking the warnings about gettype to heart, and depending on your custom needs, it's worthwhile to dynamically test the gettype result with a known variable, and link the result to a predefined result. Like so:
<?php
/*
dynamically create an array by using known variable types
link with a predefined value
*/
$R=array();
$R[gettype(.0)]='number';
$R[gettype(0)]='number';
$R[gettype(true)]='boolean';
$R[gettype('')]='string';
$R[gettype(null)]='null';
$R[gettype(array())]='array';
$R[gettype(new stdClass())]='object';
// what is
function wis_($v){
global $R;
return $R[gettype($v)];
}
echo wis_('hello') . '<br/>'; // "string"
echo wis_(24) . '<br/>'; // "number"
echo wis_(0.24) . '<br/>'; // "number"
echo wis_(null) . '<br/>'; // "null"
echo wis_($R) . '<br/>'; // "array"
?>
You won't need to worry about changes in gettype's return strings in future versions.
If the result evaluates to false then you know the variable tested is some "other" type.
I also find these useful
<?php
function is_num($v){return (is_int($v) || is_double($v));}
function is_box($v){return (is_array($v)||is_object($v));}
echo is_num(null) . '<br/>'; // false
echo is_num(false) . '<br/>'; // false
echo is_num('123') . '<br/>'; // false
echo is_num(123) . '<br/>'; // true
echo is_num(123.0) . '<br/>'; // true
?>
gilthansNOSPAM at gmail dot com
12-Sep-2005 05:18
12-Sep-2005 05:18
NaN and #IND will return double or float on gettype, while some inexistent values, like division by zero, will return as a boolean FALSE. 0 by the 0th potency returns 1, even though it is mathematically indetermined.
<?php
$number = 5/0;
$number2 = sqrt(-3);
$number3 = pow(0, 0);
$number4 = 0/0;
echo $number."<br />";
echo $number2."<br />";
echo $number3."<br />";
echo $number4."<br />";
echo "<br />";
echo gettype($number)."<br />";
echo gettype($number2)."<br />";
echo gettype($number3)."<br />";
echo gettype($number4);
?>
This will return:
-1.#IND
1
boolean
double
integer
boolean
0
1
1
0
PHP Warning: Division by zero in C\test.php on line 2 PHP Warning: Division by zero in C:\test.php on line 5
matt at appstate
17-Dec-2004 04:10
17-Dec-2004 04:10
Here is something that had me stumped with regards to gettype and is_object.
Gettype will report an incomplete object as such, whereas is_object will return FALSE.
if (!is_object($incomplete_obj)) {
echo 'This variable is not an object, it is a/an ' . gettype($incomplete_obj);
}
Will print:
This variable is not an object, it is a/an object
