函数保持从php explode返回未定义的变量而不是数组

时间:2010-02-05 08:29:47

标签: php return-value

rgborhex函数返回一个未定义的变量:

function rgborhex($unformatedColor){
if(strpos($unformatedColor, "-") == false) { //did not find a - in the color string; is not in rgb form; convert
    $rgbColor = hextorgb($unformatedColor);
    $rgbColor = explode("-", $rgbColor);
    return $rgbColor;
}
else { // found a - in the color string; is in rgb form; return
    $rgbColor = $unformatedColor;
    $rgbColor = explode("-", $rgbColor);
    return $rbgColor;
}
}

function hextorgb($hex) {
if(strlen($hex) == 3) {
    $hrcolor = hexdec(substr($hex, 0, 1));      //r
    $hrcolor .= "-" . hexdec(substr($hex, 1, 1));   //g
    $hrcolor .= "-" . hexdec(substr($hex, 2, 1));   //b
}
else if(strlen($hex) == 6) {
    $hrcolor = hexdec(substr($hex, 0, 2));      //r
    $hrcolor .= "-" . hexdec(substr($hex, 2, 2)); //g
    $hrcolor .= "-" . hexdec(substr($hex, 4, 2)); //b
}
return $hrcolor;

}

2 个答案:

答案 0 :(得分:1)

-return $rbgColor;
+return $rgbColor;

在你的第二个return陈述中只是一个错字:)


替代方案 - 次要编辑,更易于阅读IMO:

function rgborhex($unformatedColor) {
    if (strpos($unformatedColor, '-') === false) { //did not find a - in the color string; is not in rgb form; convert
        $unformatedColor = hextorgb($unformatedColor);
    }

    return explode('-', $unformatedColor);
}

答案 1 :(得分:0)

请将error_reporting放在E_ALL上E_STRICT。这使得PHP返回的错误远远超出预期。