开发者

settype() converts to bool instead of string

开发者 https://www.devze.com 2023-01-23 14:25 出处:网络
In PHP, the settype() function as below should change the type of $myvar to a string however when I run it it displays as boolean.But If i change $myvar to $my_var settype correctly changes type to st

In PHP, the settype() function as below should change the type of $myvar to a string however when I run it it displays as boolean. But If i change $myvar to $my_var settype correctly changes type to string. Why is that so?

<?php
$myvar=1995;
echo "The variable is an ".gettype($myvar)."<br>";
$myvar=settype($myvar, "string");
echo "The variable is now a ".gettype($myvar);
?>

Thanks &开发者_开发技巧amp; Regads, rseni.


According to the documentation of settype it changes the variable passed by reference and returns a bool.

Thus $myvar=settype($myvar, "string"); first changes $myvar to a string and then assigns the returned bool. The correct version is:

settype($myvar, "string");
0

精彩评论

暂无评论...
验证码 换一张
取 消