settype() - php 变量处理函数
settype()
(PHP 4, PHP 5, PHP 7)
设置变量的类型
说明
settype(mixed&$var,string $type): bool将变量$var的类型设置成$type。
参数
$var要转换的变量。
$type$type的可能值为:
- “boolean”(或为“bool”,从 PHP 4.2.0 起)
- “integer”(或为“int”,从 PHP 4.2.0 起)
- “float”(只在 PHP 4.2.0 之后可以使用,对于旧版本中使用的“double”现已停用)
- "string"
- "array"
- "object"
- “null”(从 PHP 4.2.0 起)
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
范例
Example #1settype()示例
注释
Note:Maximum value for "int"isPHP_INT_MAX
.
参见
gettype()
获取变量的类型- 类型转换
- 类型戏法
Note that you can't use this to convert a string 'true' or 'false' to a boolean variable true or false as a string 'false' is a boolean true. The empty string would be false instead...
Just a quick note, as this caught me out very briefly: settype() returns bool, not the typecasted variable - so: $blah = settype($blah, "int"); // is wrong, changes $blah to 0 or 1 settype($blah, "int"); // is correct Hope this helps someone else who makes a mistake.. ;)
Using settype is not the best way to convert a string into an integer, since it will strip the string wherever the first non-numeric character begins. The function intval($string) does the same thing. If you're looking for a security check, or to strip non-numeric characters (such as cleaning up phone numbers or ZIP codes), try this instead:
you must note that this function will not set the type permanently! the next time you set the value of that variable php will change its type as well.
To matt: This function accepts a paremeter, which does not imply you using hardcoded stuff, instead you can let the user choose! \o/ As a part of a framework or something. Plus, you can probably call this with call_user_func
Please note: When using settype to convert indexed arrays to objects, the properties of the typed object will be integers: A brief example: $a = ['1', '2']; settype($a, 'object'); var_dump($a); // output object(stdClass)#1 (2) { ["0"]=> string(1) "1" ["1"]=> string(1) "2" }
$foo = "1"; settype($foo, "bool"); var_dump($foo); // Outputs: bool(true) $bar = "0"; settype($bar, "bool"); var_dump($bar); // Outputs: bool(false)
any digit except 0 or -0 are considered true in boolean, and any string except '0' or '' are also considered true. The var_dumps both return the following: The gettype echo will show the value as an integer. So it seems that settype($val,'int') makes the conversion, but the function return value remains a string. Since settype returns a boolean, using is not a option. I resolved my array value conversion using this instead: Thanks to the posting here: http://usrportage.de/archives/ 808-Convert-an-array-of-strings-into-an-array-of-integers.html Perhaps this will save someone else spinning wheels a bit. Also thanks to robin at barafranca dot com for pointing out the boolean return value of settype.
/** * @return bool * @param array[byreference] $values * @desc Convert an array or any value to Escalar Object [not tested in large scale] */ function setobject(&$values) { $values = (object) $values; foreach ($values as $tkey => $val) { if (is_array($val)) { setobject($val); $values->$tkey = $val; } } return (bool) $values; }
note that settype() will initialize an undefined variable. Therefore, if you want to preserve type and value, you should wrap the settype() call in a call to isset(). prints "|0|", NOT "||". To get the latter, use:
settype() has some really strange, potentially buggy behavior. As noted by Michael Benedict, using settype() on a variable will initialize that variable. What is stranger is that using settype() on an uninitialized variable that you are treating as an array or object will also initialize the variable. So: This works for a chain of any length: $foo->bar['baz']->etc Next we look at what happens if $foo is already set. In and of itself, this wouldn't be problematic. It might even make sense. But in all other cases where $foo is defined, even if (boolean) $foo === false, it will throw an error unless $foo->bar is valid (i.e. $foo is an object already).
If you attempt to convert the special $this variable from an instance method (only in classes) : * PHP will silently return TRUE and leave $this unchanged if the type was 'bool', 'array', 'object' or 'NULL' * PHP will generate an E_NOTICE if the type was 'int', 'float' or 'double', and $this will not be casted * PHP will throw a catchable fatal error when the type is 'string' and the class does not define the __toString() method Unless the new variable type passed as the second argument is invalid, settype() will return TRUE. In all cases the object will remain unchanged. Here is the result : Type Succeed? Converted bool 1 Foo Object ( ) Notice: Object of class Foo could not be converted to int in C:\php\examples\oop-settype-this.php on line 9 int 1 Foo Object ( ) Notice: Object of class Foo could not be converted to float in C:\php\examples\oop-settype-this.php on line 10 float 1 Foo Object ( ) array 1 Foo Object ( ) object 1 Foo Object ( ) Warning: settype(): Invalid type in C:\php\examples\oop-settype-this.php on line 14 unknowntype Foo Object ( ) NULL 1 Foo Object ( ) Catchable fatal error: Object of class Foo could not be converted to string in C:\php\examples\oop-settype-this.php on line 15 If the class Foo implements __toString() : So the first code snippet will not generate an E_RECOVERABLE_ERROR, but instead print the same string as for the other types, and not look at the one returned by the __toString() method. Hope this helps ! :)
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)