函数php变量未知

函数php变量未知,php,Php,及 $img=”“; 对于这种情况,else,$new应该是$new=array(“auto”,“auto”),但无法识别。查看以下部分,它变得非常清楚: $img = "<img id=\"r" . $id . "\" border=\"0\" alt=\"\" src=\"" . $src . "\" " . ($addclass ? "class=\"resized\"" : "") . " style=\"width:" . $new[0] . ";height:" . $new

$img=”“;

对于这种情况,else,$new应该是
$new=array(“auto”,“auto”),但无法识别。

查看以下部分,它变得非常清楚:

$img = "<img id=\"r" . $id . "\" border=\"0\" alt=\"\" src=\"" . $src . "\" " . ($addclass ? "class=\"resized\"" : "") . " style=\"width:" . $new[0] . ";height:" . $new[1] . ";\" />";
if($max$sh)
$new=数组($max_em.“em”,“auto”);
如果($sw<$sh)
$new=数组(“自动”,$max_em.“em”);
$addclass=true;
}否则
$new=数组(“自动”、“自动”);
将其转换为值,您将看到

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($max600)
$new=数组($max_em.“em”,“auto”);
如果(600<600)
$new=数组(“自动”,$max_em.“em”);
$addclass=true;
//以上ifs均不符合600x600的情况!!!!!
// ----------------------------------------------------
}否则
$new=数组(“自动”、“自动”);

$new从未初始化过。您需要在代码顶部添加新代码。所有if语句都不会满足true,并且$new也不会被分配变量。注意:确保在使用变量之前初始化了它们

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");
功能比例($src){
$max=350;
如果(!isset($max,$src))
返回;
$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');//初始化变量per(Catalin Deaconescu)
如果($max$sh)
$new=数组($max_em.“em”,“auto”);
如果($sw<$sh)
$new=数组(“自动”,$max_em.“em”);
$addclass=true;
}否则{
$new=数组(“自动”、“自动”);
$id=mt_兰特(0000,9999);
}
如果($new[0]=“auto”&&$new[1]=“auto”)
$img=“”;
其他的
$img=“”;
返回$img;
}
这里的问题是:

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;

}
if($sw>$sh)
$new=数组($max_em.“em”,“auto”);
如果($sw<$sh)
$new=数组(“自动”,$max_em.“em”);

当$sw==$sh

此代码一团糟时,您没有匹配。把括号放在各处。使用缩进。您可能会自己看到这个问题。您没有任何有关
$sw==$sh
的信息。更改
if($sw>$sh)$new=array($max_em.“em”,“auto”);如果($sw<$sh)$new=数组(“自动”,则$max_em.“em”)
如果($sw>$sh)$new=数组($max_em.“em”,“auto”);else$new=array(“auto”、$max_em.“em”)
您应该将
$new
初始化为
数组('auto','auto')
,这样当图像到达
$sw==$sh
案例时,至少他可以显示图像。您必须在其中放入一个else案例,或者在宽度和高度相等时更改一个比较以包括案例。例如:如果($sw)
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");
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;

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