我有以下功能安全地得到一个cookie:如何类型传递到一个函数在PHP
public static function get_cookie($parameter, $default)
{
return isset($_COOKIE[$parameter]) ? $_COOKIE[$parameter] : $default;
}
当我尝试读取false
然后在三元运营商使用它,我看到的值被视为字符串(它被铸造成true
)。
我想将一个类型传递给这个函数并且投射这个值,但是没有想法如何。
UPDATE
正如尼科指出,铸造“假”布尔不起作用:http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
我想,我一定要在cookie中存储的字符串始终。 (例如,对于我的情况,“是”和“否”,而不是“假”和“真”)。
你确定你没有把它作为一个字符串开始吗? –
用'gettype'首先测试参数的类型http://php.net/manual/en/function.gettype.php – peacemaker
@John Conde Eh ...传递什么?对不起,我很愚蠢。 – noober