函数认为传递的变量是空的(它不是!)
Function thinks passed variable is empty (it is not!)
我的 PHP 函数认为我传递给它的变量 ($type) 是空的,但是当我告诉函数 return 那个变量 ($type) 时,它 return正是我粘贴的内容。
function getData($result, $player = 0, $type = 0){
// This is here for me to test the bug and it prints $type ok, how when its == 0 ?
if ($type == 0){ return $type; }
if (!empty($result) AND $type != 0){
return $result[$player]->$type;
}elseif (!empty($result) AND $type == 0){
return $result[$player];
}else{
return FALSE;
}
}
这是我在代码中调用该函数的方式:
$map_type = getData($replay, 1, "name");
会不会是包含函数的文件有问题?我认为不,因为该文件中的其他文件甚至功能都可以正常工作。
我检查了很多次,没有看到拼写错误,我实际测试了它,它工作正常。
当变量未发送到函数时将变量设置为 0 是否存在一些错误?同样,我不这么认为,因为 $player 可以工作,而且类似的代码也可以用于我的其他功能。
松散类型的比较,例如 if ($type == 0){
,其中 $type
是一个字符串值,可能会导致意外的结果。
$type
将是 cast to a numeric for the comparison according to the rules defined here。
因此,像 "13 Monkeys"
这样的字符串值将松散地转换为 13
,而 "13 Monkeys" == 0
将 return 转换为 false
;但是没有任何前导数字的值(例如 "name"
将被转换为 0
,因此 "name" == 0
是 true
.
可以在 PHP docs
中找到详细介绍所有不同类型比较(松散比较和严格比较)的有用页面
我的 PHP 函数认为我传递给它的变量 ($type) 是空的,但是当我告诉函数 return 那个变量 ($type) 时,它 return正是我粘贴的内容。
function getData($result, $player = 0, $type = 0){
// This is here for me to test the bug and it prints $type ok, how when its == 0 ?
if ($type == 0){ return $type; }
if (!empty($result) AND $type != 0){
return $result[$player]->$type;
}elseif (!empty($result) AND $type == 0){
return $result[$player];
}else{
return FALSE;
}
}
这是我在代码中调用该函数的方式:
$map_type = getData($replay, 1, "name");
会不会是包含函数的文件有问题?我认为不,因为该文件中的其他文件甚至功能都可以正常工作。
我检查了很多次,没有看到拼写错误,我实际测试了它,它工作正常。
当变量未发送到函数时将变量设置为 0 是否存在一些错误?同样,我不这么认为,因为 $player 可以工作,而且类似的代码也可以用于我的其他功能。
松散类型的比较,例如 if ($type == 0){
,其中 $type
是一个字符串值,可能会导致意外的结果。
$type
将是 cast to a numeric for the comparison according to the rules defined here。
因此,像 "13 Monkeys"
这样的字符串值将松散地转换为 13
,而 "13 Monkeys" == 0
将 return 转换为 false
;但是没有任何前导数字的值(例如 "name"
将被转换为 0
,因此 "name" == 0
是 true
.
可以在 PHP docs
中找到详细介绍所有不同类型比较(松散比较和严格比较)的有用页面