功能php变量未知

时间:2014-04-24 14:02:33

标签: php

function scale($src) {
    $max = 350;
    if (!isset($max, $src))
        return;
    $src = str_replace(" ", "%20", $src[1]);
    $info = @getimagesize($src);
    $sw = $info[0];
    $sh = $info[1];
    $addclass = false;
    $max_em = 0.06 * $max;
    if ($max < max($sw, $sh)) {
        if ($sw > $sh)
            $new = array($max_em . "em", "auto");
        if ($sw < $sh)
            $new = array("auto", $max_em . "em");
        $addclass = true;
    } else
        $new = array("auto", "auto");
    $id = mt_rand(0000, 9999);
    if ($new[0] == "auto" && $new[1] == "auto")
        $img = "<img src=\"" . $src . "\" border=\"0\" alt=\"\" />";
    else
        $img = "<img id=\"r" . $id . "\" border=\"0\" alt=\"\" src=\"" . $src . "\" " . ($addclass ? "class=\"resized\"" : "") . " style=\"width:" . $new[0] . ";height:" . $new[1] . ";\" />";
    return $img;
}

我有这个功能来调整图像大小。当图像更大时它可以完成工作,但是当它们是600x600时,我得到了 注意:未定义的变量:新行

if ($new[0] == "auto" && $new[1] == "auto")

$img = "<img id=\"r" . $id . "\" border=\"0\" alt=\"\" src=\"" . $src . "\" " . ($addclass ? "class=\"resized\"" : "") . " style=\"width:" . $new[0] . ";height:" . $new[1] . ";\" />";

对于这种情况,否则,$ new应为$new = array("auto", "auto");,但无法识别。

3 个答案:

答案 0 :(得分:1)

看下面的部分,它变得非常清楚:

if ($max < max($sw, $sh)) {
        if ($sw > $sh)
            $new = array($max_em . "em", "auto");
        if ($sw < $sh)
            $new = array("auto", $max_em . "em");
        $addclass = true;
    } else
        $new = array("auto", "auto");

将其翻译为值,您会看到..

if ($max < max($sw, $sh)) {
        if (600 > 600)
            $new = array($max_em . "em", "auto");
        if (600 < 600)
            $new = array("auto", $max_em . "em");
        $addclass = true;
        // None of the above ifs meet the case of 600x600 !!!!!
        // ----------------------------------------------------
    } else
        $new = array("auto", "auto");

答案 1 :(得分:0)

$ new永远不会被初始化。您需要在代码顶部添加新内容。所有if语句都不会满足true,$ new永远不会赋值变量。注意:确保在使用变量之前初始化变量。

function scale($src) {
    $max = 350;
    if (!isset($max, $src))
        return;
    $src = str_replace(" ", "%20", $src[1]);
    $info = @getimagesize($src);
    $sw = $info[0];
    $sh = $info[1];
    $addclass = false;
    $max_em = 0.06 * $max;
    $new = array('auto', 'auto'); //Initialized variable per (Catalin Deaconescu)

    if ($max < max($sw, $sh)) {
        if ($sw > $sh)
            $new = array($max_em . "em", "auto");
        if ($sw < $sh)
            $new = array("auto", $max_em . "em");
        $addclass = true;
    } else {
        $new = array("auto", "auto");
        $id = mt_rand(0000, 9999);
    }

    if ($new[0] == "auto" && $new[1] == "auto")
        $img = "<img src=\"" . $src . "\" border=\"0\" alt=\"\" />";
    else
        $img = "<img id=\"r" . $id . "\" border=\"0\" alt=\"\" src=\"" . $src . "\" " . ($addclass ? "class=\"resized\"" : "") . " style=\"width:" . $new[0] . ";height:" . $new[1] . ";\" />";
    return $img;

}

答案 2 :(得分:0)

这里有问题:

if ($sw > $sh)
    $new = array($max_em . "em", "auto");
    if ($sw < $sh)
    $new = array("auto", $max_em . "em");

$ sw == $ sh

时你不匹配