gettype() - php 变量处理函数
gettype()
(PHP 4, PHP 5, PHP 7)
获取变量的类型
描述
gettype(mixed $var): string返回 PHP 变量的类型$var.
Warning不要使用gettype()来测试某种类型,因为其返回的字符串在未来的版本中可能需要改变。此外,由于包含了字符串的比较,它的运行也是较慢的。
使用is_*函数代替。
返回的字符串的可能值为:
- “boolean”(从 PHP 4 起)
- “integer”
- “double”(由于历史原因,如果是float则返回“double”,而不是“float”)
- “string”
- “array”
- “object”
- “resource”(从 PHP 4 起)
- “NULL”(从 PHP 4 起)
- “user function”(只用于 PHP 3,现已停用)
- “unknown type”
对于 PHP 4,你应该使用function_exists()和method_exists()取代先前将gettype()作用于函数的用法。
参见settype()、is_array()、is_bool()、is_float()、is_integer()、is_null()、is_numeric()、is_object()、is_resource()、is_scalar()和is_string()。
Result from this post http://php.net/manual/en/function.gettype.php#56662 for new PHP 7.2.10: This will return: INF NAN 1 NAN double double integer double
After some testing I found a bug in my function "myGetType": The check for "is_callable" was done before "is_string", so that something like would output: "function reference" instead of "string" "is_callable" and "is_string" can't be checked together in this method, so I've removed the check for is_callable because it's a very rare usage case and if it's a valid string the check for is_callable never executes because is_string would be reached first (or vice-versa). So here is the new function without "is_callable"-Check:
@langpavel at phpskelet dot org Doesn't the gettype return the string already? Using get_class seems to be the only "useful" part of that solution. All that can be reduced to somewhere half in terms of the amount of code.
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. 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
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. Will print: This variable is not an object, it is a/an object
I did some benchmarking and your method here is alot slower then using the actual gettype(); (Using php 5.3.4)
This function returns string representation of type. This is generalization of get_class(). I try to order is_* tests by density of occurence, but is bad idea to use result of this call in conditions for performance reasons, usefull and better than gettype for debugging messages. Note, that last line should not be never executed. Also has no sense to make input parameter optional.
This is my work around for the gettype warning. Hope some find it useful. [EDITOR thiago NOTE: Code has been updated by PPKu-N0-SPAM-schy at mediasoft-berlin dot de]
In some rare cases a class instance object returns false when an object but gettype() returns "object".
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: 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
Hi there, this fastest solution I found to identify a variable type without using gettype function: Maybe someone has a more elegant solution to check for object and resource types... The complete script - including several identify- and timing functions follow: #!/usr/bin/php My console output - try the script several times!: $ ./gettype.php start@ 2015-01-10 01:10:37 $var = 100 - gettype($var) = integer typeof_if_query($var) = int duration: 0.0000500679 typeof_gettype($var) = integer duration: 0.0000309944 typeof_trinary($var) = int duration: 0.0000240803
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)