2010-02-05 50 views
0

的rgborhex功能returing未定义的变量:功能保持阵列从PHP而不是返回undefinded可变爆炸

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

用你的strpos你应该检查=== false,not == false。 – Psytronic 2010-02-05 08:32:50

回答

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); 
} 
+0

哇。深夜编码。那是发生在那里的事!谢谢! – 2010-02-05 08:35:30

+0

不客气:) – jensgram 2010-02-05 08:35:54

0

请把error_reporting放在E_ALL | E_STRICT。这使得PHP返回比预期更多的错误。