0

在我的网站上,我有一个 PHP 文件,它从外部 PHP 文件生成它的菜单(以使更新更容易)

这是概述问题的PHP的一部分:

$products = array("Kinetic","Key","Twister","Ellipse","Focus","Trix","Classic","Pod","Halo","Alu","Rotator","Canvas","Image","Flip","Executive","Torpedo","Nature","Wafer Card","Alloy Card","Bottle Opener","Ink Pen","Clip","Light","Tie Clip","Event Lanyard","Lizzard Wristband","Slap Wristband");
$others = array("Other 1","Other 2","Other 3","Other 4","Other 5");

function genMenu($product) {
    global $products;
    global $others;

    $menu="<div class='titlebar'><div class='title'>USBs</div></div><div class='buttons'>";

    for ($x=0;$x<count($products); $x++) {
        $p=$products[$x];
        $link=strtolower(str_replace(' ', '', $p)).".php";
        if($product==$p) {$menu=$menu."<span class='activebutton'><span class='textspace'>&raquo; ".$p."</span></span>";}
        else {$menu=$menu."<a href='".$link."'><span class='button'><span class='textspace'>&raquo; ".$p."</span></span></a>";}

        if(count($products)>$x+1) {
            $menu=$menu."<img src='layout/seperator.jpg' class='seperator' alt='' width='184' height='2' />";
        }
    }

    $menu=$menu."</div><div class='titlebar'><div class='title'>Other Products</div></div><div class='buttons'>";

    for ($y=0;$y<count($others); $y++) {
        $o=$others[$y];
        $link=strtolower(str_replace(' ', '', $o)).".php";
        if($product==$o) {$menu=$menu."<span class='activebutton'><span class='textspace'>&raquo; ".$o."</span></span>";}
        else {$menu=$menu."<a href='".$link."'><span class='button'><span class='textspace'>&raquo; ".$o."</span></span></a>";}

        if(count($others)>$y+1) {
            $menu=$menu."<img src='layout/seperator.jpg' class='seperator' alt='' width='184' height='2' />";
        }
    }

    $menu=$menu."</div>";
    return (string)$menu;
}

function genDrop($product) {
global $products;
global $others;

$drop=$products[12];
return $drop;
}

因此,菜单使用我在函数之外的一系列产品生成,通过使它们成为全局来启用它们的使用......我遇到的问题是,当函数返回时$menu,它也会将$products值更改为相同的值。(但不是$others

因此第二个函数 ( genDrop) 从$menu="<div class='t

这是注定要发生的吗?我该如何阻止它?

调用函数的 HTML 中的 PHP:

<?php include("menufoot.php");
$product="Twister";
$products = genMenu($product);
$dropmenu = genDrop($product);
print $dropmenu;
$footer = genFoot($product);
$mobilefooter = genMobFoot($product);
?>
4

0 回答 0