开发者

function keeps returning undefinded variable instead of array from php explode

the rgborhex function is returing an undefined variable:

function rgborhex($unformatedColor){
if(strpos($unformate开发者_如何学编程dColor, "-") == 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;

}


-return $rbgColor;
+return $rgbColor;

Just a typo in your second return statement :)


Alternative - minor edits, easier to read 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);
}


Please put error_reporting on E_ALL | E_STRICT. This makes PHP to return much more errors than one might expect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜